Design a program that simulates a slot machine. When the program runs, it should
ID: 3548264 • Letter: D
Question
Design a program that simulates a slot machine. When the program runs, it should do one of the following:
1. Ask the user to enter the amount of money he or she want to insert into the slot machine.
2. Instead displaying images, the program will randomly select a word from the following list: Cherries, Oranges, Plums, Bells, Melons, Bars.
The program will select and display a word from this list three times.
4. If none of the randomly selected words match, the program will inform the user that he or she has won $0. If two of the words match, the program will inform the user that he or she has won two times the amount entered. If three of the words match, the program will inform the user that he or she has won three times the amount entered.
5. The program will ask whether the user wants to play again. If so, these steps are repeated. If not, the program displays the total amount of money entered in the slot machine and the total amount won.
Explanation / Answer
/**
Design a program that simulates a slot machine. When the program runs, it should do one of the following:
1. Ask the user to enter the amount of money he or she want to insert into the slot machine.
2. Instead displaying images, the program will randomly select a word from the following list: Cherries, Oranges, Plums, Bells, Melons, Bars.
The program will select and display a word from this list three times.
4. If none of the randomly selected words match, the program will inform the user that he or she has won $0. If two of the words match, the program will inform the user that he or she has won two times the amount entered. If three of the words match, the program will inform the user that he or she has won three times the amount entered.
5. The program will ask whether the user wants to play again. If so, these steps are repeated. If not, the program displays the total amount of money entered in the slot machine and the total amount won.
**/
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{//Start Main
/******************************************************************************/
/******************************************************************************/
//Declare variables
int i = 0;
int j = 0;
char Play = 'Y';
int bet = 51;
int pot = 50;
int rnum = 0;
srand (time(NULL)); //Seed for random number
//Array to hold the slots
string slot[6] = {"Cherries", "Oranges", "Plums", "Bells", "Melons", "Bars"};
string result[3] = {"null", "null", "null"};
/******************************************************************************/
/******************************************************************************/
//While loop to cpntrol the game
while (Play =='Y'){
//While the make sure you don't bet more money than you have
while(bet>pot){
cout<<"**************************************** ";
cout<<"**************************************** ";
cout<<"**************************************** ";
cout<<" Welcome to the slot machine! ";
cout<<" Please enter your bet: ";
cin>>bet;
cout<<endl;
cout<<"**************************************** ";
cout<<"**************************************** ";
if(bet>pot){
cout<<"You don't have that much money, Chump! ";
}//End if
}//End money checking while loop
pot = pot - bet;
/******************************************************************************/
/******************************************************************************/
//Simulate a spin and record values in 'result' array.
for(i=0;i<3;i++){
rnum = rand() %6 +1;
result[i] = slot[rnum-1];
}
/******************************************************************************/
/******************************************************************************/
//Print out results
cout<<"______________________ ";
cout<<"Slot 1 Slot 2 Slot 3 ";
for (j=0;j<3;j++){
cout<<" | " <<result[j];
}cout<<" |"<<endl<<endl;
/******************************************************************************/
/******************************************************************************/
//check for a win
if(result[0]==result[1] && result[0] == result[2]){
cout<<"Winner! ";
cout<<"All three match! ";
cout<<endl;
cout<<"Beginning pot: "<<pot+bet<<" - "<<bet<<" = "<<pot<<endl;
cout<<"Your bet: "<<bet<<" Your winnings: "<<bet*3<<endl;
pot+=(bet*3);
cout<<"Your pot is now: "<<pot<<"!"<<endl;
}
else if (result[0] == result[1] || result[0] == result[2] || result[1] == result[2]){
cout<<"Winner! ";
cout<<"Two matches! ";
cout<<endl;
cout<<"Beginning pot: "<<pot+bet<<" - "<<bet<<" = "<<pot<<endl;
cout<<"Your bet: "<<bet<<" Your winnings: "<<bet*2<<endl;
pot+=(bet*2);
cout<<"Your pot is now: "<<pot<<"!"<<endl;
}
else{
cout<<"Not a match! ";
cout<<"Beginning pot: "<<pot+bet<<" - "<<bet<<" = "<<pot<<endl;
cout<<"Your bet: "<<bet<<" Your winnings: "<<bet*0<<endl;
cout<<"Your pot is now: "<<pot<<"!"<<endl;
cout<<endl;
}
cout<<"Would you like to play again? (Y or N) ";
cin>>Play;
Play = toupper(Play);
cout<<endl;
/******************************************************************************/
/******************************************************************************/
}
cout<<"Goodbye!";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.