用c++写一个五子棋游戏 包含人人对战与人机对战
时间: 2024-03-10 19:50:22 浏览: 70
好的,下面是一个基本的五子棋游戏的 C++ 代码,包含人人对战和人机对战。请注意,这只是一个基本的示例代码,您可以根据需要进行修改和优化。
```
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
const int BOARD_SIZE = 15; // 棋盘大小
const int EMPTY = 0; // 空棋子
const int BLACK = 1; // 黑棋
const int WHITE = -1; // 白棋
// 棋盘类
class Board {
public:
Board() {
for(int i = 0; i < BOARD_SIZE; i++) {
vector<int> row(BOARD_SIZE, EMPTY);
board.push_back(row);
}
}
// 判断当前位置是否越界
bool is_valid(int x, int y) const {
return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE;
}
// 判断当前位置是否为空
bool is_empty(int x, int y) const {
return board[x][y] == EMPTY;
}
// 在当前位置落子
void move(int x, int y, int player) {
board[x][y] = player;
}
// 检查是否有五子连珠
bool check_win(int x, int y) const {
int player = board[x][y];
int dx[4] = {-1, 0, 1, 1}; // 横向、纵向、正斜线、反斜线四个方向
int dy[4] = {0, 1, 1, -1};
for(int i = 0; i < 4; i++) {
int count = 1;
int nx = x + dx[i];
int ny = y + dy[i];
while(is_valid(nx, ny) && board[nx][ny] == player) {
count++;
nx += dx[i];
ny += dy[i];
}
nx = x - dx[i];
ny = y - dy[i];
while(is_valid(nx, ny) && board[nx][ny] == player) {
count++;
nx -= dx[i];
ny -= dy[i];
}
if(count >= 5) {
return true;
}
}
return false;
}
// 打印棋盘
void print() 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++) {
if(board[i][j] == EMPTY) {
cout << "+ ";
} else if(board[i][j] == BLACK) {
cout << "X ";
} else {
cout << "O ";
}
}
cout << endl;
}
}
private:
vector<vector<int>> board; // 存储棋盘上每个位置的棋子
};
// 人人对战
void play_with_human() {
Board board;
int player = BLACK;
while(true) {
cout << "Player " << player << "'s turn:" << endl;
board.print();
int x, y;
while(true) {
cout << "Enter the row and column of your move: ";
cin >> x >> y;
if(board.is_valid(x, y) && board.is_empty(x, y)) {
break;
} else {
cout << "Invalid move, please try again." << endl;
}
}
board.move(x, y, player);
if(board.check_win(x, y)) {
cout << "Player " << player << " wins!" << endl;
board.print();
break;
}
player = -player;
}
}
// 人机对战
void play_with_AI() {
Board board;
int player = BLACK;
while(true) {
cout << "Player " << player << "'s turn:" << endl;
board.print();
if(player == BLACK) {
int x, y;
while(true) {
cout << "Enter the row and column of your move: ";
cin >> x >> y;
if(board.is_valid(x, y) && board.is_empty(x, y)) {
break;
} else {
cout << "Invalid move, please try again." << endl;
}
}
board.move(x, y, player);
} else {
int x, y;
srand(time(NULL));
while(true) {
x = rand() % BOARD_SIZE;
y = rand() % BOARD_SIZE;
if(board.is_empty(x, y)) {
break;
}
}
board.move(x, y, player);
cout << "The AI plays at (" << x << ", " << y << ")." << endl;
}
if(board.check_win(x, y)) {
if(player == BLACK) {
cout << "You win!" << endl;
} else {
cout << "The AI wins!" << endl;
}
board.print();
break;
}
player = -player;
}
}
int main() {
cout << "Welcome to the game of Gobang!" << endl;
cout << "Please choose a mode:" << endl;
cout << "1. Play with human" << endl;
cout << "2. Play with AI" << endl;
int mode;
cin >> mode;
if(mode == 1) {
play_with_human();
} else if(mode == 2) {
play_with_AI();
} else {
cout << "Invalid mode, please try again." << endl;
}
return 0;
}
```
希望这个例子能对您有所帮助。如果您有任何问题,请随时提出。
阅读全文