Battle Dice is an 2 player game where 2 players each toss a die 3 times. At each
ID: 3709952 • Letter: B
Question
Battle Dice is an 2 player game where 2 players each toss a die 3 times. At each toss, the player whose die shows the larger number wins the toss. If the numbers are equal then no one wins the toss - the toss is tied. The player who wins the greater number of tosses wins the game. if no player wins the greater number of tosses then the player whose die numbers' sum is larger wins the game. If the die numbers' sums are equal then the game is tied. Write a program that simulates one game of Battle Dice. Use arrays and use the Player class in your solution.
Use arrays and use the Player class in your solution.
Explanation / Answer
Answer :
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
int p1dice[3], sum1 = 0;
int p2dice[3], sum2 = 0;
int i=0;
int score = 0;
srand(time(0));
cout << "Press enter to begin The Game BATTLE DICE";
cin.get();
cout << endl;
for(; i<3; i++){
p1dice[i] = rand() % 6 + 1;
cout << i+1 << " role of player 1: " << p1dice[i] << endl;
sum1 += p1dice[i];
p2dice[i] = rand() % 6 + 1;
cout << i+1 << " role of player 2: " << p2dice[i]<< endl;
sum2 += p2dice[i];
if(p1dice[i] > p2dice[i]){
score += 1;
cout << "Player 1 wins this toss." << endl << endl;
}else if(p1dice[i] < p2dice[i]){ // player 2 wins
score -= 1;
cout << "Player 2 wins this toss." << endl << endl;
}else{ // its a tie.
cout << "It's a tie." << endl << endl;
}
}
if(score > 0){
cout << "Player 1 wins the game by tosses." << endl;
}else if(score < 0){
cout << "Player 2 wins the game by tosses." << endl;
}else{
if(sum1 > sum2){
cout << "Player 1 wins the game by sum of tosses." << endl;
}else if(sum1 < sum2){
cout << "Player 2 wins the game by sum of tosses." << endl;
}else{
cout << "The game is TIED." << endl;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.