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

c++ Design and implement the following game: N players (who are alligators) take

ID: 3844649 • Letter: C

Question

c++

Design and implement the following game:

N players (who are alligators) take turns tossing a pair of dice to earn points. The player whose total points first equals M, some positive integer, wins. A player adds to its total points an amount equal to the sum of the dice it tossed. If the two dice are equal the player throws the dice again; that is, the player gets an extra turn. If the player’s total points is greater than M then the player losses half its points and loses its next turn. If the player’s total points equals any other player’s total points then that other player loses all its points and its next turn.

You will need either vectors or array lists to solve this problem.

below is the code given that need modification.

PLAYER.H

#pragma once
#include
#include
#include
using namespace std;

enum State {NORMAL, LOSTTURN, EXTRATURN};

class Player
{
public:
   //constructors
   Player(const string& new_name = "Nemo");

   //accessors
   string name() const;
   unsigned points() const;
   State state() const;

   //mutators
   void name(const string& new_name);
   void points(unsigned new_points);
   void state(State new_state);

   //custom methods
   void add(unsigned more_points);
   void roll(unsigned& die1, unsigned& die2);
private:
   string _name;
   unsigned _points;
   State _state;

   static default_random_engine e;
   static uniform_int_distribution u;
};

PLAYER.CPP

#include "Player.h"

default_random_engine Player::e(3);
uniform_int_distribution Player::u(1, 6);

//constructors
Player::Player(const string& new_name)
{
   name(new_name);
   points(0);
   state(NORMAL);
}

//mutators
void Player::name(const string& new_name)
{
   _name = new_name;
}

void Player::points(unsigned new_points)
{
   _points = new_points;
}

void Player::state(State new_state)
{
   _state = new_state;
}

//accessors
string Player::name() const
{
   return _name;
}

unsigned Player::points() const
{
   return _points;
}

State Player::state() const
{
   return _state;
}

//custom methods
void Player::roll(unsigned& die1, unsigned& die2)
{
   die1 = u(e);
   die2 = u(e);
}
void Player::add(unsigned more_points)
{
   _points += more_points;
}

SOURCE.CPP

#include
#include
#include
#include "Player.h"

using namespace std;

int main()
{
   unsigned M = 64;
   unsigned N = 4;

   vector players(N);
   unsigned id = 0;
   for(Player& p : players)
   {
       id++;
       p.name("p" + to_string(id));
   }
   /*
   for(Player& p : players)
       cout << p.name() << endl;
   */
   id = 0;
   do
   {
       id = (id + 1) % N;
       if(players[id].state() == State::LOSTTURN)
           players[id].state(State::NORMAL);
       else
       {
           unsigned die1, die2;
           players[id].roll(die1, die2);
           players[id].add(die1 + die2);
           unsigned points = players[id].points();
           if(points > M)
           {
               points = points / 2;
               players[id].points(points);
               players[id].state(State::LOSTTURN);
               cout << players[id].name() << " loses its next turn!" << endl;
           }
           cout << players[id].name() << " rolled " << die1 << " and " << die2 << endl;
           cout << players[id].name() << " has " << players[id].points() << " points " << endl;
       }
   }
   while(players[id].points() < M);
   cout << players[id].name() << " wins!" << endl;
   system("pause");
   return 0;
}

Explanation / Answer

Modifications to the given classes done..

#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h> /* srand, rand */
#include <time.h>

using namespace std;
enum State {NORMAL, LOSTTURN, EXTRATURN};
class player
{
public:
//constructors
player(const string& new_name = "Nemo");
//accessors
string name() const;
unsigned points() const;
State state() const;
//mutators
void name(const string& new_name);
void points(unsigned new_points);
void state(State new_state);
//custom methods
void add(unsigned more_points);
void roll(unsigned& die1, unsigned& die2);
private:
string _name;
unsigned int _points;
State _state;
static int e;
static int u;
};

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include "player.h"

//constructors
player::player(const string& new_name)
{
name(new_name);
points(0);
state(NORMAL);
}
//mutators
void player::name(const string& new_name)
{
_name = new_name;
}
void player::points(unsigned new_points)
{
_points = new_points;
}
void player::state(State new_state)
{
_state = new_state;
}
//accessors
string player::name() const
{
return _name;
}
unsigned player::points() const
{
return _points;
}
State player::state() const
{
return _state;
}
//custom methods
void player::roll(unsigned& die1, unsigned& die2)
{
//die1 = u(e);
//die2 = u(e);
}
void player::add(unsigned more_points)
{
_points += more_points;
}

------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h> /* srand, rand */
#include <time.h>
#include "player.h"

using namespace std;
int main()
{
unsigned M = 64;
unsigned N = 4;
vector<player> players;
unsigned id = 0;

   for(std::vector<int>::size_type i = 0; i != players.size(); i++) {
   id++;
players[i].name("p" + id);
   }
for(std::vector<int>::size_type i = 0; i != players.size(); i++) {
   cout << players[i].name() << endl;
   }

id = 0;
do
{
id = (id + 1) % N;
if(players[id].state() == State::LOSTTURN)
players[id].state(State::NORMAL);
else
{
unsigned die1, die2;
players[id].roll(die1, die2);
players[id].add(die1 + die2);
unsigned points = players[id].points();
if(points > M)
{
points = points / 2;
players[id].points(points);
players[id].state(State::LOSTTURN);
cout << players[id].name() << " loses its next turn!" << endl;
}
cout << players[id].name() << " rolled " << die1 << " and " << die2 << endl;
cout << players[id].name() << " has " << players[id].points() << " points " << endl;
}
}
while(players[id].points() < M);
cout << players[id].name() << " wins!" << endl;
system("pause");
return 0;
}

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