首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >随机二维阵列?

随机二维阵列?
EN

Stack Overflow用户
提问于 2010-10-23 11:50:10
回答 2查看 1.3K关注 0票数 0

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

一个O,X,X

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

  • A系列提供2*打赌
  • 两行,给3*打赌
  • 3行下注
  • 4*投注

<代码>H195行下注<代码>H 210H 111全场给出10 *投注<H 212<代码>F 213

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

这是我的二维数组的代码:我不知道我是否做了正确的事情,所以有人能帮我吗?我刚开始学习c++。我想随机化符号并比较它们。

代码语言:javascript
运行
复制
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]; 
            } 
        }
    } 
}
EN

回答 2

Stack Overflow用户

发布于 2010-10-23 12:23:57

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

谷歌的兰德和c++。rand()是一个给出伪随机数的函数。

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

符号?你是说char对吗?如果键入'X',准确地说是88。寻找ASCII表。

代码语言:javascript
运行
复制
int c = 'X'; // makes c hold 88
int a = c; 
cout << a; //prints 88

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

代码语言:javascript
运行
复制
char c = 'X';
char d = 'O';
if (c > d)

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

你真的应该自己做作业。这就是关键所在。你可以先尝试解决更简单的问题,这样你就能感觉到它是如何工作的。例如,编写代码看看是否有一行?如果有系列的话?如果你在一个特定的问题上需要帮助,那就随便问吧!(确保您发布了几乎可以工作的代码)

祝好运!

票数 0
EN

Stack Overflow用户

发布于 2010-10-23 12:02:02

代码语言:javascript
运行
复制
#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;
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4003814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档