随机化一个二维数组? [英] randomise a two-dimensional array?

查看:110
本文介绍了随机化一个二维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing a game where the user plays a slot machine. The slot machine needs to randomise the 3 different symbols in nine pieces fields, for example the symbols:

AAA
OAO
XXX

A A A O A O X X X

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

I am using a two-dimensional array but I don't know how to randomise the array? The second problem. I don't know how to compare the symbols in the array? The goal of the game is to obtain as many rows, columns, and diagonals as possible of the same symbol. In the above example, obtained a profit when the upper and lower line have equal symbols, partly 2x lines. Depending on the number of rows with the same symbol the user gets paid as the profit system follows:


  • A系列提供2 * li>
  • 两行给予3 * bet

  • 三行给予4 * bet

  • / li>
  • 五行给予7 *下注

  • 完全比赛场地给予10 *下注

  • A series provides 2 * bet
  • Two lines giving 3 * bet
  • Three rows giving 4 * bet
  • Four rows gives 5 * bet
  • Five lines gives 7 * bet
  • Fully playing field gives 10 * bet

我不知道如何解决这个问题的行和钱?
有人可以帮助我的代码?

I don't know how to fix this problem with the rows and money? Can someone help me with the code?

这是我的二维数组的代码:
我不知道如果Im doin正确的事情所以可以有人请帮助我。我刚开始学习c ++。我要随机分配符号并比较它们。

This is my code for the two-dimensional array: I don't know if Im doin the right thing so can someone please help me. I just started learning c++. I wan to randomise the symbols and compare them.

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]; 
            } 
        }
    } 
}


推荐答案

如果三个符号A,O和X以相等的分布出现,例如三个每个?在这种情况下:

Should the three symbols A, O and X appear with equal distribution, e.g. three of each? In that case:

#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";
}

这篇关于随机化一个二维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆