请用c++编写一个五子棋人机对战的小游戏
时间: 2023-06-13 11:09:23 浏览: 185
好的,以下是一个简单的五子棋人机对战的小游戏的C++代码:
```
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
const int BOARD_SIZE = 15;
const int WIN_NUM = 5;
const char PLAYER_MARK = 'X';
const char AI_MARK = 'O';
// 棋盘类
class Board {
public:
Board() : board(BOARD_SIZE, vector<char>(BOARD_SIZE, ' ')) {}
// 打印棋盘
void print_board() const {
cout << " ";
for (int i = 0; i < BOARD_SIZE; ++i) {
cout << i << " ";
}
cout << endl;
for (int i = 0; i < BOARD_SIZE; ++i) {
cout << i;
for (int j = 0; j < BOARD_SIZE; ++j) {
cout << "|" << board[i][j];
}
cout << "|" << endl;
}
}
// 判断棋盘上的某个位置是否为空
bool is_empty(int row, int col) const {
return board[row][col] == ' ';
}
// 在棋盘上落子
void place_mark(int row, int col, char mark) {
board[row][col] = mark;
}
// 判断是否有玩家或AI获胜
bool is_win(char mark) const {
// 判断每行是否有五个连续的
for (int i = 0; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE - WIN_NUM + 1; ++j) {
int k = 0;
while (k < WIN_NUM && board[i][j+k] == mark) {
++k;
}
if (k == WIN_NUM) {
return true;
}
}
}
// 判断每列是否有五个连续的
for (int i = 0; i < BOARD_SIZE - WIN_NUM + 1; ++i) {
for (int j = 0; j < BOARD_SIZE; ++j) {
int k = 0;
while (k < WIN_NUM && board[i+k][j] == mark) {
++k;
}
if (k == WIN_NUM) {
return true;
}
}
}
// 判断主对角线是否有五个连续的
for (int i = 0; i < BOARD_SIZE - WIN_NUM + 1; ++i) {
for (int j = 0; j < BOARD_SIZE - WIN_NUM + 1; ++j) {
int k = 0;
while (k < WIN_NUM && board[i+k][j+k] == mark) {
++k;
}
if (k == WIN_NUM) {
return true;
}
}
}
// 判断副对角线是否有五个连续的
for (int i = WIN_NUM - 1; i < BOARD_SIZE; ++i) {
for (int j = 0; j < BOARD_SIZE - WIN_NUM + 1; ++j) {
int k = 0;
while (k < WIN_NUM && board[i-k][j+k] == mark) {
++k;
}
if (k == WIN_NUM) {
return true;
}
}
}
return false;
}
private:
vector<vector<char>> board;
};
// 玩家类
class Player {
public:
// 玩家下棋
void make_move(Board& board) const {
int row, col;
do {
cout << "请输入要落子的位置(行 列,以空格分隔):";
cin >> row >> col;
} while (row < 0 || row >= BOARD_SIZE ||
col < 0 || col >= BOARD_SIZE ||
!board.is_empty(row, col));
board.place_mark(row, col, PLAYER_MARK);
}
};
// AI类
class AI {
public:
// AI下棋
void make_move(Board& board) const {
// 随机找一个空位落子
int row, col;
do {
row = rand() % BOARD_SIZE;
col = rand() % BOARD_SIZE;
} while (!board.is_empty(row, col));
board.place_mark(row, col, AI_MARK);
}
};
int main() {
srand(time(nullptr));
Board board;
Player player;
AI ai;
int turn = 0; // 0表示玩家先手,1表示AI先手
while (true) {
board.print_board();
if (turn % 2 == 0) {
cout << "轮到玩家落子" << endl;
player.make_move(board);
if (board.is_win(PLAYER_MARK)) {
board.print_board();
cout << "玩家获胜!" << endl;
break;
}
} else {
cout << "轮到AI落子" << endl;
ai.make_move(board);
if (board.is_win(AI_MARK)) {
board.print_board();
cout << "AI获胜!" << endl;
break;
}
}
++turn;
if (turn == BOARD_SIZE * BOARD_SIZE) {
board.print_board();
cout << "平局!" << endl;
break;
}
}
return 0;
}
```
该程序中,我们定义了一个棋盘类`Board`,一个玩家类`Player`和一个AI类`AI`。棋盘类维护了一个二维向量,表示棋盘上的格子;玩家类和AI类都有一个下棋的方法,分别接受一个棋盘对象作为参数,根据当前棋盘状态进行下棋。在主函数中,我们通过不断轮流调用玩家和AI的下棋方法来模拟人机对战。同时,我们在棋盘类中定义了一个`is_win`方法来判断当前状态下是否有玩家或AI获胜。
该程序实现了一个比较简单的AI,它只是随机找一个空位落子。如果想要实现更强的AI,可以考虑使用一些搜索算法,例如Alpha-Beta剪枝算法等。
阅读全文
相关推荐
















