#include <iostream>
#include <cstdlib>
#include <vector>
const int BOARD_SIZE = 19;
// 定义一个结构体来记录每一步的落子信息
struct Move {
int row;
int col;
char player;
};
// 存储每一步落子信息的向量
std::vector<Move> moveHistory;
// 检查是否有五子连珠
bool checkWin(char board[BOARD_SIZE][BOARD_SIZE], int x, int y, char player) {
// 检查横向
int count = 1;
for (int i = y - 1; i >= 0 && board[x][i] == player; i--) {
count++;
}
for (int i = y + 1; i < BOARD_SIZE && board[x][i] == player; i++) {
count++;
}
if (count >= 5) return true;
// 检查纵向
count = 1;
for (int i = x - 1; i >= 0 && board[i][y] == player; i--) {
count++;
}
for (int i = x + 1; i < BOARD_SIZE && board[i][y] == player; i++) {
count++;
}
if (count >= 5) return true;
// 检查正斜向
count = 1;
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i][j] == player; i--, j--) {
count++;
}
for (int i = x + 1, j = y + 1; i < BOARD_SIZE && j < BOARD_SIZE && board[i][j] == player; i++, j++) {
count++;
}
if (count >= 5) return true;
// 检查反斜向
count = 1;
for (int i = x - 1, j = y + 1; i >= 0 && j < BOARD_SIZE && board[i][j] == player; i--, j++) {
count++;
}
for (int i = x + 1, j = y - 1; i < BOARD_SIZE && j >= 0 && board[i][j] == player; i++, j--) {
count++;
}
if (count >= 5) return true;
return false;
}
// 打印棋盘
void printBoard(char board[BOARD_SIZE][BOARD_SIZE]) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
std::cout << board[i][j] << " ";
}
std::cout << i + 1 << std::endl;
}
std::cout << "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19" << std::endl;
}
// 打印落子历史记录
void printMoveHistory() {
std::cout << "Move History:" << std::endl;
for (size_t i = 0; i < moveHistory.size(); ++i) {
const Move& move = moveHistory[i];
std::cout << "Step " << i + 1 << ": Player " << (move.player == '0' ? "Black" : "White")
<< " placed at (" << move.row + 1 << ", " << move.col + 1 << ")" << std::endl;
}
}
int main() {
char board[BOARD_SIZE][BOARD_SIZE];
// 初始化棋盘
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = '+';
}
}
while (1) {
printBoard(board);
int a, b;
while (1) {
std::cout << "Black's turn. Enter row and column (1 - 19, or 0 to quit): ";
std::cin >> a >> b;
if (a == 0) {
std::cout << "White win!";
printMoveHistory();
return 0;
}
if (a >= 1 && a <= BOARD_SIZE && b >= 1 && b <= BOARD_SIZE && board[a - 1][b - 1] == '+') {
break;
}
std::cout << "Invalid move. Try again." << std::endl;
}
board[a - 1][b - 1] = '0';
// 记录黑棋落子信息
Move blackMove = {a - 1, b - 1, '0'};
moveHistory.push_back(blackMove);
system("cls");
if (checkWin(board, a - 1, b - 1, '0')) {
printBoard(board);
std::cout << "Black win!";
printMoveHistory();
break;
}
printBoard(board);
int c, d;
while (1) {
std::cout << "White's turn. Enter row and column (1 - 19, or 0 to quit): ";
std::cin >> c >> d;
if (c == 0) {
std::cout << "Black win!";
printMoveHistory();
return 0;
}
if (c >= 1 && c <= BOARD_SIZE && d >= 1 && d <= BOARD_SIZE && board[c - 1][d - 1] == '+') {
break;
}
std::cout << "Invalid move. Try again." << std::endl;
}
board[c - 1][d - 1] = '1';
// 记录白棋落子信息
Move whiteMove = {c - 1, d - 1, '1'};
moveHistory.push_back(whiteMove);
system("cls");
if (checkWin(board, c - 1, d - 1, '1')) {
printBoard(board);
std::cout << "White win!";
printMoveHistory();
break;
}
}
return 0;
}五子棋,要的自己拿
lz是ppl