c++实现黑白棋包含计算机走法,人人对弈
时间: 2023-12-06 08:44:33 浏览: 136
一个黑白棋的C++实现
黑白棋,又称翻转棋,是一种非常经典的策略棋类游戏。下面是一个简单的 C++ 实现,包含人机对弈和计算机自动走棋两种模式。
首先,我们需要定义一个棋盘类,用于存储棋局状态和操作。代码如下:
```c++
#include <iostream>
#include <vector>
using namespace std;
// 定义棋盘类
class Board {
public:
static const int SIZE = 8;
static const int EMPTY = 0;
static const int BLACK = 1;
static const int WHITE = -1;
static const int DIRECTIONS[][2];
Board(): curPlayer(BLACK) {
// 初始化棋盘
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = EMPTY;
}
}
board[SIZE/2 - 1][SIZE/2 - 1] = WHITE;
board[SIZE/2][SIZE/2] = WHITE;
board[SIZE/2 - 1][SIZE/2] = BLACK;
board[SIZE/2][SIZE/2 - 1] = BLACK;
}
// 打印当前棋盘状态
void printBoard() const {
cout << " ";
for (int j = 0; j < SIZE; j++) {
cout << j << " ";
}
cout << endl;
for (int i = 0; i < SIZE; i++) {
cout << i << " ";
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == EMPTY) {
cout << "* ";
} else if (board[i][j] == BLACK) {
cout << "X ";
} else {
cout << "O ";
}
}
cout << endl;
}
}
// 判断某个位置是否可以落子
bool canPlace(int i, int j, int player) const {
if (board[i][j] != EMPTY) {
return false;
}
for (int k = 0; k < 8; k++) {
int ni = i + DIRECTIONS[k][0], nj = j + DIRECTIONS[k][1];
if (ni >= 0 && ni < SIZE && nj >= 0 && nj < SIZE && board[ni][nj] == -player) {
while (ni >= 0 && ni < SIZE && nj >= 0 && nj < SIZE && board[ni][nj] == -player) {
ni += DIRECTIONS[k][0];
nj += DIRECTIONS[k][1];
}
if (ni >= 0 && ni < SIZE && nj >= 0 && nj < SIZE && board[ni][nj] == player) {
return true;
}
}
}
return false;
}
// 落子
void place(int i, int j, int player) {
board[i][j] = player;
for (int k = 0; k < 8; k++) {
int ni = i + DIRECTIONS[k][0], nj = j + DIRECTIONS[k][1];
if (ni >= 0 && ni < SIZE && nj >= 0 && nj < SIZE && board[ni][nj] == -player) {
vector<pair<int, int>> flip;
while (ni >= 0 && ni < SIZE && nj >= 0 && nj < SIZE && board[ni][nj] == -player) {
flip.emplace_back(ni, nj);
ni += DIRECTIONS[k][0];
nj += DIRECTIONS[k][1];
}
if (ni >= 0 && ni < SIZE && nj >= 0 && nj < SIZE && board[ni][nj] == player) {
for (auto [fi, fj] : flip) {
board[fi][fj] = player;
}
}
}
}
}
// 判断是否有合法落子点
bool hasValidPlace(int player) const {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (canPlace(i, j, player)) {
return true;
}
}
}
return false;
}
// 计算当前棋局得分
int getScore(int player) const {
int score = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == player) {
score++;
} else if (board[i][j] == -player) {
score--;
}
}
}
return score;
}
// 获取当前玩家
int getCurrentPlayer() const {
return curPlayer;
}
// 切换玩家
void switchPlayer() {
curPlayer = -curPlayer;
}
private:
int board[SIZE][SIZE];
int curPlayer;
};
const int Board::DIRECTIONS[][2] = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
```
接下来,我们定义一个游戏类,用于管理游戏流程,包括双方轮流落子、判断胜负等操作。代码如下:
```c++
// 定义游戏类
class Game {
public:
Game(): mode(HUMAN_VS_HUMAN), curPlayer(Board::BLACK) {}
// 开始游戏
void start() {
while (true) {
board.printBoard();
if (mode == HUMAN_VS_HUMAN) {
humanPlay();
} else {
computerPlay();
}
if (!board.hasValidPlace(curPlayer)) {
board.switchPlayer();
if (!board.hasValidPlace(curPlayer)) {
break;
}
}
board.switchPlayer();
}
board.printBoard();
int score = board.getScore(Board::BLACK);
if (score > 0) {
cout << "Black wins! Score: " << score << endl;
} else if (score < 0) {
cout << "White wins! Score: " << -score << endl;
} else {
cout << "Draw game!" << endl;
}
}
// 人机对弈
void computerPlay() {
if (curPlayer == Board::BLACK) {
humanPlay();
} else {
cout << "Computer is thinking..." << endl;
int bestScore = -Board::SIZE * Board::SIZE;
int bestI, bestJ;
for (int i = 0; i < Board::SIZE; i++) {
for (int j = 0; j < Board::SIZE; j++) {
if (board.canPlace(i, j, curPlayer)) {
Board tempBoard = board;
tempBoard.place(i, j, curPlayer);
int score = alphaBeta(tempBoard, -curPlayer, 1, -Board::SIZE * Board::SIZE, Board::SIZE * Board::SIZE);
if (score > bestScore) {
bestScore = score;
bestI = i;
bestJ = j;
}
}
}
}
cout << "Computer places at (" << bestI << ", " << bestJ << ")" << endl;
board.place(bestI, bestJ, curPlayer);
}
}
// 人人对弈
void humanPlay() {
cout << (curPlayer == Board::BLACK ? "Black" : "White") << " places (i, j): ";
int i, j;
cin >> i >> j;
while (!board.canPlace(i, j, curPlayer)) {
cout << "Invalid position! Please try again." << endl;
cout << (curPlayer == Board::BLACK ? "Black" : "White") << " places (i, j): ";
cin >> i >> j;
}
board.place(i, j, curPlayer);
}
// alpha-beta 剪枝搜索
int alphaBeta(Board& board, int player, int depth, int alpha, int beta) {
if (depth == 5 || !board.hasValidPlace(player)) {
return board.getScore(player);
}
if (player == curPlayer) {
int bestScore = -Board::SIZE * Board::SIZE;
for (int i = 0; i < Board::SIZE; i++) {
for (int j = 0; j < Board::SIZE; j++) {
if (board.canPlace(i, j, player)) {
Board tempBoard = board;
tempBoard.place(i, j, player);
int score = alphaBeta(tempBoard, -player, depth + 1, alpha, beta);
bestScore = max(bestScore, score);
alpha = max(alpha, score);
if (alpha >= beta) {
return bestScore;
}
}
}
}
return bestScore;
} else {
int bestScore = Board::SIZE * Board::SIZE;
for (int i = 0; i < Board::SIZE; i++) {
for (int j = 0; j < Board::SIZE; j++) {
if (board.canPlace(i, j, player)) {
Board tempBoard = board;
tempBoard.place(i, j, player);
int score = alphaBeta(tempBoard, -player, depth + 1, alpha, beta);
bestScore = min(bestScore, score);
beta = min(beta, score);
if (alpha >= beta) {
return bestScore;
}
}
}
}
return bestScore;
}
}
private:
enum Mode { HUMAN_VS_HUMAN, HUMAN_VS_COMPUTER };
Mode mode;
Board board;
int curPlayer;
};
```
最后,我们在主函数中创建游戏对象并开始游戏:
```c++
int main() {
Game game;
game.start();
return 0;
}
```
这样,我们就完成了一个简单的黑白棋实现,包含计算机走法和人人对弈两种模式。
阅读全文