Long Programming Exercise 1 Write a program to play a dice g

Long Programming Exercise 1: Write a program to play a dice game against the computer. The game is very simple. The computer rolls a dice and you roll a dice. If your dice is greater in value than the computer\'s dice you win. If the computer\'s dice is greater in value than yours, the computer wins. Nobody wins in a tie. Generating random numbers: Throwing a dice is basically like generating random numbers between 1 and 6. You will have to do this once for the computer and once for the user. The C libraries have a built-in function to generate random numbers. To generate random numbers you will need to follow the steps listed below: Include stdlib. h and time. h in your preprocessor directives In the main body of the program, after all variables have been initialized use the srand () function to seed your random number generator rand (). srand(time(NULL)); more_statements, Dice = (rand() % 16)+ 1;/* random number 1 to 6 */Program Steps (pseudocode): Intro block comment Begin main () function Initialize variables including one to store the starting number of tokens = 100, wager, computer\'s dice face value, and user\'s dice face value. Seed the random number generator Explain program to user Ask the user if the user wants to play. This involves reading in a character/number from the user (and therefore a variable). The User will start with 100 tokens. If the user says yes - you can do this by asking the user to enter \'1\' or \'y\': Ask the user to place a wager. Remember to check that the wager is valid (i.e. not number of tokens) \"Roll\" the dice - remember you will have to do this twice, once for the user and once for the computer Inform the user of each dice value If the computer dice value is greater than the user dice value subtract the wager from the number of tokens If the user dice value is greater, add the wager to the number of tokens If the number of tokens is 0, inform the user that they have lost the game If the number of tokens is 200, inform the user they have won the game Return to step 6. At this point remember that you must initialize the starting number of tokens back to 100 If the user says no - by entering \'0\' or \'n\' end the game.

Solution

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int tokens; //variable for storing tokens value
int wager; //variable for storing wager value
int computer_diceface_value; //variable for storing computer_diceface_value value   
int user_diceface_value; //variable for storing user_diceface_value value
int wish; //variable for storing wish of the user i.e whether he wants to play or not
srand(time(NULL)); //seeding random number
printf(\"It is a dice game with computer,the computer rolls a dice and you roll a dice \ \"); //explaining the //game to user
printf(\"If computer\'s dice is greater in value than yours,the computer wins,Nobody wins a tie \ \");
printf(\"You want to play this game or not if yes then either enter \'1\' or \'y\',if not please enter \'0\' or \'n\'. \ \");
wish=getchar();
if(wish==\'y\'|| wish==\'1\') //if user wants to play
{while(wish==\'y\'|| wish==\'1\')
{int tokens=100;
printf(\"Please enter the wager,wager must be greater than 0 and less than equal to tokens i.e 100 \ \"); //asking the value of wager from user
scanf(\"%d\",&wager);
while(wager<=0 || wager > tokens){
printf(\"Please enter the valid wager which must be greater than 0 and less than equal to tokens i.e 100 \ \"); //asking for the correct value of wager
scanf(\"%d\",&wager);}
while(tokens != 0 || tokens != 200) //while token is not equal to 0 or 100 game will be in //continue mode
{
printf(\"Rolling the dice for the user \ \");
user_diceface_value=(rand() % 6 ) + 1;
printf(\"Rolling the dice for the computer \ \");
computer_diceface_value=(rand() % 6 ) + 1;
printf(\"Your dice value %d \ \", user_diceface_value);
printf(\"Computer\'s dice value %d \ \", computer_diceface_value);
if(computer_diceface_value > user_diceface_value ) //computer wons
tokens=tokens-wager;
else if(computer_diceface_value < user_diceface_value) //user wons
tokens=tokens+wager;
           else //handaling the case of tie
           tokens=tokens;
}
if(tokens==200) //tokens=200,user won
{printf(\"You won the dice game \ \");
printf(\"You want to play this game or not if yes then either enter \'1\' or \'y\',if not please enter \'0\' or \'n\'. \ \");
wish=getchar();
}
else
{printf(\"You lost the dice game\");
printf(\"You want to play this game or not if yes then either enter \'1\' or \'y\',if not please enter \'0\' or \'n\'. \ \");
wish=getchar();
}

}

  
  
}
else //if user doesnot want to play exit the game.
exit(1);
  

return 0;
} //end of code

****************Output***************
It is a dice game with computer,the computer rolls a dice and you roll a dice   
If computer\'s dice is greater in value than yours,the computer wins,Nobody wins a tie   
You want to play this game or not if yes then either enter \'1\' or \'y\',if not please enter \'0\' or \'n\'.
1
Please enter the wager,wager must be greater than 0 and less than equal to tokens i.e 100
2
Rolling the dice for the user   
Rolling the dice for the computer   
Your dice value 5   
Computer\'s dice value 4   
Your dice value 3
Computer\'s dice value 6

You lost the dice gameYou want to play this game or not if yes then either enter \'1\' or \'y\',if not please enter \'0\' or \'n\'.

*******************Output***************

Please let me know in case you have any doubt.

 Long Programming Exercise 1: Write a program to play a dice game against the computer. The game is very simple. The computer rolls a dice and you roll a dice.
 Long Programming Exercise 1: Write a program to play a dice game against the computer. The game is very simple. The computer rolls a dice and you roll a dice.

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site