Create a C dice rolling game with 2 players rolling twice Th
Create a C++ dice rolling game, with 2 players rolling twice,
The program must have a random number funtion to choose a random number between 1 and 6.
And each number is associated with a value: 1 is 20, 2 is 30, 3 is 40, 4 is 50, 5 is 60, 6 is 70;
When the player one rolls the dice for the first roll, his result is stored in an array1, also when player two rolls the dice for the first roll, his result is stored in an array2,
Then a cout statement prints the results for the first round.
Again player 1 rolls the dice for the second round, and player 2 rolls the dice for the second round,
the result of player one second round is added to the array1, and the result of player 2 second round is added to array2,
The program now prints the sum of the results of player one and the sum of the results of player two,
At the end the program will have a function that checks which player has a higher score and will display the name of the winning player.
Sample output:
******************************************
Round 1:
Player 1 rolls a 2 with a value of 30
Player 2 rolls a 4 with a value of 50
After round 1, Player 1 score is 30, Player 2 score is 50.
Round 2:
Player 1 rolls a 6 with a value of 70
Player 2 rolls a 3 with a value of 40
After second roll, Player 1 total score is 100, and total score for Player 2 is 90
The winner is Player 1, since he has the highest score.
******************************************
Solution
#include <iostream>
#include <stdlib.h> /* we use this header file for srand, rand function */
#include <time.h> /* we use this header file for time */
using namespace std;
int randomInteger();
using namespace std;
int main()
{
srand (time(NULL));
int num1;
int num2;
int array1=0;
int array2=0;
cout<<\"******************************************\"<<endl;
/* for loop call randomInteger function 2 times*/
for(int i=0;i<2;i++)
{
// player 1 rolls the dice
num1=randomInteger();
//result is stored in an array1
array1=array1+ (num1+1)*10;
// player 2 rolls the dice
num2=randomInteger();
//result is stored in an array2
array2=array2+(num2+1)*10;
cout<<\"Round \"<<i+1<<endl;
cout<<\"Player 1 rolls a \"<<num1<<\" with a value of \"<<(num1+1)*10<<endl;
cout<<\"Player 2 rolls a \"<<num2<<\" with a value of \"<<(num2+1)*10<<endl;
cout<<\"After round \"<<i+1<<\", Player 1 score is \"<<array1<<\", Player 2 score is \"<<array2<<\".\"<<endl;
}
if(array1>array2)
cout<<\"The winner is Player 1, since he has the highest score.\"<<endl;
else
cout<<\"The winner is Player 2, since he has the highest score.\"<<endl;
cout<<\"******************************************\"<<endl;
return 0;
}
/*randomInteger function definition */
int randomInteger()
{
/* random number generation between 0 to 99*/
int rand_num=((rand() % 6)+1);
return rand_num;
}
=========================================================
output sample 1:-
******************************************
Round 1
Player 1 rolls a 4 with a value of 50
Player 2 rolls a 2 with a value of 30
After round 1, Player 1 score is 50, Player 2 score is 30.
Round 2
Player 1 rolls a 5 with a value of 60
Player 2 rolls a 4 with a value of 50
After round 2, Player 1 score is 110, Player 2 score is 80.
The winner is Player 1, since he has the highest score.
******************************************
output sample 2:-
******************************************
Round 1
Player 1 rolls a 1 with a value of 20
Player 2 rolls a 6 with a value of 70
After round 1, Player 1 score is 20, Player 2 score is 70.
Round 2
Player 1 rolls a 5 with a value of 60
Player 2 rolls a 6 with a value of 70
After round 2, Player 1 score is 80, Player 2 score is 140.
The winner is Player 2, since he has the highest score.
******************************************
output sample 3:-
******************************************
Round 1
Player 1 rolls a 4 with a value of 50
Player 2 rolls a 5 with a value of 60
After round 1, Player 1 score is 50, Player 2 score is 60.
Round 2
Player 1 rolls a 4 with a value of 50
Player 2 rolls a 6 with a value of 70
After round 2, Player 1 score is 100, Player 2 score is 130.
The winner is Player 2, since he has the highest score.
******************************************
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.


