Im doing an exercise of a book which says:
Implement a function that returns a random integer in the range [low, high], where low and high are input parameters. The function prototype should look like this:
int Random(int low, int high);
Be sure to verify that your function implementation works by testing it.
Using your Random function, write a virtual slot machine program. The program should start the player off with $1000, and should display a menu like this:
Player's chips: $1000
1) Play slot. 2) Exit.
If the player enters "1", the program should ask the user to enter in his or her bet. The program needs to verify that a legal bet was placed; that is, a bet greater than 0 and less than or equal to the amount of money the player has. After the player has input his or her bet, the program must calculate three random numbers in the range [2, 7] and output them neatly to the screen. If all three numbers are sevens, then the player wins ten times their betting money; else if, the three numbers are all the same, but not sevens, then the player wins five times their betting money; else if, two out of the three numbers are the same then the player wins three times their betting money; else, the player loses his or her bet. At this point, calculate the player’s new chip amount and redisplay the menu. If at any point the player loses all of his or her chips, a message should be displayed to the player and the program should exit. Also, if the player enters “2” from the menu then the program should exit. Here is an example of what the output should look like:
Player’s chips: $1000
1) Play slot. 2) Exit. 1
Enter your bet: 1500
You did not enter a valid
Place your bet: 1000
3 3 7
You win!
Player’s chips: $3000
1) Play slot. 2) Exit. 2
Exiting…
This is my script(sorry for the layout, i know its quite a mess but i changed things so many times that it messed up):
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int Random(int low, int high);
int main()
{
srand (time(0));
int chips = 1000;
int o = 0;
for(int exit = 0; exit != 1 || chips == 0; )
{
cout << "Player's chips: $" << chips << endl;
cout << "1) Play slot. 2) Exit. ";
cin >> o;
if(o == 1)
{
int bet = 0;
while(bet > chips || bet ==0)
{
cout << "Enter your bet: ";
cin >> bet;
if(bet > chips || bet == 0)
{
cout << "You did not enter a valid bet." << endl;
}
}
int r1 =0;
int r2 =0;
int r3 =0;
r1, r2, r3 = Random(2, 7);
cout << r1 << " " << r2 << " " << r3 << endl;
if (r1 == r2 == r3 == 7)
{
chips += bet * 10;
cout << "You win!" << endl;
}
else if(r1 == r2 == r3 != 7)
{
chips += bet * 5;
cout << "You win!" << endl;
}
else if(r1 == r2 || r1 == r3 || r2 == r3)
{
chips += bet * 3;
cout << "You win!" << endl;
}
else
{
chips -= bet;
cout << "You lost!" << endl;
}
}
else if (o == 2)
{
exit = 1;
continue;
}
else
{
cout << "Invalid option." << endl;
}
}
if(chips == 0)
{
cout << "Sorry, but you lost all your chips. You have to leave." << endl;
}
cout << "Exiting..." << endl;
system("PAUSE");
}
int Random(int low, int high)
{
int result = low + rand() % (high - 1 - low);
return result;
}
But there is a problem. If you test it, the numbers each time you play are almost the same: " 0 0 3" or "0 0 4" or "0 0 5". Whats wrong with it? And, is there a way to simplify it? cause its quite a mess.
Thank you,
Cherry.