📄 game.cpp
字号:
// definition of members method of all the classes
#include <iostream.h>
#include "board.h"
int Counter::tokenNum ; // allocate storage to the static
// sets the representational token to invisible
void Counter:: setTokenNum()
{
tokenNum = 0;
}
// sets the representational token to the one requested
inline Counter::Counter()
{
theToken = INVISIBLE;
}
// dummy parameter to distinguish constructor
Counter::Counter(const int)
{ // Subscripting operator used to deliver a token to use asa counter
theToken = "XO$#"[tokenNum++];
}
Counter::Counter(const char representation)
{
theToken = representation;
}
inline char Counter::token() const
{
return theToken;
}
inline void Counter::display() const
{
cout << theToken;
}
// assigns the token for the player
Player::Player()
{
thePlayersCounter = Counter (SELECT_REPRESENTATION);
}
int Player::getMove(const Boolean first) const
{
int move;
if (first != True)
cout << "ERROR -- Invalid choice " << endl;
cout << "Move for player " << thePlayersCounter.token() << " is ";
cin >> move;
return move -1;
}
inline Counter Player::getCounter() const
{
return thePlayersCounter;
}
void Player::announce(const BoardState what) const
{
cout << "Player " << thePlayersCounter.token();
switch (what)
{
case WIN : cout << " wins" << endl;
break;
case DRAW : cout << " Board is full " << endl;
break;
}
}
Cell::Cell()
{
theCounter = Counter(INVISIBLE);
}
inline void Cell:: clear()
{
theCounter = Counter(INVISIBLE);
}
inline void Cell:: drop(const Counter c)
{
theCounter = c;
}
inline char Cell::holds() const
{
return theCounter.token();
}
inline void Cell::display() const
{
theCounter.display();
}
Board::Board (const int rows,const int columns)
{
reset(rows, columns);
}
void Board::reset(const int rows,const int columns)
{
rowSize= rows;
columnSize= columns;
for (int i = 0; i < columnSize; i++)
{
for (int j =0; j < rowSize; j++)
grid[j][i].clear();
height[i] = 0;
}
lastRow=lastCol = 0;
numEmptyCells = rows*columns;
}
void Board::dropInColumn (const int column, const Counter c)
{
lastCol = column;
lastRow=height[column];
addCounterToBoard(column, c);
}
Boolean Board::moveOkForColumn (const int column) const
{
if (column >= 0 && column < columnSize)
if (height[column] < rowSize)
return True;
return False;
}
void Board:: addCounterToBoard(const int column, const Counter playersCounter)
{
grid[height[column]] [column].drop(playersCounter);
height[column]++;
numEmptyCells--;
}
BoardState Board::situation() const
{
if (isThereAWin() == TRUE)
return WIN;
if (numEmptyCells == 0)
return DRAW;
return PLAY;
}
Boolean Board::isThereAWin() const
{
char counter = grid[lastRow][lastCol].holds();
for (int direction = 1; direction <=4; direction++)
{
int countersInaLine = checkForWin(direction, lastRow, lastCol, counter)
+ checkForWin(direction + 4, lastRow, lastCol, counter) - 1;
if(countersInaLine >= WINNING_LINE)
return True;
}
return False;
}
int Board:: checkForWin(const int direction, const int xCord, const int yCord,
const char counter) const
{
int x = xCord;
int y = yCord;
if (( x >=0 && x < rowSize) && (y >=0 && y < columnSize)
&& (grid[x][y].holds() == counter))
{
switch(direction)
{
case 1: y++; break;
case 2: x++; y++; break;
case 3: x++; break;
case 4: x++; y--; break;
case 5: y--; break;
case 6: x--; y--; break;
case 7: x--; break;
case 8: x--; y++; break;
default: cerr << "Internal Error 1" << endl;
}
return 1+ checkForWin(direction, x, y, counter); // this is recirsive call
}
else
return 0;
}
void Board::display() const
{
cout << " " ;
for (int i=1; i <=columnSize; i++)
cout << i << " " ;
cout << endl;
for(int j= rowSize-1; j >=0; j--)
{
cout << " | ";
for(int k=0; k < columnSize; k++)
{
grid[j][k].display();
cout << " | ";
}
cout << endl;
}
for(int m=rowSize; m >=0; m--)
cout << "----";
cout << "--" << endl << endl;
}
Game::Game()
{
Counter :: setTokenNum(); // set static
state = PLAY; // contestants can play
num =0;
board.display(); // display the board
}
void Game:: play()
{
while (state==PLAY)
{
int move = contestant[num].getMove(True);
while(board.moveOkForColumn(move) == False)
move= contestant[num].getMove(False);
board.dropInColumn(move, contestant[num].getCounter());
board.display();
state = board.situation(); // board returns its current state i.e.
// the state of play
if (state == PLAY)
num = (num +1) % NUM_PLAYERS;
else
contestant[num].announce(state);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -