📄 board.h
字号:
// define the different class for simple board game
const int MAX_ROWS = 9; // Maximum rows
const int MAX_COLUMNS = 9; // Maximum columns
const int WINNING_LINE = 4;
const int NUM_PLAYERS = 2; // Maximum numbers of players is 4
const int DEFAULT_ROWS = 6; // Default numbers of rows
const int DEFAULT_COLUMNS = 7; // Default numbers of columns
enum Boolean {False , True };
enum BoardState {DRAW, WIN, PLAY};
const char INVISIBLE = ' ';
const int SELECT_REPRESENTATION = 1;
// Used to represent a counter that will be dropped into column on the gaming
// board
class Counter
{
private:
static int tokenNum; // which token to allocate
char theToken; // the token used as the counter
public:
static void setTokenNum();
Counter();
Counter( const char);
Counter (const int);
~Counter() {}
char token() const; // returns the characters
void display () const; // display the character to screen
};
// represent a game player, makes move and announces win or draw
class Player
{
private:
Counter thePlayersCounter;
public:
Player (); // constructor assigns a token for the player
~Player() { } // destructor
int getMove(const Boolean ) const; // Ask for a move
Counter getCounter () const; // Get a counter
void announce(const BoardState)const; // allows the player to announce the win or draw
};
// represent the individual cell in the gaming board
class Cell
{
private:
Counter theCounter; // the counter in this cell
public:
Cell(); // sets the cel to contain the space ' ' character
~Cell() {} // destructor
void clear(); // clear the cell to place space character ' '.
void drop (const Counter); // drops the counter in the cell
char holds()const; // report back the token representation of the counter
void display()const; // request the counter to display itself
};
//The gaming board which which appears on the computer
class Board
{
private:
Cell grid[MAX_ROWS][MAX_COLUMNS]; //playing board made up of cells
int height[MAX_COLUMNS]; // current height of counters col
int rowSize;
int columnSize; // size of the playing area
int lastRow; // row of the last counter
int lastCol; // col of the last counter
int numEmptyCells; // remaining empty cell
protected:
int checkForWin ( const int ,const int ,const int, const char )const;
void addCounterToBoard (const int,const Counter);
Boolean isThereAWin()const ; // check for a win
public:
// constructor default size 6 x 7
Board (const int rows =DEFAULT_ROWS,const int columns = DEFAULT_COLUMNS);
~Board() { } // destructor
// reset the game for new game
void reset (const int rows =DEFAULT_ROWS,const int columns = DEFAULT_COLUMNS);
void dropInColumn (const int,const Counter ); // drop the counter in the column
Boolean moveOkForColumn (const int) const; // check for valid col number and
// col is not full
void display()const; // draw the board
BoardState situation()const; // reports the state of the game
};
// Co-ordinate Board Game
class Game
{
private: // the playing board
Board board;
BoardState state;
Player contestant[NUM_PLAYERS]; // holds the number of player
int num;
public:
Game();
~Game(){} // destructor
void play (); // controls the game play until the state of the game
// changes
};
// end of board.h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -