使用c++编写五子棋游戏
时间: 2024-03-03 09:54:00 浏览: 67
好的,以下是一个简单的五子棋游戏的示例。该示例使用了控制台界面和简单的人机对战。
```c++
#include <iostream>
#include <cstring>
#include <ctime>
#include <cstdlib>
using namespace std;
const int SIZE = 15;
char board[SIZE][SIZE];
void clear_board() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = '+';
}
}
}
void print_board() {
cout << " ";
for (int i = 0; i < SIZE; i++) {
cout << i + 1 << " ";
}
cout << endl;
for (int i = 0; i < SIZE; i++) {
cout << char(i + 'A') << " ";
for (int j = 0; j < SIZE; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}
bool is_valid_pos(int row, int col) {
if (row < 0 || row >= SIZE || col < 0 || col >= SIZE) {
return false;
}
if (board[row][col] != '+') {
return false;
}
return true;
}
bool check_win(char player) {
// 检查每行
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j <= SIZE - 5; j++) {
int k;
for (k = 0; k < 5; k++) {
if (board[i][j + k] != player) {
break;
}
}
if (k == 5) {
return true;
}
}
}
// 检查每列
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j <= SIZE - 5; j++) {
int k;
for (k = 0; k < 5; k++) {
if (board[j + k][i] != player) {
break;
}
}
if (k == 5) {
return true;
}
}
}
// 检查每条对角线
for (int i = 0; i <= SIZE - 5; i++) {
for (int j = 0; j <= SIZE - 5; j++) {
int k;
for (k = 0; k < 5; k++) {
if (board[i + k][j + k] != player) {
break;
}
}
if (k == 5) {
return true;
}
}
}
// 检查每条反对角线
for (int i = 0; i <= SIZE - 5; i++) {
for (int j = SIZE - 1; j >= 4; j--) {
int k;
for (k = 0; k < 5; k++) {
if (board[i + k][j - k] != player) {
break;
}
}
if (k == 5) {
return true;
}
}
}
return false;
}
void human_play(char player) {
cout << "请玩家 " << player << " 下棋(如:A1):" << endl;
while (true) {
string input;
getline(cin, input);
if (input.length() < 2) {
cout << "输入无效,请重新输入:" << endl;
continue;
}
int row = input[0] - 'A';
int col = input[1] - '1';
if (!is_valid_pos(row, col)) {
cout << "此位置不能下,请重新输入:" << endl;
continue;
}
board[row][col] = player;
break;
}
}
void computer_play(char player) {
cout << "电脑 " << player << " 正在思考..." << endl;
while (true) {
int row = rand() % SIZE;
int col = rand() % SIZE;
if (is_valid_pos(row, col)) {
board[row][col] = player;
break;
}
}
}
int main() {
srand(time(NULL));
clear_board();
print_board();
char human_player, computer_player;
cout << "请选择执棋方(X或O):" << endl;
while (true) {
string input;
getline(cin, input);
if (input.length() < 1 || (input[0] != 'X' && input[0] != 'O')) {
cout << "输入无效,请重新输入:" << endl;
continue;
}
human_player = input[0];
computer_player = (human_player == 'X') ? 'O' : 'X';
break;
}
while (true) {
human_play(human_player);
print_board();
if (check_win(human_player)) {
cout << "恭喜玩家 " << human_player << " 获胜!" << endl;
break;
}
computer_play(computer_player);
print_board();
if (check_win(computer_player)) {
cout << "电脑 " << computer_player << " 获胜!" << endl;
break;
}
}
return 0;
}
```
该示例中,我们首先定义了一个15x15的棋盘,并实现了清空棋盘、打印棋盘、检查落子位置是否有效、检查是否获胜、人类玩家下棋和电脑玩家下棋等函数。然后,我们在主函数中实现了游戏的流程,包括选择执棋方、轮流下棋、检查获胜等操作。在电脑玩家下棋时,我们使用了随机算法来选择落子位置。
需要注意的是,这只是一个简单的示例,实际应用中还需要进行更多的错误处理和数据校验,以确保程序的稳定性和可靠性。此外,该示例只实现了最基本的人机对战,还可以通过网络实现多人对战等更高级的功能。
阅读全文