Answer To: CS 115: Assignment 5 Assignment 5 Due March 31, 2021 Overview In this assignment, you will...
Kamal answered on Mar 30 2021
Dice.h
//
// Dice.h
//
// A module to handle dice rolling.
//
#pragma once
const int DICE_SIDE_COUNT = 4;
void diceInit ();
int diceRoll ();
void dicePrint2and2 (int r1, int r2, int c1, int c2);
void dicePrint2and2and1 (int r1, int r2, int c1, int c2, int e1);
Game.cpp
Game.cpp
//
// Game.cpp
//
#include
#include
#include
#include "Player.h"
#include "BoardSize.h"
#include "CellId.h"
#include "Tile.h"
#include "Board.h"
#include "AvailableTiles.h"
#include "Dice.h"
#include "CellChooser.h"
#include "Game.h"
using namespace std;
Game :: Game ()
: board(),
available()
{
}
void Game :: printState (unsigned int whose_turn) const
{
assert(whose_turn < playerGetCount());
board.print();
cout << endl;
available.print();
cout << endl;
cout << playerGetName(whose_turn) << "'s turn:" << endl;
cout << endl;
}
void Game :: handleDiceRoll (unsigned int whose_turn)
{
assert(whose_turn < playerGetCount());
bool is_extra_die = false;
if(playerHasDice(whose_turn, 1))
{
is_extra_die = true;
playerDecreaseDice(whose_turn, 1);
}
int row1 = diceRoll();
int row2 = diceRoll();
int column1 = diceRoll();
int column2 = diceRoll();
int extra = diceRoll();
if(is_extra_die)
dicePrint2and2and1(row1, row2, column1, column2, extra);
else
dicePrint2and2(row1, row2, column1, column2);
cout << endl; // blank line
// ask player to choose a cell if needed
CellChooser cell_chooser(row1, row2, column1, column2, extra, is_extra_die);
if(!cell_chooser.isChosen())
{
cell_chooser.printAvailable();
while(!cell_chooser.isChosen())
{
cout << "Choose a cell to roll: ";
string input;
getline(cin, input);
if(isOnBoard(input))
{
CellId chosen = toCellId(input);
if(cell_chooser.isAvailable(chosen))
cell_chooser.chooseAvailable(chosen);
else
cout << " Cell " << chosen << " is not available" << endl;
}
else
cout << " Invalid cell" << endl;
}
}
else
cout << "Rolled cell: " << cell_chooser.getChosen() << endl;
CellId chosen_cell = cell_chooser.getChosen();
assert(isOnBoard(chosen_cell));
Tile tile = board.getAt(chosen_cell);
tile.activate(whose_turn);
}
bool Game :: puchaseTile (unsigned int whose_turn)
{
assert(whose_turn < playerGetCount());
playerPrint(whose_turn);
string input;
cout << "Choose a cell to place a tile: ";
getline(cin, input);
if(input == "q")
return true; // player wants to quit
bool is_discard_tile = true;
if(isOnBoard(input))
{
CellId chosen_cell = toCellId(input);
assert(isOnBoard(chosen_cell));
if(!board.getAt(chosen_cell).isOwner())
{
int tile_index;
cout << "Choose a tile to buy (by index): ";
cin >> tile_index;
cin.clear(); // clean up bad input
getline(cin, input); // read to end of line
if(tile_index >= 0 && tile_index < AVAILABLE_TILE_COUNT)
{
int cost = available.getCost(tile_index);
if(playerHasMoney(whose_turn, cost))
{
board.setAt(chosen_cell, available.getTile(tile_index), whose_turn);
playerDecreaseMoney(whose_turn, cost);
available.replaceAt(tile_index);
is_discard_tile = false;
cout << "Success: Tile added" << endl;
}
else
cout << "Failure: Not enough money" << endl;
}
else
cout << "Failure: Invalid tile" << endl;
}
else
cout << "Failure: Cell already has an owner" << endl;
}
else
cout << "Failure: Invalid cell" << endl;
if(is_discard_tile)
available.replaceAt(0);
return false; // player does not want to quit
}
Game.h
//
// Game.h
//
// A module to represent the complete state of the game.
//
#pragma once
#include
#include "BoardSize.h"
#include "CellId.h"
#include "Tile.h"
#include "Board.h"
#include "AvailableTiles.h"
//
// Game
//
// A class to represent the complete state of the game.
//
class Game
{
public:
Game ();
void printState (unsigned int whose_turn) const;
void handleDiceRoll (unsigned int whose_turn);
bool puchaseTile (unsigned int whose_turn);
private:
Board board;
AvailableTiles available;
};
Main.cpp
Main.cpp
//
// Main.cpp
//
// The main program for Assignment 4.
// For Dr. Hamilton's CS 115 class, 202110 (Winter) term.
//
// Name: ___________________
// Student number: _________
//
#include
#include
#include
#include "Dice.h"
#include "Player.h"
#include "Game.h"
using namespace std;
const int POINTS_TO_WIN = 5;
int main ()
{
// setup
diceInit();
string player_names[2] = { "Alice", "Bob" };
playerInit(2, player_names);
Game game;
cout << "Welcome to the game." << endl;
// run main loop
unsigned int current_player = 0;
bool is_quit = false;
do
{
cout << endl;
cout << endl;
assert(current_player < playerGetCount());
game.printState(current_player);
game.handleDiceRoll(current_player);
cout << endl;
is_quit = game.puchaseTile(current_player);
current_player++;
if(current_player >= playerGetCount())
current_player = 0;
}
while (!is_quit && !playerHasPointsAnyone(POINTS_TO_WIN));
// print end messages
cout << endl;
cout << endl;
for(unsigned int p = 0; p < playerGetCount(); p++)
{
playerPrint(p);
}
cout << endl;
cout << "Thank you for playing!" << endl;
return 0; // program exited without crashing
}
Player.cpp
Player.cpp
//
// Player.cpp
//
#include
#include
#include
#include
#include "Player.h"
using namespace std;
const unsigned int START_MONEY = 0;
const unsigned int START_DICE = 1;
const unsigned int START_POINTS = 0;
struct Player
{
string name;
int money;
int dice;
int points;
};
static Player players[PLAYER_COUNT_MAX];
static unsigned int player_count = 0;
static bool isInvariantTrue ()
{
if(player_count > PLAYER_COUNT_MAX)
return false;
for(unsigned int i = 0; i < player_count; i++)
if(players[i].name == "")
return false;
return true;
}
void playerInit (unsigned int player_count1,
const string names[])
{
assert(isInvariantTrue());
assert(player_count1 >= PLAYER_COUNT_MIN);
assert(player_count1 <= PLAYER_COUNT_MAX);
for(unsigned int i = 0; i < player_count1; i++)
assert(names[i] != "");
player_count = player_count1;
for(unsigned int i = 0; i < player_count; i++)
{
players[i].name = names[i];
players[i].money = START_MONEY;
players[i].dice = START_DICE;
players[i].points = START_POINTS;
}
assert(isInvariantTrue());
}
unsigned int isInitialized ()
{
assert(isInvariantTrue());
if(player_count >= PLAYER_COUNT_MIN &&
player_count <= PLAYER_COUNT_MAX)
{
return true;
}
else
return false;
}
unsigned int playerGetCount ()
{
assert(isInvariantTrue());
assert(isInitialized());
return player_count;
}
unsigned int playerGetIndex (const string& name)
{
assert(isInvariantTrue());
assert(isInitialized());
assert(name != "");
for(unsigned int i = 0; i < player_count; i++)
{
if(players[i].name == name)
return i;
}
// if we get here, there was no player with name name
return NO_SUCH_PLAYER;
}
void playerPrint (unsigned int player_index)
{
assert(isInvariantTrue());
assert(isInitialized());
assert(player_index < playerGetCount());
cout<< setw(8) << left << players[player_index].name
<< "$" << setw(4) << left << players[player_index].money
<< "#" << setw(4) << left << players[player_index].dice
<< "*" << setw(4) << left << players[player_index].points << endl;
}
const string& playerGetName (unsigned int player_index)
{
assert(isInvariantTrue());
assert(isInitialized());
assert(player_index < playerGetCount());
return players[player_index].name;
}
char playerGetTileChar (unsigned int player_index)
{
assert(isInvariantTrue());
assert(isInitialized());
assert(player_index < playerGetCount());
assert(players[player_index].name.size() > 0);
return players[player_index].name[0];
}
bool playerHasMoney (unsigned int player_index,
int money)
{
assert(isInvariantTrue());
assert(isInitialized());
assert(player_index < playerGetCount());
assert(money >= 0);
if(players[player_index].money >= money)
return true;
else
return false;
}
bool playerHasDice (unsigned int player_index,
int dice)
{
assert(isInvariantTrue());
assert(isInitialized());
assert(player_index < playerGetCount());
assert(dice >= 0);
if(players[player_index].dice >= dice)
return true;
else
return false;
}
bool playerHasPointsAnyone (int points)
{
assert(isInvariantTrue());
assert(isInitialized());
assert(points >= 0);
for(unsigned int i = 0; i < player_count; i++)
{
if(players[i].points >= points)
return true;
}
return false;
}
void playerIncreaseMoneyAndPrint (unsigned int player_index,
int increase)
{
assert(isInvariantTrue());
assert(isInitialized());
assert(player_index < playerGetCount());
assert(increase >= 0);
int old_money = players[player_index].money;
players[player_index].money += increase;
int new_money = players[player_index].money;
cout << players[player_index].name << "'s money:"
<< " $" << old_money
<< " + $" << increase
<< " = $" << new_money << endl;
assert(isInvariantTrue());
}
void playerIncreaseDiceAndPrint (unsigned int player_index,
int increase)
{
assert(isInvariantTrue());
assert(isInitialized());
assert(player_index < playerGetCount());
assert(increase >= 0);
int old_dice = players[player_index].dice;
players[player_index].dice += increase;
int new_dice = players[player_index].dice;
cout << players[player_index].name << "'s dice:"
<< " #" << old_dice
<< " + #" << increase
<< " = #" << new_dice << endl;
assert(isInvariantTrue());
}
void playerIncreasePointsAndPrint (unsigned int player_index,
int increase)
{
assert(isInvariantTrue());
assert(isInitialized());
assert(player_index < playerGetCount());
assert(increase >= 0);
int old_points = players[player_index].points;
players[player_index].points += increase;
int new_points = players[player_index].points;
cout << players[player_index].name << "'s points:"
<< " *" << old_points
<< " + *" << increase
<< " = *" << new_points << endl;
assert(isInvariantTrue());
}
void playerDecreaseMoney (unsigned int player_index,
int decrease)
{
assert(isInvariantTrue());
assert(isInitialized());
assert(player_index < playerGetCount());
assert(decrease >= 0);
assert(playerHasMoney(player_index, decrease));
assert(players[player_index].money >= decrease);
players[player_index].money -= decrease;
assert(isInvariantTrue());
}
void playerDecreaseDice (unsigned int player_index,
int decrease)
{
assert(isInvariantTrue());
assert(isInitialized());
assert(player_index < playerGetCount());
assert(decrease >= 0);
assert(playerHasDice(player_index, decrease));
assert(players[player_index].dice >= decrease);
players[player_index].dice -= decrease;
assert(isInvariantTrue());
}
Player.h
//
// Player.h
//
// A module to keep track of the information for the players.
//
// Each player has a name and an amount of money, dice, and
// points. The amount of money and the number of dice can be
// increased and decreased, while the number of points can
// only be increased.
//
// This is an encapsulated global service.
//
#pragma once
#include
//
// PLAYER_COUNT_MIN
//
// The minimum number of players in the game.
//
const unsigned int PLAYER_COUNT_MIN = 1;
//
// PLAYER_COUNT_MAX
//
// The maximum number of players in the game.
//
const unsigned int PLAYER_COUNT_MAX = 4;
//
// NO_SUCH_PLAYER
//
// A special value indicating that there was not a player with
// the requested name.
//
const unsigned int NO_SUCH_PLAYER = 999999;
//
// playerInit
//
// Purpose: To initialize the Player module for the specified
// players. Each player starts with $0 money, #1 die,
// and *0 points.
// Parameter(s):
//<1> count: The number of players in the game
//<2> names: The player names
// Precondition(s):
//<1> count >= PLAYER_COUNT_MIN
//<2> count < PLAYER_COUNT_MAX
//<3> names[i] != "" WHERE 0 <= i < count
// Returns: N/A
// Side Effect: The players module is initialized to contain
// count players with the first count names in
// names. If the module was already initialized,
// all infromation about the previous players is
// lost.
//
void playerInit (unsigned int count,
const std::string names[]);
//
// isInitialized
//
// Purpose: To determine whether the player module has been
// initialized.
// Parameter(s): N/A
// Precondition(s): N/A
// Returns: Whether the player module is initialized.
// Side Effect: N/A
//
unsigned int isInitialized ();
//
// playerGetCount
//
// Purpose: To determine the number of players in the game.
// Parameter(s): N/A
// Precondition(s):
//<1> isInitialized()
// Returns: How many players in the game.
// Side Effect: N/A
//
unsigned int playerGetCount ();
//
// playerGetIndex
//
// Purpose: To determine the index of the player with the
// specified name.
// Parameter(s):
//<1> name: The player name
// Precondition(s):
//<1> isInitialized()
//<2> name != ""
// Returns: The index of the player with name name. If there
// isn't a player with that name, NO_SUCH_PLAYER is
// returned.
// Side Effect: N/A
//
unsigned int playerGetIndex (const std::string& name);
//
// playerPrint
//
// Purpose: To print the information for the specified player.
// Parameter(s):
//<1> player_index: Which player
// Precondition(s):
//<1> isInitialized()
//<2> player_index < playerGetCount()
// Returns: N/A
// Side Effect: The name, money, dice, and points for player
// player_index is printed to standard output
// (cout).
//
void playerPrint (unsigned int player_index);
//
// playerGetName
//
// Purpose: To determine the specified player's name.
// Parameter(s):
//<1> player_index: Which player
// Precondition(s):
//<1> isInitialized()
//<2> player_index < playerGetCount()
// Returns: The name of player player_index.
// Side Effect: N/A
//
const std::string& playerGetName (unsigned int who);
//
// playerGetTileChar
//
// Purpose: To determine the character that the specified
// player uses to mark tile ownership.
// Parameter(s):
//<1> player_index: Which player
// Precondition(s):
//<1> isInitialized()
//<2> player_index < playerGetCount()
// Returns: The char used to show that player player_index owns
// a tile.
// Side Effect: N/A
//
char playerGetTileChar (unsigned int player_index);
//
// playerHasMoney
//
// Purpose: To determine if the specified player has at least
// the specified amount of money.
// Parameter(s):
//<1> player_index: Which player
//<2> money: How much money to check for
// Precondition(s):
//<1> isInitialized()
//<2> player_index < playerGetCount()
//<3> money >= 0
// Returns: Whether player player_index has at least money
// money.
// Side Effect: N/A
//
bool playerHasMoney (unsigned int player_index,
int money);
//
// playerHasDice
//
// Purpose: To determine if the specified player has at least
// the specified number of dice.
// Parameter(s):
//<1> player_index: Which player
//<2> dice: How many dice to check for
// Precondition(s):
//<1> isInitialized()
//<2> player_index < playerGetCount()
//<3> dice >= 0
// Returns: Whether player player_index has at least dice dice.
// Side Effect: N/A
//
bool playerHasDice (unsigned int player_index,
int dice);
//
// playerHasPointsAny
//
// Purpose: To determine if any player has at least the
// specified number of points.
// Parameter(s):
//<1> points: How many points to check for
// Precondition(s):
//<1> isInitialized()
//<2> points >= 0
// Returns: Whether any player has at least points points.
// Side Effect: N/A
//
bool playerHasPointsAnyone (int points);
//
// playerIncreaseMoneyAndPrint
//
// Purpose: To give the specified player the specified amount
// of additional money.
// Parameter(s):
//<1> player_index: Which player
//<2> increase: How much money to give the player
// Precondition(s):
//<1> isInitialized()
//<2> player_index < playerGetCount()
//<3> increase >= 0
// Returns: N/A
// Side Effect: Player player_index gains increase money. The
// change to the player's money is printed,
// including the player name.
//
void playerIncreaseMoneyAndPrint (unsigned int player_index,
int increase);
//
// playerIncreaseDiceAndPrint
//
// Purpose: To give the specified player the specified number
// of additional dice.
// Parameter(s):
//<1> player_index: Which player
//<2> increase: How many dice to give the player
// Precondition(s):
//<1> isInitialized()
//<2> player_index < playerGetCount()
//<3> increase >= 0
// Returns: N/A
// Side Effect: Player player_index gains increase dice. The
// change to the player's number of dice is
// printed, including the player name.
//
void playerIncreaseDiceAndPrint (unsigned int player_index,
int increase);
//
// playerIncreasePointsAndPrint
//
// Purpose: To give the specified player the specified number
// of additional points.
// Parameter(s):
//<1> player_index: Which player
//<2> increase: How many points to give the player
// Precondition(s):
//<1> isInitialized()
//<2> player_index < playerGetCount()
//<3> increase >= 0
// Returns: N/A
// Side Effect: Player player_index gains increase points. The
// change to the player's points is printed,
// including the player name.
//
void playerIncreasePointsAndPrint (unsigned int player_index,
int increase);
//
// playerDecreaseMoney
//
// Purpose: To reduce the specified player's money by the
// specified amount.
// Parameter(s):
//<1> player_index: Which player
//<2> decrease: How much money to take from the player
// Precondition(s):
//<1> isInitialized()
//<2> player_index < playerGetCount()
//<3> decrease >= 0
//<4> playerHasdDice(player_index, decrease)
// Returns: N/A
// Side Effect: Player player_index loses decrease money.
//
void playerDecreaseMoney (unsigned int player_index,
int decrease);
//
// playerDecreaseDice
//
// Purpose: To reduce the specified player's number of dice by
// the specified amount.
// Parameter(s):
//<1> player_index: Which player
//<2> decrease: How many dice to take from the player
// Precondition(s):
//<1> isInitialized()
//<2> player_index < playerGetCount()
//<3> decrease >= 0
//<4> playerHasdDice(player_index, decrease)
// Returns: N/A
// Side Effect: Player player_index loses decrease dice.
//
void playerDecreaseDice (unsigned int player_index,
int...