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 <cstdlib>
#include <ctime>
#include <cmath>
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];
}
}
}
}
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";
}
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 char
s 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!
#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;
}
精彩评论