Atb
Atb

Reputation: 49

for-loop to compare the two-dimensional array???Help!

I am currently developing a game in which a player plays slot machine.The game is based on that the user stops in money, 100sek, 300sek or 500sek. Then the user makes a bet for each game. The one-armed bandit randomly spits out 3 different symbols in nine pieces fields. See figure:

alt text The goal of the game is to obtain as many rows, columns, and diagonals as possible of the same symbol. In the above example that I made, obtained a profit when the upper and lower line have equal symbols, 2 simbles of the same in a row (aaa). The following profit system is depending on the number of rows with the same symbol as:

•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 dont know how to solve this problem with the paying? What code can I use? Should I use a for-loop? I'm new with c++ so I'm having trouble with this. I' been spending a lot of hours on this game and I just can't solve it. Please help me! Here's a small part of my code for now: I just want to compare the results. Can somone help me to compare the result after the previous goals I just described. Im thankfull for all the help I get!

 srand(time(0)); 
 char game[3][3] = {{'O','X','A'}, {'X','A','X'}, {'A','O','O'}}; 

 for (int i = 0; i < 3; ++i) 
 { 
 int r = rand() % 3; 
 cout << " " <<game[r][0] << " | " << game[r][1] << " | " << game[r][2] << "\n"; 
 cout << "___|___|___\n"; 
 } 


 //.....compare the two-dimensional array????

Upvotes: 0

Views: 5032

Answers (2)

SteveRawlinson
SteveRawlinson

Reputation: 915

I'm not sure that I understand your requirements perfectly but have decided to interpret them as best I can. This code does what I think you want. I've used 0, 1 and 2 as the characters but you can easily swap them for whatever you want in the output. I've also not bothered calculating the payout since you've written the pseudo-code for that in your question.

int game[3][3];
int x, y;
int lines = 0;

// select a random grid
srand(time(0));
for(x = 0; x < 3; x++) {
    for(y = 0; y < 3; y++) {
        game[x][y] = rand() % 3;
        cout << game[x][y];
        if (y == 2)
            cout << '\n';

    }
}

// count horizontal lines
for (y = 0; y < 2; y++)
    if (game[0][y] == game[1][y] && game[0][y] == game[2][y])
        lines++;

// vertical
for (x = 0; x < 2; x++)
    if (game[x][0] == game[x][1] && game[x][0] == game[x][2])
        lines++;

// diagonal
if (game[0][0] == game[1][1] && game[0][0] == game[2][2])
    lines++;
if (game[0][2] == game[1][1] && game[0][2] == game[2][0])
    lines++;

cout << lines << " lines\n";

By the way that's my first ever c++ program.

Upvotes: 0

Jacob
Jacob

Reputation: 34601

You can use comparison operators to check if elements in your matrix are equal to each other. You already know how to access each element of your matrix (game[r][0], etc.).

Now, all you have to do is check if elements along a row, diagonal, etc. are equal.

Additionally, you need to store your randomly generated symbols. You should create a new matrix (or 2D array) called, say, results.

E.g.

char results[3][3];

Now when you're displaying the slot machine results, store it in this 2D array:

for(int i = 0; i < 3; ++i)
{
   int r = rand() % 3;
   cout << " " <<game[r][0] << " | " << game[r][1] << " | " << game[r][2] << "\n"; 
   cout << "___|___|___\n";
   // Store
   for(int j = 0; j < 3; ++j) results[r][j] = game[r][j];
}

Above, I've just used another loop to store what you've displayed.

To get you started, here's how to check along a horizontal line for all 3 rows of your matrix:

bool horz_equal[3];
for(int r = 0; r < 3; ++r) 
{
  horz_equal[r] = (results[r][0] == results[r][1] && results[r][1] == results[r][2]);
}

Additionally, you should think about the method you have for randomly generating symbols. There are better ways!

Upvotes: 1

Related Questions