满足第三次作业要求。

This commit is contained in:
unlockable
2023-03-07 15:55:44 +08:00
parent 14eed1ed79
commit 4deead2fdb
3 changed files with 53 additions and 8 deletions

View File

@@ -14,11 +14,15 @@ public:
~ChessPiece();
void setColor(bool newColor);
// 设置颜色,黑或白
void setWin();
// 将自身设置为“获胜棋子”
void reset();
// 重置为未下状态
bool isEmpty();
bool getColor();
std::string show();
// 按照自身颜色及是否是“获胜棋”给出不同的样式。
};
class ChessBoard {
@@ -29,25 +33,38 @@ private:
int chessCount;
char toHex(int);
// 把序号数字0123...变成123...ABCD...
void makeTitle();
// 输出一个棋盘表头
void countWinningPiece(bool color, int row, int column,
std::function<int(int)> const &rowFunc,
std::function<int(int)> const &columnFunc,
int possiblePieces[][2], int *connectCount);
// 检查从row, column开始以rowFunc
// columnFunc为状态转移函数连续的同颜色棋子的数量
// possiblePieces是用来存储连接的棋子坐标的
// connectCount是其它函数可能已经再其它方向找到了连接的棋子
void setWinPiece(int connectCount, int winningPieces[][2]);
// 将获胜棋子全部设置为获胜状态
public:
ChessBoard();
ChessBoard(int boardSize);
~ChessBoard();
void show();
// 显示现在的棋盘
// Returns true if success, returns false if not
bool setChess(bool color, int row, int column);
// Returns true if success, returns false if not
void reset();
// 重置所有棋子位置为空的
bool isFull();
// 返回现在是否已经下满了
bool checkWinner(int row, int column, bool color);
// 检查现在是否有玩家赢了row,
// column是最新下的一个棋子的坐标color是下这个棋的颜色
int getSize();
void countWinningPiece(bool color, int row, int column,
std::function<int(int)> const &rowFunc,
std::function<int(int)> const &columnFunc,
int possiblePieces[][2], int* connectCount);
void setWinPiece(int connectCount, int winningPieces[][2]);
// 显示棋盘的大小(行数)
};
class Player {
@@ -55,8 +72,12 @@ private:
ChessBoard *board;
std::string playerName;
bool chessType;
static int chessCount;
int toInt(char input);
// 把行号1, 2, ... ABC转换为数字0123...
static void addCount();
// 棋子计数器增加
public:
Player(std::string name, bool type, ChessBoard *chessBoard);
@@ -64,4 +85,7 @@ public:
std::string getName();
bool getColor();
putChessPieceResult setChess(char row, char column);
// 在row, column处下自己颜色的棋子
static void resetCounts();
// 重置棋子计数器
};

View File

@@ -4,6 +4,7 @@
int main() {
ChessBoard *aBoard;
// 获取棋盘的大小,初始化棋盘
{
int boardSize = 0;
while (true) {
@@ -24,6 +25,7 @@ int main() {
aBoard->show();
Player *players[2];
{
Player::resetCounts();
std::string temp = "";
std::cout << "Player 1 name: ";
std::cin >> temp;
@@ -36,6 +38,7 @@ int main() {
aBoard->show();
putChessPieceResult result;
bool nowMovePlayerIndex = false;
// 只有两种状态bool足够
do {
std::cout << players[nowMovePlayerIndex]->getName() << " ["
@@ -101,9 +104,11 @@ int main() {
'\n');
} while (input != 'y' && input != 'Y' && input != 'n' &&
input != 'N');
if (input == 'y' || input == 'Y') {
aBoard->reset();
aBoard->show();
Player::resetCounts();
continue;
}
else {

View File

@@ -1,5 +1,7 @@
#include "fiveInARow.h"
int Player::chessCount = 0;
int Player::toInt(char input) {
if ('1' <= input && input <= '9') {
return input - '1';
@@ -39,6 +41,9 @@ putChessPieceResult Player::setChess(char row, char column) {
if (!this->board->setChess(chessType, rowCount, columnCount)) {
return failed;
}
// 棋放下去了,看看是否有赢家/棋盘满/没发生什么特别的
Player::addCount();
// 有赢家
if (this->board->checkWinner(rowCount, columnCount, this->chessType)) {
std::cout << "\n####################\n"
<< this->playerName << " ["
@@ -47,11 +52,22 @@ putChessPieceResult Player::setChess(char row, char column) {
<< "####################\n";
return win;
}
if (this->board->isFull()) {
std::cout << "Board is full. Tie!" << std::endl;
// 没有赢家,检查是否满了
if (Player::chessCount >=
(this->board->getSize() * this->board->getSize())) {
std::cout << std::endl << "Board is full. Tie!" << std::endl;
return fullBoard;
}
// 没有特殊的事情,继续
else {
return success;
}
}
void Player::resetCounts() {
Player::chessCount = 0;
}
void Player::addCount() {
Player::chessCount++;
}