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. ????
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);
}
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
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...
精彩评论