You are on page 1of 6

ASSIGNMENT 2

Efren Martinez
Description

Steer the Rat to the Right Hole

For this assignment we are playing a betting game. The user starts off with a starting amount. Then is asked
the amount they wish to bet. They then try to guess what hole the cheese is in. If they choose correctly the
user wins double his bet amount. If they lose they lose the bet amount. The game keeps going till the user
decides to quit or the user has $0.

The Code

double inhand_cash = 100.00;


double bet_amount = 0;
char want_to_play = 'y';
int mouse_location = 0;
int user_guess = 0;

while (want_to_play == 'y') {


cout << "You are playing. You have $" << setprecision(5) << inhand_cash << " to bet" << endl;

cout << "How much do you want to bet?: " << endl;
cin >> bet_amount;

while (bet_amount <= 0 || bet_amount > inhand_cash || !cin) {

cout << "Invalid amount. Enter new amount: " << endl;

cin.clear();

cin.ignore();

cin >> bet_amount;

}
cout << endl;

srand(time(NULL));

mouse_location = rand() % 3 + 1;

//cout << mouse_location << endl;

cout << "There are three rat holes, one has the cheese" << endl;

cout << "What hole is the cheese in? Type 1 for right, 2 for center and 3 for right:" << endl;

cin >> user_guess;

while (user_guess > 3 || user_guess < 1 || !cin) {

cout << "Must pick a number between 1-3" << endl;

cin.clear();

cin.ignore();

cin >> user_guess;

cout << endl;

if (user_guess == mouse_location) {

cout << "You won!!" << endl;

inhand_cash = inhand_cash + (bet_amount * 2);

cout << endl;

else {
cout << "Wrong guess :(" << endl;

inhand_cash = inhand_cash - bet_amount;

cout << endl;

if (inhand_cash <= 0) {

cout << "Ran out of money. Game Over" << endl;

exit(1);

cout << "Amount in hand: $" << setprecision(5) << inhand_cash << endl;

cout << "Do you want to play again? Type 'y' for yes or 'n' for no: " << endl;

cin >> want_to_play;

while (want_to_play != 'y' && want_to_play != 'n') {

cout << "Invalid input. Enter either y or n: " << endl;

cin >> want_to_play;

cout << endl;

cout << "Game over" << endl << "You're ending amount: $" << inhand_cash << endl;
The output
Screenshots of your runs.
Comments/Notes (Extra Credit)
The comments or the notes section is if you wanted extra credit. This could be struggles you have come
over while doing your program, or additions you wanted to highlight so I notice while grading…

You might also like