Question

In: Computer Science

C program with functions Make a dice game with the following characteristics: must be two players...

C program with functions

Make a dice game with the following characteristics:

must be two players with two dice.

when player 1 rolls the marker is annotated

when player 2 rolls the marker is annotated

in other words, for each roll the added scores must be shown.

Example:

Round 1:

player 1 score = 6

player 2 score = 8

Round 2:

player 1 score = 14

player 2 score = 20

Whoever reaches 35 points first wins.

if a player goes over 35 points, the last shot does not count, example:

If a player is at 30 points and when he rolls the two dice he gets 6 points, the sum (30 + 6) would be 36, it goes over 35 so that throw does not count and the score remains at 30.

Whoever gets double 3 or double 5 loses automatically.

Solutions

Expert Solution

here is the required solution

here is the code

#include <stdio.h>
#include<stdlib.h>
//function to get roll the dice
int rolldice()
{ //roll of dice1 and dice2
int dice1=rand()%6+1;
int dice2=rand()%6+1;
if(dice1==dice2 && (dice1==3 || dice1==5))
return -1;
else
return dice1+dice2;
}
int main()
{ //declaration of relevant variable
int player1=0,player2=0,prev1=-1,prev2,tem, i=1;
while(1)
{ printf("Round %d\n",i++);
tem=rolldice();
//check if double 3 or 5 occur
if(tem==-1)
{   
printf("Player 2 win");
break;
}
prev1=tem;
  
//add score if after adding score is less than 36
if(player1+prev1<=35)
player1=player1+prev1;
//if player1 reaches 35 than won
if(player1==35)
{
printf("player 1 win");
break;
}
printf("player 1 score=%d\n",player1);
tem=rolldice();
//check if double 3 or 5 occur
if(tem=-1)
{
printf("Player 1 win");
break;
}
prev2=tem;
//add score if after adding score is less than 36
if(player2+prev2<=35)
player2=player2+prev2;
printf("player 2 score=%d\n",player2);
//if player1 reaches 35 than won
if(player2==35)
{printf("Player 2 win");
break;}
  
//print the score
printf("player 1 score =%d\n",player1);
printf("player 2 score =%d\n",player2);
}
return 0;
}


Related Solutions

Craps is a dice game in which the players make wagers on the outcome of the...
Craps is a dice game in which the players make wagers on the outcome of the roll, or a series of rolls, of a pair of dice. Most outcomes depend on the sum of the up faces of two, fair, six-sided dice. 1) What is the lower class boundary of the "6" class? 2) Find the mean. 3) Find the standard deviation. 4) Find the z-score for snake eyes.
Two players A and B play a game of dice . They roll a pair of...
Two players A and B play a game of dice . They roll a pair of dice alternately . The player who rolls 7 first wins . If A starts then find the probability of B winning the game ?
Two players A and B play a dice game with a 6-face fair dice. Player A...
Two players A and B play a dice game with a 6-face fair dice. Player A is only allowed to roll the dice once. Player B is allowed to roll the dice maximally twice (that is, Player B can decide whether or not she or he would roll the dice again after seeing the value of the first roll). If the player with the larger final value of the dice wins, what is the maximal probability for Player B to...
I am using C++ Write a program that allows two players to play a game of...
I am using C++ Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user...
PROGRAMING in C RPSLS: The program that we must implement must allow two players to play...
PROGRAMING in C RPSLS: The program that we must implement must allow two players to play in the same console (there will be only one program) The program will initialize the information necessary to be able to store the name of two players and their respective markers throughout the game, to present a result message at the end of the game. 1. When the program is ready for the game to start, it will ask PLAYER1 enter your name. 2....
Write a C++ program to score the paper-rock-scissor game. Each of two players (player1 and player2)...
Write a C++ program to score the paper-rock-scissor game. Each of two players (player1 and player2) input a character which could be either ‘P’, ‘R’, or ‘S’ (in uppercase or lowercase). For any other input character should display a message “Invalid input”. The program then announces who is the winner as well as the basis for determining the winner which could be one of the following: “Paper covers rock”, “Rock breaks scissors”, “Scissors cut paper”, or “Nobody wins”. (Use switch...
Write a C++ program to score the paper-rock-scissor game. Each of two players (player1 and player2)...
Write a C++ program to score the paper-rock-scissor game. Each of two players (player1 and player2) input a character which could be either ‘P’, ‘R’, or ‘S’ (in uppercase or lowercase). For any other input character should display a message “Invalid input”. The program then announces who is the winner as well as the basis for determining the winner which could be one of the following: “Paper covers rock”, “Rock breaks scissors”, “Scissors cut paper”, or “Nobody wins”. (Use switch...
Dice Game Rules: 2 - 4 players Each player has 5 Dice. The dice have 6...
Dice Game Rules: 2 - 4 players Each player has 5 Dice. The dice have 6 sides. Each player rolls their dice, and the dice statistics are reported: Sum, number of pairs (and of what), and "straights" - (all dice in order - e.g. 1,2,3,4,5 or 2,3,4,5,6) Player 1 might roll 2,2,3,4,4 so the results would be: Sum: 15, 1 pair (2), 1 pair (4) Player 2 might roll 1, 1, 4, 6, 6 so the results would be: Sum:...
1. Consider the following game. There are two piles of matches and two players. The game...
1. Consider the following game. There are two piles of matches and two players. The game starts with Player 1 and thereafter the players take turns. When it is a player's turn, she can remove any number of matches from either pile. Each player is required to remove some number of matches if either pile has matches remaining, and can only remove matches from one pile at a time. Whichever player removes the last match wins the game. Winning gives...
Write a C++ program to play the dice game "Craps". Craps is one of the most...
Write a C++ program to play the dice game "Craps". Craps is one of the most popular games of chance and is played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT