views:

110

answers:

3

I'm developing a slot machine game. The player insert amount of money and makes a bet to play. And 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 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.

 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 result of the random symbols. ????
+3  A: 

You're selecting between the letters 'A', 'O', and 'X', right?

#include <algorithm>

char randomSymbol()
{
    char symbol = rand() % 3 + 'A';
    if(symbol == 'B') symbol = 'O';
    else if(symbol == 'C') symbol = 'X';
    return symbol;
}

void populateSlotField(char field[3][3])
{
    // pass your array as an argument to this function
    // note that 9 is because you said your slot field was 3x3
    // for a different sized field, adjust accordingly
    std::generate(field[0], field[0] + 9, randomSymbol);
}
PigBen
Thanks. But can I use this code with a two-dimensional array? How do I do then?
Atb
@Nelly -- See updated answer
PigBen
thank you. If I want to put the field on the screen? should I just code this way then: cout << field[0][0] << field[0][1] << field[0][2] << "\n";
Atb
I really want to use this code because it seem to be really good. How do I make the field to be shown on the screen?
Atb
@Atb -- I don't know how you "should" display it. That's up to you. By your comment, you seem to understand how to use cout, so I don't really understand what problem you are having.
PigBen
A: 

You could do it by using an array instead

#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int main(){
    char symbs[] = {'0','X','A'};

    for (int i = 0; i != 9 ; i++) {
        int rndnum = round((double)rand() / (double) RAND_MAX * 3);
        cout << symbs[rndnum];
    }
    cout << endl;

    return 0;
}

where (double)rand() / (double) RAND_MAX gives you a random double from 0→1

Twig
Thank you very much for helping me. I will use this function.
Atb
But does anyone know how to solve the second problem with the money? Someone who can give me som tips?
Atb
For this its probably just best to check manually for the combinations, as you only have 5 to check(top, middle and bottom, and diagonals)
Twig
Ok I will do that. But I was just wondering. Can I use a two-dimensional array instead? How do I change to that array? I just started learning c++ so I have some troubles. Thanks for helping me out.
Atb
The array in the code above is just to turn integers 0,1,2 into the letters. You could use a 2 dimentional array(3x3) to store the output, such that you could check it or a 1 dimentional array of 9 would work equally well
Twig
A: 

Here is a note about c++ random numbers, there are some point to be handled... Otherwise, values you generated may not be truely random.

UPDATE: If i didnt get you wrong, you are asking for this... Thats a pretty good examle i think, and contains more than what you need, so just rip the part you needed...

FallenAngel
Can I use a two-dimensional array instead for the symbols?
Atb
Answer updated...
FallenAngel