实现输赢判定。

This commit is contained in:
unlockable
2023-02-22 23:50:43 +08:00
parent 5af9ee4316
commit 8255bee6a9

View File

@@ -21,7 +21,7 @@ char emptyBoardString[3][29][4] = {
};
// clang-format on
enum putChessPieceResult { failed, success, win };
enum putChessPieceResult { failed, success, win, fullBoard };
class ChessPiece {
private:
@@ -56,13 +56,13 @@ public:
std::string show() {
if (this->color == BLACK) {
if (this->isWinStatus) {
return "";
return "X";
}
return "";
}
else {
if (this->isWinStatus) {
return "";
return "X";
}
return "";
}
@@ -143,7 +143,184 @@ public:
this->init();
}
bool checkWinner(int row, int column) {
bool isFull() {
return this->chessCount == 15 * 15;
}
bool checkWinner(int row, int column, bool color) {
int connetCount = 0;
int i = row, j = column;
int possiblePieces[9][2] = {0};
// vertical
for (; i < 15; i++) {
if (this->ChessPieces[i][j].isEmpty()) {
break;
}
if (this->ChessPieces[i][j].getColor() == color) {
possiblePieces[connetCount][0] = i;
possiblePieces[connetCount][1] = j;
connetCount++;
}
else {
break;
}
}
for (i = row - 1; i >= 0; i--) {
if (this->ChessPieces[i][j].isEmpty()) {
break;
}
if (this->ChessPieces[i][j].getColor() == color) {
possiblePieces[connetCount][0] = i;
possiblePieces[connetCount][1] = j;
connetCount++;
}
else {
break;
}
}
if (connetCount >= 5) {
for (; connetCount >= 0; connetCount--) {
this->ChessPieces[possiblePieces[connetCount][0]]
[possiblePieces[connetCount][1]]
.setWin();
}
return true;
}
// horizontal
i = row;
j = column;
connetCount = 0;
for (; j < 15; j++) {
if (this->ChessPieces[i][j].isEmpty()) {
break;
}
if (this->ChessPieces[i][j].getColor() == color) {
possiblePieces[connetCount][0] = i;
possiblePieces[connetCount][1] = j;
connetCount++;
}
else {
break;
}
}
for (j = column - 1; j >= 0; j--) {
if (this->ChessPieces[i][j].isEmpty()) {
break;
}
if (this->ChessPieces[i][j].getColor() == color) {
possiblePieces[connetCount][0] = i;
possiblePieces[connetCount][1] = j;
connetCount++;
}
else {
break;
}
}
if (connetCount >= 5) {
for (; connetCount >= 0; connetCount--) {
this->ChessPieces[possiblePieces[connetCount][0]]
[possiblePieces[connetCount][1]]
.setWin();
}
return true;
}
// left-up
i = row;
j = column;
connetCount = 0;
while (i < 15 && j < 15) {
if (this->ChessPieces[i][j].isEmpty()) {
break;
}
if (this->ChessPieces[i][j].getColor() == color) {
possiblePieces[connetCount][0] = i;
possiblePieces[connetCount][1] = j;
connetCount++;
}
else {
break;
}
i++;
j++;
}
i = row - 1;
j = column - 1;
while (i >= 0 && j >= 0) {
if (this->ChessPieces[i][j].isEmpty()) {
break;
}
if (this->ChessPieces[i][j].getColor() == color) {
possiblePieces[connetCount][0] = i;
possiblePieces[connetCount][1] = j;
connetCount++;
}
else {
break;
}
i--;
j--;
}
if (connetCount >= 5) {
for (; connetCount >= 0; connetCount--) {
this->ChessPieces[possiblePieces[connetCount][0]]
[possiblePieces[connetCount][1]]
.setWin();
}
return true;
}
// right-up
i = row;
j = column;
connetCount = 0;
while (i >= 0 && j < 15) {
if (this->ChessPieces[i][j].isEmpty()) {
break;
}
if (this->ChessPieces[i][j].getColor() == color) {
possiblePieces[connetCount][0] = i;
possiblePieces[connetCount][1] = j;
connetCount++;
}
else {
break;
}
i--;
j++;
}
i = row + 1;
j = column - 1;
while (i < 15 && j >= 0) {
if (this->ChessPieces[i][j].isEmpty()) {
break;
}
if (this->ChessPieces[i][j].getColor() == color) {
possiblePieces[connetCount][0] = i;
possiblePieces[connetCount][1] = j;
connetCount++;
}
else {
break;
}
i++;
j--;
}
if (connetCount >= 5) {
for (; connetCount >= 0; connetCount--) {
this->ChessPieces[possiblePieces[connetCount][0]]
[possiblePieces[connetCount][1]]
.setWin();
}
return true;
}
return false;
}
};
@@ -159,7 +336,10 @@ private:
return input - '1';
}
else if ('A' <= input && input <= 'F') {
return input - 'A' + 10;
return input - 'A' + 9;
}
else if ('a' <= input && input <= 'f') {
return input - 'a' + 9;
}
else {
return -1;
@@ -188,10 +368,14 @@ public:
if (!this->board->setChess(chessType, rowCount, columnCount)) {
return failed;
}
if (this->board->checkWinner(rowCount, columnCount)) {
if (this->board->checkWinner(rowCount, columnCount, this->chessType)) {
std::cout << this->playerName << " wins!" << std::endl;
return win;
}
if (this->board->isFull()) {
std::cout << "Board is full. Tie!" << std::endl;
return fullBoard;
}
else {
return success;
}
@@ -218,8 +402,8 @@ int main() {
do {
std::cout << players[nowMovePlayerIndex].getName() << " ["
<< (players[nowMovePlayerIndex].getColor() ? "White"
: "Black")
<< (players[nowMovePlayerIndex].getColor() ? "White"
: "Black")
<< "]: "
<< "Please enter the coordinate of your new chess piece."
<< std::endl;
@@ -227,7 +411,7 @@ int main() {
std::cout << "Row No. ";
std::cin >> row;
fflush(stdin);
while (!(('1' <= row && row <= '9') || ('A' <= row) && (row <= 'F'))) {
while (!(('1' <= row && row <= '9') || ('A' <= row && row <= 'F') || ('a' <= row && row <= 'f'))) {
std::cout << "Not a valid row number. Try again." << std::endl;
std::cout << "Row No. ";
std::cin >> row;
@@ -237,7 +421,7 @@ int main() {
std::cout << "Column No. ";
std::cin >> column;
fflush(stdin);
while (!(('1' <= row && row <= '9') || ('A' <= row) && (row <= 'F'))) {
while (!(('1' <= row && row <= '9') || ('A' <= row) && (row <= 'F') || ('a' <= row && row <= 'f'))) {
std::cout << "Not a valid column number. Try again." << std::endl;
std::cout << "Column No. ";
std::cin >> column;
@@ -252,7 +436,8 @@ int main() {
aBoard.show();
continue;
}
else if (result == win) {
else {
aBoard.show();
break;
}
} while (true);