随机化一个二维数组?

时间:2010-10-23 11:50:10

标签: c++

我正在开发一款用户玩老虎机的游戏。老虎机需要将9个字段中的3个不同符号随机化,例如符号:

A A A. O A O. X X X

我使用的是二维数组,但我不知道如何随机化数组? 第二个问题。我不知道如何比较数组中的符号? 游戏的目标是尽可能多地获得相同符号的行,列和对角线。在上面的例子中,当上下行具有相等的符号时,获得了利润,部分为2x行。根据具有相同符号的行数,用户获得支付,因为利润系统如下:

  • A系列提供2 * bet
  • 两行给3 *下注
  • 三行给4 *下注
  • 四行给出5 *下注
  • 五行给出7 *下注
  • 完全比赛场地10 *下注

我不知道如何用行和钱解决这个问题? 有人可以帮我处理代码吗?

这是我的二维数组代码: 我不知道我是否做对了,所以有人可以帮助我。我刚开始学习c ++。我想随机化符号并进行比较。

char game[3][3] = {{'X','X','X'}, {'A','A','A'}, {'O','O','O'}};

cout << game[0][0] << game[0][1] << game[0][2] << "\n";
cout << game[1][0] << game[1][1] << game[1][2] << "\n";
cout << game[2][0] << game[2][1] << game[2][2] << "\n";


#include <iostream>
#include <cstdlib>   
#include <ctime>   
#include <cmath> 
using namespace std;  
void insertmoney();
void newgame();
void slot();
int money, moneyleft = 0;  
int bet;


int main()   
{

    int mainmenu = 1;  
    system("CLS"); 
    cout << endl;
    cout << "Options:\n" << endl;
    cout << " [1] Start New Game\n\n\n"
    << " [2] Quit\n\n";  
    cin >> mainmenu;
    if (mainmenu < 1 || mainmenu > 2)   
    {
     system("CLS");
     main();   
    }
    if(mainmenu == 1)
    {
     insertmoney();
    }
    if(mainmenu == 2)
    {

     return 0; 

    }

    newgame();  
    slot();

system("pause");     
return 0;  
}

void insertmoney()   
{
     system("CLS");
     do
     {
     cout << "Please insert money: 100 SEK, 300 SEK or 500 SEK\n";
     cin >> money;
     }while(money != 100 && money != 300 && money != 500);  
     cout << "Insert is accepted!" << endl;
     cout << "Current insert is: " << money << endl;       
     moneyleft += money;       

system("pause");
}

void newgame()
{
     system("CLS");
     do
     {
     cout << "Please place your bet: " << endl;
     cout << "OBS: You are not allowed to exceed the amount of insert money!" << endl;
     cin >> bet;
     }while(bet > money);   
     cout << "Bet is accepted!" << endl;
     cout << "Current bet is: " << bet << endl;
     moneyleft -=bet;      

system("pause");
}

void slot()
{
    system("CLS");
    int slotmenu;
    cout << endl;
    cout << "[1] Play\n"
    << "[2] Main Menu\n\n";
    cin >> slotmenu;
    if (slotmenu == 1)
    {
        system("CLS");
        cout << "Your bet is: " << bet << endl;
        cout << "The game is on!!!" << endl;
        cout << endl;
        bet--;

        char symbs[] = {'O','X','A'}; 
        char game[3][3];   

        for (int i = 0; i != 3 ; i++)  
        { 
            for (int j = 0; j != 3 ; j++) 
            { 
                int rndnum = round((double)rand() / (double) RAND_MAX * 3); 
                game[i][j] = symbs[rndnum]; 
            } 
        }
    } 
}

3 个答案:

答案 0 :(得分:1)

三个符号A,O和X是否应该以相等的分布出现,例如每个三个?在那种情况下:

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    char game[3][3] = {{'A','A','A'}, {'O','O','O'}, {'X','X','X'}};

    srand(time(0));
    std::random_shuffle(game[0], game[3]);

    for (int y = 0; y < 3; ++y)
        std::cout << " " << game[y][0] << " | " << game[y][1] << " | " << game[y][2] << "\n";
}

答案 1 :(得分:0)

  

我使用的是二维数组,但我不知道如何随机化数组?

Google for rand和c ++。 rand()是一个为您提供伪随机数的函数。

  

我不知道如何比较数组中的符号?

符号?你的意思是char对吗?如果您输入'X'这是一个值,准确地说是88。寻找ASCII表。

int c = 'X'; // makes c hold 88
int a = c; 
cout << a; //prints 88

如果您想比较两个值,只需使用==, <, >, <=, =>或您需要的任何值。 char就像int:

一样工作
char c = 'X';
char d = 'O';
if (c > d)
  

我不知道如何用行和钱解决这个问题?有人可以帮我处理代码吗?

你真的应该做自己的功课。这就是它的重点。您可以先尝试解决更简单的问题,这样您就会感觉它是如何工作的。例如,编写代码以查看是否有一行?如果有一个系列?如果您需要有关特定问题的帮助,请随时提出! (确保发布几乎正常工作的代码)

祝你好运!

答案 2 :(得分:-1)

#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main(){
    char symbs[] = {'0','X','A'}; // array to convert int to symbol

    char game[3][3]; // make game array 


    //randomise game array
    for (int i = 0; i != 3 ; i++) {
        for (int j = 0; j != 3 ; j++) {
            int rndnum = round((double)rand() / (double) RAND_MAX * 3);
            game[i][j] = symbs[rndnum];
        }
    }

    //count for number of lines equal;
    int lineseq = 0;

    if ((game[0][0]) ==  game[0][1]) && (game[0][0]) ==  game[0][2]) ) {
        lineseq += 1;
    }

    /*
    .., repeat for other combinations
    */

    return 0;
}