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 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...
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...
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...
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...
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.
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random number with min number being 1 and max being 10 import random min_number = 1 max_number = 10 rand = random.randint(min_number, max_number) #Prints the header, welcoming the user print("Welcome to the Guess My Number Program!") while (True): #While loop, comparing the users guessed number with the random number. #If it matches, it will say you guessed it. guess = eval(input("Please try to guess my...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a random choice (Rock, Paper or Scissors) then ask the user to choose Rock, Paper or Scissors. After that the program will display its choice and a message showing if the player won, lost or tied. Next, the program should prompt the user to play again or not. Once the player selects to stop playing the game, the program should print the number of wins,...
A gambler plays a dice game where a pair of fair dice are rolled one time...
A gambler plays a dice game where a pair of fair dice are rolled one time and the sum is recorded. The gambler will continue to place $2 bets that the sum is 6, 7, 8, or 9 until she has won 7 of these bets. That is, each time the dice are rolled, she wins $2 if the sum is 6, 7, 8, or 9 and she loses $2 each time the sum is not 6, 7, 8, or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT