Files
BasicsOfComputerSoftwareEng…/OOP/FiveInARow/player.cpp
2023-03-07 23:41:27 +08:00

73 lines
1.8 KiB
C++

#include "fiveInARow.h"
int Player::chessCount = 0;
int Player::toInt(char input) {
if ('1' <= input && input <= '9') {
return input - '1';
}
else if ('A' <= input && input <= 'J') {
return input - 'A' + 9;
}
else if ('a' <= input && input <= 'j') {
return input - 'a' + 9;
}
else {
return -1;
}
}
Player::Player(std::string name, bool type, ChessBoard *chessBoard) {
this->playerName = name;
this->chessType = type;
this->board = chessBoard;
}
Player::~Player() {
}
std::string Player::getName() {
return this->playerName;
}
bool Player::getColor() {
return this->chessType;
}
putChessPieceResult Player::setChess(char row, char column) {
int rowCount, columnCount;
rowCount = toInt(row);
columnCount = toInt(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 << " ["
<< (this->getColor() ? "○White" : "●Black") << "] "
<< " wins!" << std::endl
<< "####################\n";
return win;
}
// 没有赢家,检查是否满了
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++;
}