优化获胜判定。
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
#include "fiveInARow.h"
|
||||
|
||||
// clang-format off
|
||||
char ChessBoard::emptyBoardString[3][29][4] = {
|
||||
char ChessBoard::emptyBoardString[3][4][4] = {
|
||||
{
|
||||
"┏", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┯", "━", "┓"
|
||||
"┏", "━", "┯", "┓"
|
||||
},
|
||||
{
|
||||
"┠", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┼", "─", "┨"
|
||||
"┠", "─", "┼", "┨"
|
||||
},
|
||||
{
|
||||
"┗", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┷", "━", "┛",
|
||||
"┗", "━", "┷", "┛",
|
||||
}
|
||||
};
|
||||
// clang-format on
|
||||
@@ -46,28 +46,37 @@ ChessBoard::~ChessBoard() {
|
||||
|
||||
void ChessBoard::show() {
|
||||
this->makeTitle();
|
||||
for (int i = 0; i < 15; i++) {
|
||||
for (int i = 0; i < this->size; i++) {
|
||||
std::cout << this->toHex(i + 1) << ' ';
|
||||
char(*emptyLineRef)[29][4] = NULL;
|
||||
char(*emptyLineRef)[4][4] = NULL;
|
||||
if (i == 0) {
|
||||
emptyLineRef = &emptyBoardString[0];
|
||||
}
|
||||
else if (i == 14) {
|
||||
else if (i == this->size - 1) {
|
||||
emptyLineRef = &emptyBoardString[2];
|
||||
}
|
||||
else {
|
||||
emptyLineRef = &emptyBoardString[1];
|
||||
}
|
||||
int j = 0;
|
||||
for (; j < 15; j++) {
|
||||
for (; j < this->size; j++) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
std::cout << (*emptyLineRef)[j * 2];
|
||||
// std::cout << (*emptyLineRef)[j * 2];
|
||||
if (j == 0) {
|
||||
std::cout << (*emptyLineRef)[0];
|
||||
}
|
||||
else if (j == this->size - 1) {
|
||||
std::cout << (*emptyLineRef)[3];
|
||||
}
|
||||
else {
|
||||
std::cout << (*emptyLineRef)[2];
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cout << this->ChessPieces[i][j].show();
|
||||
}
|
||||
if (j < 14) {
|
||||
std::cout << (*emptyLineRef)[j * 2 + 1];
|
||||
if (j < this->size - 1) {
|
||||
std::cout << (*emptyLineRef)[1];
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
@@ -75,7 +84,8 @@ void ChessBoard::show() {
|
||||
}
|
||||
|
||||
bool ChessBoard::setChess(bool color, int row, int column) {
|
||||
if (row > 14 || row < 0 || column > 14 || column < 0) {
|
||||
if (row > this->size - 1 || row < 0 || column > this->size - 1 ||
|
||||
column < 0) {
|
||||
std::cout << "Out of border!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
@@ -89,8 +99,8 @@ bool ChessBoard::setChess(bool color, int row, int column) {
|
||||
}
|
||||
|
||||
void ChessBoard::reset() {
|
||||
for (int i = 0; i < 15; i++) {
|
||||
for (int j = 0; j < 15; j++) {
|
||||
for (int i = 0; i < this->size; i++) {
|
||||
for (int j = 0; j < this->size; j++) {
|
||||
this->ChessPieces[i][j].reset();
|
||||
}
|
||||
}
|
||||
@@ -98,7 +108,7 @@ void ChessBoard::reset() {
|
||||
}
|
||||
|
||||
bool ChessBoard::isFull() {
|
||||
return this->chessCount == 15 * 15;
|
||||
return this->chessCount == this->size * this->size;
|
||||
}
|
||||
|
||||
bool ChessBoard::checkWinner(int row, int column, bool color) {
|
||||
@@ -107,173 +117,101 @@ bool ChessBoard::checkWinner(int row, int column, bool color) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
this->countWinningPiece(
|
||||
color, row + 1, column, [=](int i) { return i + 1; },
|
||||
[=](int j) { return j; }, possiblePieces, &connetCount);
|
||||
this->countWinningPiece(
|
||||
color, row - 1, column, [=](int i) { return i - 1; },
|
||||
[=](int j) { return j; }, possiblePieces, &connetCount);
|
||||
|
||||
if (connetCount >= 5) {
|
||||
for (connetCount--; connetCount >= 0; connetCount--) {
|
||||
this->ChessPieces[possiblePieces[connetCount][0]]
|
||||
[possiblePieces[connetCount][1]]
|
||||
.setWin();
|
||||
}
|
||||
if (connetCount >= 4) {
|
||||
// 最后现在下的这个点没有算在这个里面,要手动设置成成功
|
||||
this->ChessPieces[row][column].setWin();
|
||||
this->setWinPiece(connetCount, possiblePieces);
|
||||
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;
|
||||
}
|
||||
}
|
||||
this->countWinningPiece(
|
||||
color, row, column + 1, [=](int i) { return i; },
|
||||
[=](int j) { return j + 1; }, possiblePieces, &connetCount);
|
||||
this->countWinningPiece(
|
||||
color, row, column - 1, [=](int i) { return i; },
|
||||
[=](int j) { return j - 1; }, possiblePieces, &connetCount);
|
||||
|
||||
if (connetCount >= 5) {
|
||||
for (connetCount--; connetCount >= 0; connetCount--) {
|
||||
this->ChessPieces[possiblePieces[connetCount][0]]
|
||||
[possiblePieces[connetCount][1]]
|
||||
.setWin();
|
||||
}
|
||||
if (connetCount >= 4) {
|
||||
// 最后现在下的这个点没有算在这个里面,要手动设置成成功
|
||||
this->ChessPieces[row][column].setWin();
|
||||
this->setWinPiece(connetCount, possiblePieces);
|
||||
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--;
|
||||
}
|
||||
this->countWinningPiece(
|
||||
color, row + 1, column + 1, [=](int i) { return i + 1; },
|
||||
[=](int j) { return j + 1; }, possiblePieces, &connetCount);
|
||||
this->countWinningPiece(
|
||||
color, row - 1, column - 1, [=](int i) { return i - 1; },
|
||||
[=](int j) { return j - 1; }, possiblePieces, &connetCount);
|
||||
|
||||
if (connetCount >= 5) {
|
||||
for (connetCount--; connetCount >= 0; connetCount--) {
|
||||
this->ChessPieces[possiblePieces[connetCount][0]]
|
||||
[possiblePieces[connetCount][1]]
|
||||
.setWin();
|
||||
}
|
||||
if (connetCount >= 4) {
|
||||
// 最后现在下的这个点没有算在这个里面,要手动设置成成功
|
||||
this->ChessPieces[row][column].setWin();
|
||||
this->setWinPiece(connetCount, possiblePieces);
|
||||
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--;
|
||||
}
|
||||
this->countWinningPiece(
|
||||
color, row + 1, column - 1, [=](int i) { return i + 1; },
|
||||
[=](int j) { return j - 1; }, possiblePieces, &connetCount);
|
||||
this->countWinningPiece(
|
||||
color, row - 1, column + 1, [=](int i) { return i - 1; },
|
||||
[=](int j) { return j + 1; }, possiblePieces, &connetCount);
|
||||
|
||||
if (connetCount >= 5) {
|
||||
for (connetCount--; connetCount >= 0; connetCount--) {
|
||||
this->ChessPieces[possiblePieces[connetCount][0]]
|
||||
[possiblePieces[connetCount][1]]
|
||||
.setWin();
|
||||
}
|
||||
if (connetCount >= 4) {
|
||||
// 最后现在下的这个点没有算在这个里面,要手动设置成成功
|
||||
this->ChessPieces[row][column].setWin();
|
||||
this->setWinPiece(connetCount, possiblePieces);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
int ChessBoard::getSize() {
|
||||
return this->size;
|
||||
}
|
||||
|
||||
void ChessBoard::countWinningPiece(bool color, int row, int column,
|
||||
std::function<int(int)> const &rowFunc,
|
||||
std::function<int(int)> const &columnFunc,
|
||||
int possiblePieces[][2], int *connectCount) {
|
||||
int i = row, j = column;
|
||||
while (0 <= i && i <= 15 && 0 <= j && j <= 15) {
|
||||
if (this->ChessPieces[i][j].isEmpty()) {
|
||||
break;
|
||||
}
|
||||
if (this->ChessPieces[i][j].getColor() == color) {
|
||||
possiblePieces[*connectCount][0] = i;
|
||||
possiblePieces[*connectCount][1] = j;
|
||||
*connectCount += 1;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
i = rowFunc(i);
|
||||
j = columnFunc(j);
|
||||
}
|
||||
}
|
||||
|
||||
void ChessBoard::setWinPiece(int connectCount, int winningPieces[][2]) {
|
||||
int count = connectCount;
|
||||
for (count--; count >= 0; count--) {
|
||||
this->ChessPieces[winningPieces[count][0]][winningPieces[count][1]]
|
||||
.setWin();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user