Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Shut The Box game has a serious flaw. If a player rolls 10, 11, or 12 the player loses his turn because there are

The Shut The Box game has a serious flaw. If a player rolls 10, 11, or 12 the player loses his turn because there are no boxes 10, 11, or 12. Modify the program so that if a player rolls 10, 11, or 12 then the player can try to shut the boxes corresponding to each die.For example, if the player rolls 4 and 6 and boxes 4 and 6 are open then the player shuts those boxes.

source file ---------------------------------

#include #include #include #include #include "Boxes.h" #include "Dice.h" #include "Player.h"

using namespace std;

int player_index; Boxes boxes; Dice dice; const int n_players = 4; vector players(n_players);

void play();

int main() { string names[] = { "Alana", "Bella", "Celia", "Donna" }; bool select_totals[] = { false, true, false, true };

default_random_engine e(static_cast(time(nullptr))); uniform_int_distribution u(0, n_players - 1); for (int index = 0; index < n_players; index++) { Player player(names[index], select_totals[index]); players[index] = player; } player_index = u(e); while(players.size() > 1 && boxes.openBoxNumbersTotal() > 6) play(); dice.nDice(1); while(players.size() > 1 && boxes.openBoxesCount() > 0) play();

cout << players[player_index].name() << " wins!" << endl;

system("pause"); return 0; } void play() { static const int exclusion_penalty_points = 45; vector shut_boxes = players[player_index].shutBoxes(boxes, dice); cout << players[player_index].name() << " rolled "; cout << dice << endl; if(shut_boxes.size() > 0) { cout << players[player_index].name() << " shut box(s) "; for (unsigned box_number : shut_boxes) cout << box_number << ' '; cout << endl; cout << boxes << endl; } else { unsigned more_penalty_points = boxes.openBoxNumbersTotal(); players[player_index].addPenaltyPoints(more_penalty_points); cout << players[player_index].name() << " suffers " << players[player_index].penaltyPoints() << " penalty points" << endl; if(players.size() > 1) { if (players[player_index].penaltyPoints() >= exclusion_penalty_points) { cout << players[player_index].name() << " leaves" << endl; players.erase(players.begin() + player_index); } player_index = (player_index + 1) % players.size(); } } }

player.cpp-------------------------------------------

#include #include "Boxes.h" #include "Dice.h" #include "Player.h"

//constructors Player::Player(const string& new_name, bool new_select_totals) { name(new_name); selectTotals(new_select_totals); _penalty_points = 0; } //accessors string Player::name() const { return _name; } unsigned Player::penaltyPoints() const { return _penalty_points; } bool Player::selectTotals() const { return _select_totals; } //mutators void Player::name(const string& new_name) { assert(new_name.length() > 0); _name = new_name; } void Player::selectTotals(bool new_select_totals) { _select_totals = new_select_totals; } //custom methods void Player::addPenaltyPoints(unsigned more_penalty_points) { assert(more_penalty_points >= 0); _penalty_points += more_penalty_points; } vector Player::shutBoxes(Boxes& boxes, Dice& dice) { dice.roll(); vector shut_boxes; vector faces = dice.faces(); unsigned total = dice.total(); if(boxes.isOpen(faces) && boxes.isOpen(total)) if (_select_totals) { boxes.shut(total); shut_boxes.push_back(total); } else { boxes.shut(dice.faces()); shut_boxes = dice.faces(); } return shut_boxes; }

dice.cpp--------------------

#include "Dice.h" #include #include using namespace std;

//io operators ostream& operator<<(ostream& outs, const Dice& dice) { for(const int face : dice._faces) outs << face << ' '; return outs; }

//constructors Dice::Dice(unsigned n_dice) :_faces(n_dice), u(1, n_faces) { for(unsigned index = 0; index < _faces.size(); index++) _faces[index] = 0; } //accessors unsigned Dice::nDice() const { return _faces.size(); } vector Dice::faces() const { return _faces; } //mutators void Dice::nDice(unsigned n_dice) { _faces.resize(n_dice); for(unsigned index = 0; index < _faces.size(); index++) _faces[index] = 0; } //predicates unsigned Dice::total() const { unsigned faces_total = 0; for(unsigned index = 0; index < _faces.size(); index++) faces_total += _faces[index]; return faces_total; } //custom methods void Dice::roll() { for(unsigned index = 0; index < _faces.size(); index++) _faces[index] = u(e); }

boxes.cpp----

#include #include "Boxes.h" using namespace std;

//operators ostream& operator<<(ostream& sink, const Boxes& boxes) { for(unsigned box_number = 1; box_number <= boxes.nBoxes(); box_number++) sink << box_number << ' '; sink << endl; for(unsigned box_number = 1; box_number <= boxes.nBoxes(); box_number++) if(boxes.isOpen(box_number)) sink << "O "; else sink << "X "; sink << endl; return sink; } //constructors Boxes::Boxes(unsigned new_size) :_state(new_size) { open(); } //custom methods bool Boxes::open(unsigned box_number) { bool good_box_number = 1 <= box_number && box_number <= _state.size(); if(good_box_number) _state[box_number - 1] = true; return good_box_number; } void Boxes::open() { for(unsigned box_number = 1; box_number <= _state.size(); box_number++) open(box_number); } bool Boxes::shut(unsigned box_number) { bool good_box_number = 1 <= box_number && box_number <= _state.size(); if(good_box_number) _state[box_number - 1] = false; return good_box_number; } void Boxes::shut() { for(unsigned box_number = 1; box_number <= _state.size(); box_number++) shut(box_number); } bool Boxes::shut(const vector& box_numbers) { for(unsigned box_numbers_index = 0; box_numbers_index < box_numbers.size(); box_numbers_index++) if(!shut(box_numbers[box_numbers_index])) return false; return true; } vector Boxes::openBoxNumbers() const { unsigned n_open_boxes = openBoxesCount(); vector open_box_numbers(n_open_boxes); for(unsigned box_number = 1; box_number <= _state.size(); box_number++) if(isOpen(box_number)) open_box_numbers.push_back(box_number); return open_box_numbers; } unsigned Boxes::openBoxNumbersTotal() const { unsigned box_numbers_total = 0; for(unsigned box_number = 1; box_number <= _state.size(); box_number++) if(isOpen(box_number)) box_numbers_total += box_number; return box_numbers_total; } unsigned Boxes::openBoxesCount() const { int open_boxes_count = 0; for(unsigned box_number = 1; box_number <= _state.size(); box_number++) if(isOpen(box_number)) open_boxes_count++; return open_boxes_count; } //predicates bool Boxes::isOpen(const vector& box_numbers) const { for(unsigned box_numbers_index = 0; box_numbers_index < box_numbers.size(); box_numbers_index++) if(!isOpen(box_numbers[box_numbers_index])) return false; return true; } bool Boxes::isOpen(unsigned box_number) const { return 1 <= box_number && box_number <= _state.size() && _state[box_number - 1] == true; } bool Boxes::isShut(unsigned box_number) const { return 1 <= box_number && box_number <= _state.size() && _state[box_number - 1] == false; } //accessors unsigned Boxes::nBoxes() const { return _state.size(); }

player.h----------------------------------------------

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

class Player { public: //constructor Player(const string& new_name = "no name", bool new_select_totals = true); //accessors string name() const; unsigned penaltyPoints() const; bool selectTotals() const; //mutators void name(const string& new_name); void selectTotals(bool new_select_totals); //custom methods void addPenaltyPoints(unsigned more_penalty_points); vector shutBoxes(Boxes& boxes, Dice& dice); private: string _name; unsigned _penalty_points; bool _select_totals; };

dice.h---------------

#pragma once #include #include using namespace std;

class Dice { friend ostream& operator<<(ostream& outs, const Dice& dice); public: //constructors Dice(unsigned dice = 2); //accessors vector faces() const; unsigned nDice() const; //mutators void nDice(unsigned new_n_dice); //custom methods void roll(); unsigned total() const; private: static const unsigned n_faces = 6; default_random_engine e; uniform_int_distribution u; vector _faces; };

boxes.h----------------------------------------------------------

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

class Boxes { friend ostream& operator<<(ostream& sink, const Boxes& boxes); public: //constructor Boxes(unsigned new_size = 9); //custom methods void open(); bool open(unsigned box_number); void shut(); bool shut(unsigned box_number); bool shut(const vector& box_numbers); vector openBoxNumbers() const; unsigned openBoxNumbersTotal() const; unsigned openBoxesCount() const; //predicates bool isOpen(unsigned box_number) const; bool isOpen(const vector& box_numbers) const; bool isShut(unsigned box_number) const; //accessors unsigned nBoxes() const; private: vector _state; };

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions