tags:

views:

106

answers:

2

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????
+1  A: 

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!

Jacob
A: 

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.

SteveRawlinson
I think you just made my day!!! God bless you. I just posted a new question but I don't think I needed to. Im gonna try this one now directly!! im so happy :D:D:D I hope it works
Atb
Just one question. How do I get the symbols(a, x, o) insted of numbers?
Atb
@Atb, you need to understand that SO is a community. I've pretty much done all the work for you and yet you have not accepted or even voted my answer. And you *still* want more help.
SteveRawlinson
Im sorry. Im really thankfull for all the help I got from you. It's just that I'm new on this site and I don't know how it works fully yet and I'm really busy to get my code to work because I'm not very good on c++, the fact is I suck! I have read a book but I'm still not good in making my own functions :/ I'm sorry but you should now that your help has been very usefull for me.
Atb
But do you know how to change the numbers into symbols? You don't have to help me if you don't want to. It's just that I've been working a long time on this game and that's the only thing I got left to solve :/:/ Thank you for all the help you've been very nice to me :)
Atb
Here's a hint: you do not have to change the numbers into symbols in the array - only when you are displaying them. So the only bit you need to change is the cout << bit.
SteveRawlinson