Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//************************************** // Board.h // #ifndef BOARD_H #define BOARD_H // Position // A data type to represent a position on a Tic Tac Toe board. typedef

image text in transcribedimage text in transcribedimage text in transcribed

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

// Board.h

//

#ifndef BOARD_H

#define BOARD_H

// Position

// A data type to represent a position on a Tic Tac Toe board.

typedef unsigned int Position;

//

// Board

//

// A class to represent the game board in a Tic Tac Toe game.

// It is possible to query and set the symbols in the cells

// of a Board. It is also possible to print a Board to

// standard output and to determine the current game state:

// which player, if any, has won.

///

// Class Invariant:

// Symbol::isValid(ma_cells[i]) FOR 0

//

class Board

{

public:

//

// SIZE

//

// The number of rows and columns in this Board.

static const unsigned int SIZE = 3;

//

// CELL_COUNT

//

// The number cells in this Board.

//

static const unsigned int CELL_COUNT = SIZE * SIZE;

public:

//

// Default Constructor

//

// Purpose: To create a new, empty Board.

// Parameter(s): N/A

// Precondition(s): N/A

// Returns: N/A

// Side Effect: A new Board is created.

//

Board ();

//

// Copy Constructor

//

// Purpose: To create a new Board with the same symbols marked

// on it as another Board.

// Parameter(s):

// original: The Board to copy

// Precondition(s): N/A

// Returns: N/A

// Side Effect: A new Board is created. Each cell is set to

// have the same value as the corresponding cell

// in original.

//

Board (const Board& original);

//

// Destructor

//

// Purpose: To safely deallocate this Board without memory

// leaks.

// Parameter(s): N/A

// Precondition(s): N/A

// Returns: N/A

// Side Effect: All dynamically allocated memory is freed.

//

~Board ();

//

// Assignment Operator

//

// Purpose: To modify this Board to have the same symbols

// marked on it as another Board.

// Parameter(s):

// original: The Board to copy

// Precondition(s): N/A

// Returns: A reference to this Board.

// Side Effect: Each cell in this Board is set to have the same

// value as the corresponding cell in original.

//

Board& operator= (const Board& original);

//

// isValidPosition

//

// Purpose: To determine if the specified Position is valid for

// this Board.

// Parameter(s):

// position: The position

// Predcondition(s): N/A

// Returns: Whether Position position is valid for this Board.

// Side Effect:

//

inline bool isValidPosition (const Position& position) const;

//

// toPosition

//

// Purpose: To convert a pair of row/column coordinates a

// Position.

// Parameter(s):

// row: The row

// column: The column

// Predcondition(s):

// row

// column

// Returns: The Position for the cell at row row and column

// column.

// Side Effect:

//

inline Position toPosition (unsigned int row,

unsigned int column) const;

//

// getRowForPosition

// getColumnForPosition

//

// Purpose: To determine the row/column that a Position refers

// to.

// Parameter(s):

// position: The Position

// Predcondition(s):

// isValidPosition(position)

// Returns: The row/column for Position position.

// Side Effect:

//

inline unsigned int getRowForPosition (

const Position& position) const;

inline unsigned int getColoumForPosition (

const Position& position) const;

//

// getCellSymbol

//

// Purpose: To determine the symbol in the cell at the

// specified Position.

// Parameter(s):

// position: The position of the cell

// Precondition(s):

// isValidPosition(position)

// Returns: The symbol at position position.

// Side Effect: N/A

//

char getCellSymbol (const Position& position) const;

//

// getGameState

//

// Purpose: To determine the winner of the game.

// Parameter(s): N/A

// Precondition(s): N/A

// Returns: GameState::X_WINS if player X has won

// GameState::O_WINS if player O has won

// GameState::TIE if the game is a tie

// GameState::UNFINISHED otherwise

// Side Effect: N/A

//

unsigned int getGameState () const;

//

// print

//

// Purpose: To print this Boiard to standard output.

// Parameter(s): N/A

// Precondition(s): N/A

// Returns: N/A

// Side Effect: This Board is printed to standard output.

//

void print () const;

//

// setCellSymbol

//

// Purpose: To change the symbol in the cell at the specified

// Position.

// Parameter(s):

// position: The position of the cell

// symbol: The new symbol

// Precondition(s):

// isValidPosition(position)

// Symbol::isValid(symbol)

// Returns: N/A

// Side Effect: The cell at position position in the Board is

// set to contain symbol symbol.

//

void setCellSymbol (const Position& position,

char symbol);

private:

//

// copy

//

// Purpose: copy the symbols from another Board to this Board.

// Parameter(s):

// original: The Board to copy

// Precondition(s): N/A

// Returns: N/A

// Side Effect: This Board is set to contain the same symbols

// as original. The existing symbols are lost.

//

void copy (const Board& original);

//

// invariant

//

// Purpose: To determine if the class invariant is true.

// Parameter(s): N/A

// Precondition(s): N/A

// Returns: Whether the class invariant is true

// Side Effect: N/A

//

bool invariant () const;

private:

char ma_cells[CELL_COUNT];

};

#include "Board.inl"

#endif

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

// Player.h

//

#ifndef PLAYER_H

#define PLAYER_H

class Board;

//

// Player

//

// An abstract superclass for the classes that decide which

// move to make in a Tic Tac Toe game.

//

// Class Invariant:

// Symbol::isValid(m_symbol)

//

class Player

{

public:

//

// Default Constructor

//

// Purpose: To create a new Player with default values.

// Parameter(s): N/A

// Precondition(s): N/A

// Returns: N/A

// Side Effect: A new Player is created that places symbol

// Symbol::X on the game board.

//

Player ();

//

// Constructor

//

// Purpose: To create a new Player that places the specified

// symbol on the game board.

// Parameter(s):

// symbol: The symbol

// Precondition(s):

// Symbol::isValid(symbol)

// Returns: N/A

// Side Effect: A new Player is created that places symbol

// symbol on the game board.

//

Player (char symbol);

//

// Copy Constructor

//

// Purpose: To create a new Player as a copy of another.

// Parameter(s):

// original: The Player to copy

// Precondition(s): N/A

// Returns: N/A

// Side Effect: A new Player is created that places the same

// symbol on the game board as original.

//

Player (const Player& original);

//

// Destructor

//

// Purpose: To safely destroy a Player without memory leaks.

// Parameter(s): N/A

// Precondition(s): N/A

// Returns: N/A

// Side Effect: All dynamically allocated memory is freed.

//

virtual ~Player ();

//

// Assignment Operator

//

// Purpose: To modify this Player to be a copy of another.

// Parameter(s):

// original: The Player to copy

// Precondition(s): N/A

// Returns: A reference to this Player.

// Side Effect: This Player is set to place the same symbol on

// the game board as original.

//

Player& operator= (const Player& original);

//

// getSymbol

//

// Purpose: To determine the symbol placed by this Player.

// Parameter(s): N/A

// Precondition(s): N/A

// Returns: The symbol placed by this Player.

// Side Effect: N/A

//

char getSymbol () const;

//

// getMove

//

// Purpose: To determine the next move for this Player on the

// specified Board.

// Parameter(s):

// board: The Board

// Precondition(s):

// board.getGameState() == GameState::UNFINISHED

// Returns: The next move for this Player on Board board.

// Side Effect: N/A

//

virtual unsigned int getMove (

const Board& board) const = 0;

//

// setSymbol

//

// Purpose: To change the symbol placed by this Player.

// Parameter(s):

// symbol: The new symbol

// Precondition(s):

// Symbol::isValid(symbol)

// Returns: N/A

// Side Effect: This Player is set to place symbol symbol in

// the game board.

//

void setSymbol (char symbol);

private:

//

// invariant

//

// Purpose: To determine if the class invariant is true.

// Parameter(s): N/A

// Precondition(s): N/A

// Returns: Whether the class invariant is true.

// Side Effect: N/A

//

bool invariant () const;

private:

char m_symbol;

};

#endif

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

// PlayerHuman.h

//

#ifndef PLAYER_HUMAN_H

#define PLAYER_HUMAN_H

#include "Player.h"

//

// PlayerHuman

//

// A Player that asks the user for moves.

//

class PlayerHuman : public Player{

public:

//

// Default Constructor

//

// Purpose: To create a new PlayerHuman with default values.

// Parameter(s): N/A

// Precondition(s): N/A

// Returns: N/A

// Side Effect: A new PlayerHuman is created that places symbol

// Symbol::X on the game board.

//

PlayerHuman ();

//

// Constructor

//

// Purpose: To create a new PlayerHuman that places the

// specified symbol on the game board.

// Parameter(s):

// symbol: The symbol

// Precondition(s):

// Symbol::isValid(symbol)

// Returns: N/A

// Side Effect: A new PlayerHuman is created that places symbol

// symbol on the game board.

//

PlayerHuman (char symbol);

//

// Copy Constructor

//

// Purpose: To create a new PlayerHuman as a copy of another.

// Parameter(s):

// original: The PlayerHuman to copy

// Precondition(s): N/A

// Returns: N/A

// Side Effect: A new PlayerHuman is created that places the

// same symbol on the game board as original.

//

PlayerHuman (const PlayerHuman& original);

//

// Destructor

//

// Purpose: To safely destroy a PlayerHuman without memory

// leaks.

// Parameter(s): N/A

// Precondition(s): N/A

// Returns: N/A

// Side Effect: All dynamically allocated memory is freed.

//

virtual ~PlayerHuman ();

//

// Assignment Operator

//

// Purpose: To modify this PlayerHuman to be a copy of another.

// Parameter(s):

// original: The PlayerHuman to copy

// Precondition(s): N/A

// Returns: A reference to this PlayerHuman.

// Side Effect: This PlayerHuman is set to place the same

// symbol on the game board as original.

//

PlayerHuman& operator= (const PlayerHuman& original);

//

// getMove

//

// Purpose: To determine the next move for this Player on the

// specified Board.

// Parameter(s):

// board: The Board

// Precondition(s):

// board.getGameState() == GameState::UNFINISHED

// Returns: The next move for this Player on Board board.

// Side Effect: N/A

//

virtual unsigned int getMove (const Board& board) const;

};

#endif

Notice: Using the given header file

CS 210 Assignment 2 Due Date: Feb.9/2018 In this assignment, you will implement a simple Tic-Tac-Toe game. It will be possible to play human-vs-human, human-vs-computer, or computer-vs-computer Tic-Tac-Toe, also called X's and O's, Noughts and Crosses, and X and 0 is a simple game played on a 3x3 grid, referred to as the board. The grid starts empty, and the two players take turns placing their respective symbols, X and O, on an empty grid cell, referred to as making a move The first player to get a straight line of three symbols wins. If all the cells on the board are filled and neither player has a line of 3 symbols, the game is a tie. Lines may be horizontal, vertical, or diagonal. You will implement a Board class to represent the 3x3 grid. This class will have functions to determine which symbol, if any, is in a cell, to place a symbol in a cell, to determine the winner, if any so far, and to print the board to standard output. The board should appear as below: You will implement an abstract Player class to determine the player's moves. The Player class should store which symbol it will place and contain a pure virtual function to determine the Player's next move. This function will be overridden by the subclasses of Player You will implement PlayerHuman as a subclass of Player. When this subclass is to choose a move, it should print out the current board and ask the user for the row and column to place a symbol. This class should detect if the user enters an invalid location, either because it is not in the grid or it already has a symbol, and if the location is invalid, ask the user again. You will implement PlayerRandom as a subclass of Player. When this subclass is to choose a move, it should return a random position in the grid that does not yet have a symbol. You will implement a program to play the Tic Tac Toe game. The program should begin by asking the user if each team should be controlled by a human or by the computer. The progranm should then use dynamic memory allocation to create instances of the appropriate subclasses of Player. These must be stored with pointers of type Player*. The first team should place X symbols, while the second team should place O symbols. The asking the players for moves until one player wins, or the board is full and the game is a tie After the game is over, the program should print the winner and the board and exit. program should then alternate CS 210 Assignment 2 Due Date: Feb.9/2018 In this assignment, you will implement a simple Tic-Tac-Toe game. It will be possible to play human-vs-human, human-vs-computer, or computer-vs-computer Tic-Tac-Toe, also called X's and O's, Noughts and Crosses, and X and 0 is a simple game played on a 3x3 grid, referred to as the board. The grid starts empty, and the two players take turns placing their respective symbols, X and O, on an empty grid cell, referred to as making a move The first player to get a straight line of three symbols wins. If all the cells on the board are filled and neither player has a line of 3 symbols, the game is a tie. Lines may be horizontal, vertical, or diagonal. You will implement a Board class to represent the 3x3 grid. This class will have functions to determine which symbol, if any, is in a cell, to place a symbol in a cell, to determine the winner, if any so far, and to print the board to standard output. The board should appear as below: You will implement an abstract Player class to determine the player's moves. The Player class should store which symbol it will place and contain a pure virtual function to determine the Player's next move. This function will be overridden by the subclasses of Player You will implement PlayerHuman as a subclass of Player. When this subclass is to choose a move, it should print out the current board and ask the user for the row and column to place a symbol. This class should detect if the user enters an invalid location, either because it is not in the grid or it already has a symbol, and if the location is invalid, ask the user again. You will implement PlayerRandom as a subclass of Player. When this subclass is to choose a move, it should return a random position in the grid that does not yet have a symbol. You will implement a program to play the Tic Tac Toe game. The program should begin by asking the user if each team should be controlled by a human or by the computer. The progranm should then use dynamic memory allocation to create instances of the appropriate subclasses of Player. These must be stored with pointers of type Player*. The first team should place X symbols, while the second team should place O symbols. The asking the players for moves until one player wins, or the board is full and the game is a tie After the game is over, the program should print the winner and the board and exit. program should then alternate

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_2

Step: 3

blur-text-image_3

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

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions

Question

What are you curious about regarding Robert?

Answered: 1 week ago

Question

=+5. How can you show them their personal benefits?

Answered: 1 week ago

Question

=+7. How does it enhance their lifestyle?

Answered: 1 week ago