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
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 players total points is greater than M then the player losses half its points and loses its next turn. If the players total points equals any other players 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; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started