#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
using namespace std;
#define ROW 15
#define COL 15
// 打印用户界面
void print_the_board(char chessboard[][COL])
{
// 打印列号
cout << " ";
for (int i = 0; i < COL; i++)
{
cout << hex << i << " ";
}
cout << endl;
for (int i = 0; i < ROW; i++)
{
// 打印行号
cout << hex << i << " ";
for (int j = 0; j < COL; j++)
{
if (chessboard[i][j] == '0')
{
cout << ". ";
}
else
{
cout << chessboard[i][j] << " ";
}
}
cout << endl;
}
}
// 初始化棋盘
void initialise_the_board(char chessboard[][COL])
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
chessboard[i][j] = '0';
}
}
}
// 用户输入坐标
void enter_coordinates(char chessboard[][COL], int &number)
{
int a = 0;
int b = 0;
int valid = false;
while (!valid)
{
cout << "玩家 " << (number % 2 == 1? "黑子玩家" : "白子玩家")
<< " 请输入落子位置(行 列): " << endl;
cin >> a >> b;
if (a >= 0 && a < ROW && b >= 0 && b < COL
&& chessboard[a][b] == '0')
{
valid = true;
chessboard[a][b] = (number % 2 == 1)? 'B' : 'W';
}
else
{
cout << "位置违规或已被占用,请重新输入" << endl;
}
}
number++;
}
// 检查是否获胜
bool check_win(char chessboard[][COL])
{
// 检查行
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j <= COL - 5; j++)
{
if (chessboard[i][j] != '0' &&
chessboard[i][j] == chessboard[i][j + 1] &&
chessboard[i][j] == chessboard[i][j + 2] &&
chessboard[i][j] == chessboard[i][j + 3] &&
chessboard[i][j] == chessboard[i][j + 4])
{
return true;
}
}
}
// 检查列
for (int j = 0; j < COL; j++)
{
for (int i = 0; i <= ROW - 5; i++)
{
if (chessboard[i][j]!= '0' &&
chessboard[i][j] == chessboard[i + 1][j] &&
chessboard[i][j] == chessboard[i + 2][j] &&
chessboard[i][j] == chessboard[i + 3][j] &&
chessboard[i][j] == chessboard[i + 4][j] )
{
return true;
}
}
}
// 检查主对角线
for (int i = 0; i <= ROW - 5; i++)
{
for (int j = 0; j <= COL - 5; j++)
{
if (chessboard[i][j]!= '0' &&
chessboard[i][j] == chessboard[i + 1][j + 1] &&
chessboard[i][j] == chessboard[i + 2][j + 2] &&
chessboard[i][j] == chessboard[i + 3][j + 3] &&
chessboard[i][j] == chessboard[i + 4][j + 4])
{
return true;
}
}
}
// 检查副对角线
for (int i = 0; i <= ROW - 5; i++)
{
for (int j = 4; j < COL; j++)
{
if (chessboard[i][j]!= '0' &&
chessboard[i][j] == chessboard[i + 1][j - 1] &&
chessboard[i][j] == chessboard[i + 2][j - 2] &&
chessboard[i][j] == chessboard[i + 3][j - 3] &&
chessboard[i][j] == chessboard[i + 4][j - 4])
{
return true;
}
}
}
return false;
}
// 检查棋盘是否已满
bool is_board_full(char chessboard[][COL])
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
if (chessboard[i][j] == '0')
{
return false;
}
}
}
return true;
}
{
int number = 1;
char chessboard [ROW][COL] = {'0'};
// 初始化棋盘
initialise_the_board(chessboard);
// 打印用户界面
print_the_board(chessboard);
while (true)
{
//用户输入坐标
enter_coordinates(chessboard, number);
// 打印用户界面
print_the_board(chessboard);
if (check_win(chessboard))
{
cout << "玩家 " << ((number - 1) % 2 == 1? "X" : "O")
<< " 获胜!" << endl;
break;
}
if (is_board_full(chessboard))
{
cout << "平局!" << endl;
break;
}
}
return 0;
}
五子棋代码(不分)
最新推荐文章于 2025-05-17 22:15:01 发布