Are you Even/ Odd? Game Simulation Write a C++ program to simulate the following
ID: 3814547 • Letter: A
Question
Are you Even/ Odd? Game Simulation Write a C++ program to simulate the following game: 1. The player has two dice to ro 2. The numbers that are rolled on the dice are added and the sum (Sum1) is checked to see if it is even or odd 3. If Sum1 is even, then the player gets one more chance to roll the dice. a. After the player rolls the dice, the sum is generated again. Let us call it Sum2. b. Now Sum2 is added with Sum1 and the total is checked to see if it is Even or odd. i. If Even, the player won the game. Display appropriate message and display the total (Sumi Sum2) as the Number of points earned. ii. If Odd, the player lost the game. Display appropriate message. (Do not have to display points/total) 4. If Sum1 is Odd, the player lost the game. Display appropriate message. (Do not have to display points/total) The player can repeat this game any number of times. Ask the player if he wants to play the game again. If yes, repeat, else stop. (Hint: Can use yor 'y" to represent yes and 'n or 'N to represent No. A sample Run should look like this Rolling the Dice!! Let's assume the dice reads, 2 and 4. Now Suml is 6, that means roll the dice again: you got 2 and 4. The total is 6 and is even, so Hurray!! you get another chance! Rolling Dice Again!Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <ctime>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/* Even/Odd Game Logic */
void odd_game_simulate(){
int sum1 = 0, sum2 = 0;
int dice1_num1_try1, dice2_num2_try1;
dice1_num1_try1 = rand() % 6 + 1;
dice2_num2_try1 = rand() % 6 + 1;
sum1 = dice1_num1_try1 + dice2_num2_try1;
if((sum1 % 2) == 0){
std::cout<<"You got "<<dice1_num1_try1<<" and "<<dice2_num2_try1<<". The total is "<<sum1<<" and is even, so Hurray!! you get another chance!"<<std::endl;
std::cout<<"Rolling Dice Again!"<<std::endl;
int dice1_num1_try2, dice2_num2_try2, final_sum = 0;
dice1_num1_try2 = rand() % 6 + 1;
dice2_num2_try2 = rand() % 6 + 1;
sum2 = dice1_num1_try2 + dice2_num2_try2;
final_sum = sum1 + sum2;
if(final_sum % 2 == 0){
std::cout<<"You got "<<dice1_num1_try2<<" and "<<dice2_num2_try2<<". The total is "<<sum2<<" and grand total is "<<final_sum<<". It's even and therefore, you won. Congratulations!! "<<std::endl<<" You secured a total of "<<final_sum<<" for this game "<<std::endl;
}else{
std::cout<<"You got "<<dice1_num1_try2<<" and "<<dice2_num2_try2<<". The total is "<<sum2<<" and grand total is "<<final_sum<<". It's odd. Sorry, you lost this game."<<std::endl;
}
}else{
std::cout<<"You got "<<dice1_num1_try1<<" and "<<dice2_num2_try1<<". The total is "<<sum1<<" and is odd. Sorry, you lost this game."<<std::endl;
}
}
int main(int argc, char** argv) {
goto START_GAME;
while(true){
char enter_ch;
std::cout<<"Do you want to play the game again"<<std::endl;
std::cin>>enter_ch;
switch(enter_ch){
case 'Y':
START_GAME:
std::cout<<"Rolling the Dice!"<<std::endl;
odd_game_simulate();
break;
case 'N':
exit (EXIT_FAILURE);
break;
default:
std::cout<<"Invalid input. Enter Y to play the game again or N to exit the game."<<std::endl;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.