c++编程写一个五子棋小游戏
时间: 2024-03-23 18:42:25 浏览: 84
一个使用C++基于EasyX图形库编写的五子棋小游戏项目。适合C++初学者练习,可以快速上手图形游戏编程.zip
5星 · 资源好评率100%
好的,以下是一个简单的五子棋小游戏的C++代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
const int SIZE = 15; // 棋盘大小
char chessBoard[SIZE][SIZE]; // 棋盘
// 初始化棋盘
void initChessBoard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
chessBoard[i][j] = '+';
}
}
}
// 显示棋盘
void displayChessBoard() {
cout << " ";
for (int i = 0; i < SIZE; i++) {
cout << i + 1 << " ";
}
cout << endl;
for (int i = 0; i < SIZE; i++) {
cout << i + 1 << " ";
for (int j = 0; j < SIZE; j++) {
cout << chessBoard[i][j] << " ";
}
cout << endl;
}
}
// 判断是否胜利
bool isWin(int x, int y) {
char ch = chessBoard[x][y];
int count = 1;
// 判断横向
for (int i = y + 1; i < SIZE; i++) {
if (chessBoard[x][i] == ch) {
count++;
} else {
break;
}
}
for (int i = y - 1; i >= 0; i--) {
if (chessBoard[x][i] == ch) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 判断竖向
count = 1;
for (int i = x + 1; i < SIZE; i++) {
if (chessBoard[i][y] == ch) {
count++;
} else {
break;
}
}
for (int i = x - 1; i >= 0; i--) {
if (chessBoard[i][y] == ch) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 判断左上-右下斜向
count = 1;
for (int i = x + 1, j = y + 1; i < SIZE && j < SIZE; i++, j++) {
if (chessBoard[i][j] == ch) {
count++;
} else {
break;
}
}
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) {
if (chessBoard[i][j] == ch) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
// 判断左下-右上斜向
count = 1;
for (int i = x + 1, j = y - 1; i < SIZE && j >= 0; i++, j--) {
if (chessBoard[i][j] == ch) {
count++;
} else {
break;
}
}
for (int i = x - 1, j = y + 1; i >= 0 && j < SIZE; i--, j++) {
if (chessBoard[i][j] == ch) {
count++;
} else {
break;
}
}
if (count >= 5) {
return true;
}
return false;
}
int main() {
int x, y; // 记录下棋位置
int step = 0; // 记录步数
char player; // 玩家
initChessBoard(); // 初始化棋盘
displayChessBoard(); // 显示棋盘
while (step < SIZE * SIZE) {
if (step % 2 == 0) {
player = 'X';
} else {
player = 'O';
}
cout << "请玩家 " << player << " 下棋(输入坐标):";
cin >> x >> y;
if (x <= 0 || x > SIZE || y <= 0 || y > SIZE || chessBoard[x - 1][y - 1] != '+') {
cout << "输入不合法,请重新输入!" << endl;
continue;
}
chessBoard[x - 1][y - 1] = player;
displayChessBoard();
if (isWin(x - 1, y - 1)) {
cout << "玩家 " << player << " 获胜!" << endl;
break;
}
step++;
}
if (step == SIZE * SIZE) {
cout << "平局!" << endl;
}
return 0;
}
```
运行结果:
```
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 + + + + + + + + + + + + + + +
2 + + + + + + + + + + + + + + +
3 + + + + + + + + + + + + + + +
4 + + + + + + + + + + + + + + +
5 + + + + + + + + + + + + + + +
6 + + + + + + + + + + + + + + +
7 + + + + + + + + + + + + + + +
8 + + + + + + + + + + + + + + +
9 + + + + + + + + + + + + + + +
10 + + + + + + + + + + + + + + +
11 + + + + + + + + + + + + + + +
12 + + + + + + + + + + + + + + +
13 + + + + + + + + + + + + + + +
14 + + + + + + + + + + + + + + +
15 + + + + + + + + + + + + + + +
请玩家 X 下棋(输入坐标):8 8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 + + + + + + + + + + + + + + +
2 + + + + + + + + + + + + + + +
3 + + + + + + + + + + + + + + +
4 + + + + + + + + + + + + + + +
5 + + + + + + + + + + + + + + +
6 + + + + + + + + + + + + + + +
7 + + + + + + + + + + + + + + +
8 + + + + + + + X + + + + + + +
9 + + + + + + + + + + + + + + +
10 + + + + + + + + + + + + + + +
11 + + + + + + + + + + + + + + +
12 + + + + + + + + + + + + + + +
13 + + + + + + + + + + + + + + +
14 + + + + + + + + + + + + + + +
15 + + + + + + + + + + + + + + +
请玩家 O 下棋(输入坐标):8 9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 + + + + + + + + + + + + + + +
2 + + + + + + + + + + + + + + +
3 + + + + + + + + + + + + + + +
4 + + + + + + + + + + + + + + +
5 + + + + + + + + + + + + + + +
6 + + + + + + + + + + + + + + +
7 + + + + + + + + + + + + + + +
8 + + + + + + + X O + + + + + +
9 + + + + + + + + + + + + + + +
10 + + + + + + + + + + + + + + +
11 + + + + + + + + + + + + + + +
12 + + + + + + + + + + + + + + +
13 + + + + + + + + + + + + + + +
14 + + + + + + + + + + + + + + +
15 + + + + + + + + + + + + + + +
请玩家 X 下棋(输入坐标):
```
注意:这只是一个简单的五子棋小游戏,还有很多可以改进和扩展的地方。
阅读全文