Question

In: Computer Science

1. Write a program that plays a simple dice game between the computer and the user....

1. Write a program that plays a simple dice game between the computer and the user. When the program runs, it asks the user to input an even number in the interval [2..12], the number of plays. Display a prompt and read the input into a variable called ‘plays’ using input validation. Your code must ensure that the input always ends up being valid, i.e. even and in [2..12].
2. A loop then repeats for ‘plays’ iterations. Do the following for each iteration:
 generate a random integer in the range of 1 through 6; this is the face value of the
computer’s die; output it clearly labeled as ‘computer’s die’
 generate another random integer in the range of 1 through 6; this is the face value of
the user’s die; output it clearly labeled as ‘user’s die’
 the die with the highest value wins; display the winner for this iteration, computer or
user, or state it was a tie
3. As the loop iterates, the program keeps count of the number of times the computer wins, and of the number of times that the user wins. After the loop performs all of its iterations, the program displays the grand winner, the computer or the user, or it states that the game was tied.

Solutions

Expert Solution

Code (in C):

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(0));
int plays,i,cw=0,uw=0;
printf("Input an even number in the interval [2..12], the number of plays: ");
scanf("%d",&plays);
while(!(plays%2==0 && plays>=2 &&plays<=12))
{
printf("Try again!\n");
printf("Input an even number in the interval [2..12], the number of plays: ");
scanf("%d",&plays);
}
for(i=0;i<plays;i++)
{
int cd = (rand() % (6 - 1 + 1)) + 1;
printf("\nComputer’s Die: %d\n",cd);
int ud = (rand() % (6 - 1 + 1)) + 1;
printf("User’s Die: %d\n",ud);
if(cd>ud)
{
printf("Winner -> Computer\n");
cw++;
}
else if(ud>cd)
{
printf("Winner -> User\n");
uw++;
}
else
printf("Tie\n");
}
if(cw>uw)
printf("\nGrand winner -> Computer\n");
else if(cw<uw)
printf("\nGrand winner -> User\n");
else
printf("\nGrand winner -> Tie\n");
return 0;
}

Please refer to the screenshot of the code to understand the indentation of the code:

Output:

For any doubts or questions comment below.


Related Solutions

For this problem, you will write a program in which the computer plays a dice game...
For this problem, you will write a program in which the computer plays a dice game called Craps. You might need to explore some Python documentation on random() for this homework assignment. PROVIDE A GRAPHICAL FLOWCHART as part of your submission for this solution Rules: The player rolls two six-sided dice. After rolling, the sum of what the dice show is calculated. If the sum is 7 or 11 on the first roll, the player wins. If the sum is...
Write a program that plays an addition game with the user (imagine the user is a...
Write a program that plays an addition game with the user (imagine the user is a 5th grade student). First ask the user how many addition problems they want to attempt. Next, generate two random integers in the range between 10 and 50 inclusive. You must use the Math.random( ) method call.   Prompt the user to enter the sum of these two integers. The program then reports "Correct" or "Incorrect" depending upon the user's "answer".  If the answer was incorrect, show...
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
Write a program in Matlab where the program plays a hot and cold game. The user...
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Program Calculates the distance to the object. Program picks another point, calculates the distance to the object. Program knows its at the right spot when the distance is less than...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The program asks the user questions and then creates a paragraph using the user’s answers. 2.The program must perform the following: a.Uses a Scanner object to ask the user: (The program asks for no other information) i.Full Name (First and Last name only) - stores this Full Name in one String object variable. ii.Age – must be read in as an int. iii.Profession or expected...
Write a simple menu-driven program called “Million Dollar Game”. The dice that we are using for...
Write a simple menu-driven program called “Million Dollar Game”. The dice that we are using for this game are special oriental dice. Each die has six different patterns. The six possible patterns are: Fish, Shrimp, Crab, Chicken, goldenCoin, Barrel. This game has three dice. In this game, the player can place his or her bet on any die-pattern. The amount of the winning prize is directly proportional to the number of matched dice after the two dice have been tossed...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out the state of the board, asks the user where they would like to place their mark, and implements this decision. The program then places its own mark on a randomly chosen available position. Once one of the player won, the program declares the result and asks if the user would like to continue. The first player is selected at random.
Write a Python program to simulate a very simple game of 21 •Greet the user. •Deal...
Write a Python program to simulate a very simple game of 21 •Greet the user. •Deal the user a card and display the card with an appropriate message. •Deal the user another card and display the card .•Ask the user if they would like a third card. If so, deal them another card and display the value with an appropriate message. •Generate a random number between 10 and 21 representing the dealer’s hand. Display this value with an appropriate message....
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT