Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Battle Dice is an 2 player game where 2 players each toss a die 3 times. At each

ID: 3710320 • 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.

#pragma once

#include <string>

using namespace std;

class Player

{

public:

//constructor

Player(const string& new_name = "No Name");

//accessors

string name() const;

int sums() const;

int wins() const;

//mutators

void name(const string& new_name) const;

//custom methods

void clear();

void addDie(int new_die);

void addWin();

private:

string _name;

int _sums;

int _wins;

};

#include "Player.h"

Player::Player(const string& new_name)

{

_name = new_name;

_sums = 0;

_wins = 0;

}

//accessors

string Player::name() const

{

return _name;

}

int Player::sums() const

{

return _sums;

}

int Player::wins() const

{

return _wins;

}

//custom methods

void Player::clear()

{

_sums = 0;

_wins = 0;

}

void Player::addDie(int new_die)

{

_sums += new_die;

}

void Player::addWin()

{

_wins++;

}

include array and player class

Explanation / Answer

Player.h has not been modified. It stays as defined in the problem. i.e. all the line before #include "Player.h'.

The code with inline comments for the game is here :

/******************************************************************************

BATTLE DICE GAME PROGRAM

*******************************************************************************/

#include <iostream>

#include <stdlib.h>

#include "Player.h"

using namespace std;

int main()

{

// Define two players - name them, we can get it from console

string p1_name;

cout << "What's player one's name? ";

getline (cin, p1_name);

  

string p2_name;

cout << "What's player two's name? ";

getline (cin, p2_name);

  

// Create objects for Player class

Player p1(p1_name);

Player p2(p2_name);

  

unsigned seed; /* number used to seed random number generator */

printf("Enter seed: ");

scanf("%u", &seed);

// this is to kick start the random number generator

srand(seed);

  

// string to let user choose if he wants to continue playing one more time or not

string to_continue = "y";

  

while(to_continue == "y"){

// index represents the toss

for(int index=0; index<3; index++){

// Use in built rand() function to generate a random number and modulo it by 6 to get

// a number in the range 0-5. For our dice purpose, add 1 so that range becomes 1-6

int toss_value_p1 = rand()%6 + 1;

int toss_value_p2 = rand()%6 + 1;

  

// print toss values for this iteration

cout << "Die value for " << p1.name() << " is " << toss_value_p1 << endl;

cout << "Die value for " << p2.name() << " is " << toss_value_p2 << endl;

  

// add obtained die value that adds to its sum   

p1.addDie(toss_value_p1);

p2.addDie(toss_value_p2);

  

// As mentioned in the requirement, die with greater value wins this toss

if(toss_value_p2 > toss_value_p1){

// Add win in the player and print that it wins this toss

p2.addWin();

cout << "Player " << p2.name() << " wins " << " toss number " << index+1 << endl;

}else if(toss_value_p1 > toss_value_p2){

p1.addWin();

cout << "Player " << p1.name() << " wins" << " toss number " << index+1 << endl;

}else{

cout << " Toss number " << " is a Tie " << index+1 << endl;

}

  

// Print sums at the end of each toss so that we can assess manually also

cout << "At the end of toss " << index+1 << " Player " << p1.name() << "'s sum is " << p1.sums() << endl;

cout << "At the end of toss " << index+1 << " Player " << p2.name() << "'s sum is " << p2.sums() << endl << endl;

  

}

  

// As per the requirement, more wins in the tosses makes the player win

if(p2.wins() > p1.wins()){

cout << "Player " << p2.name() << " wins " << " the game " << endl;

}else if(p1.wins() > p2.wins()){

cout << "Player " << p1.name() << " wins " << " the game " << endl;

}else{

// If wins are equal, then compare sums

if(p2.sums() > p1.sums()){

cout << "Player " << p2.name() << " wins " << " the game " << endl;

}else if(p1.sums() > p2.sums()){

cout << "Player " << p1.name() << " wins " << " the game " << endl;   

}else{

// If all are equal, the game is a tie

cout << " This Game is a Tie " << endl;

}

}

  

// clear the values for the next Game

p1.clear();

p2.clear();

  

cout << "press y if you want to continue playing" << endl;

cin >> to_continue;

}

return 0;

}

Player::Player(const string& new_name)

{

_name = new_name;

_sums = 0;

_wins = 0;

}

//accessors

string Player::name() const

{

return _name;

}

int Player::sums() const

{

return _sums;

}

int Player::wins() const

{

return _wins;

}

//custom methods

void Player::clear()

{

_sums = 0;

_wins = 0;

}

void Player::addDie(int new_die)

{

_sums += new_die;

}

void Player::addWin()

{

_wins++;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote