views:

104

answers:

4

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:

A A A O A O X X X

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 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?

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

include

include

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: 
if(game[0][0] == game[0][1])
{
...
}
etc...
PigBen
A: 
#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;
}
Twig
thank you that looks like a really good solution. but my compiler says: "In function `int main()': warning: converting to `int' from `double', expected `,' or `;' before "if"
Atb
sorry but I'm new with c++ so I don't understand what the compiler means
Atb
this is just a warning that can be ignored, because rand() produces an integer between 0→RAND_MAX, so it is a warning for you to be careful, but in this case if you try to divide two integers (without `(double)` it will 'floor' the result giving 0 every time
Twig
@Magnus Hoff: D'oh. You're absolutely right. Prior suggestion withdrawn. (It must be time for bed if I'm conflating "==" and "=".)
Eric Towers
A: 

I am using a two-dimensional array but I don't know how to randomise the array?

Google for rand and c++. rand() is a function that gives you pseudo random numbers.

I don't know how to compare the symbols in the array?

Symbols? You mean chars right? If you type 'X' that's a value, 88 to be exact. Look for ASCII table.

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

If you want to compare two values, just use ==, <, >, <=, => or whatever you need. A char works just like an int:

char c = 'X';
char d = 'O';
if (c > d)

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

You really should make your own homework. That's the point of it. You could try solving easier problems first, so you get the feeling how it works. For example, write code to see if there is a line? If there is a series? If you need help on a specific problem feel free to ask! (Make sure you post your almost working code)

Good luck!

Ishtar
Thanks you the tip. I have allready done the most of the previous coding just didn't post it here. I worked for days to solve this two problems and I just can't do it. That's why I turned to this site. I tried to fix the code on my own for days, just to get the work done on my own. But I could'n fix it. So this site was my only option to get help. Thank you!
Atb
@Nelly - Can you edit your question and put all of your code in it? Would be a lot easier for us to give you some good advice.
Ishtar
I can do that :) It's not much left on the coding.
Atb
Do you have any good advice for my code?
Atb
Hmm. Try making functions like `bool hasLine(char slots[][])` and test them. Test them with a small main function. Like in your old code: `int main(){ char game[3][3]={...} if (hasLine(game)) {...` Try it with different char game. You don't have to execute code that is working. Make useful functions, and test them! When they all work, "glue" them together. You see?
Ishtar
@Nelly - `bool hasLine(char slots[][])` , `int randomNumber(int min,int max)`, `void makeRandom(char slots[][])`, etc. Maybe change some methods to `bool wantsPlayNewGame()`, `int getBetAmount()`, ...
Ishtar
A: 

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