c++黑白棋游戏,6*6的棋盘,要求可以选择玩家,人机两种玩法
时间: 2023-10-14 09:04:15 浏览: 229
下面是一个简单的 C++ 黑白棋游戏的实现,包括人机对战和双人对战两种玩法。该游戏使用 6x6 的棋盘,黑方先手。其中,空位置用 "." 表示,黑子用 "X" 表示,白子用 "O" 表示。
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int ROWS = 6;
const int COLS = 6;
const int DIRS = 8;
const int dx[DIRS] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy[DIRS] = {0, 1, 1, 1, 0, -1, -1, -1};
class Board {
public:
Board() {
for (int i = 0; i < ROWS; i++) {
vector<string> row(COLS, ".");
board.push_back(row);
}
board[2][2] = board[3][3] = "O";
board[2][3] = board[3][2] = "X";
}
void print() {
cout << " 0 1 2 3 4 5" << endl;
for (int i = 0; i < ROWS; i++) {
cout << i << " ";
for (int j = 0; j < COLS; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}
bool is_valid_move(int row, int col, string player) {
if (row < 0 || row >= ROWS || col < 0 || col >= COLS || board[row][col] != ".") {
return false;
}
for (int d = 0; d < DIRS; d++) {
int r = row + dx[d];
int c = col + dy[d];
if (r < 0 || r >= ROWS || c < 0 || c >= COLS || board[r][c] == "." || board[r][c] == player) {
continue;
}
while (r >= 0 && r < ROWS && c >= 0 && c < COLS && board[r][c] != ".") {
if (board[r][c] == player) {
return true;
}
r += dx[d];
c += dy[d];
}
}
return false;
}
void make_move(int row, int col, string player) {
board[row][col] = player;
for (int d = 0; d < DIRS; d++) {
int r = row + dx[d];
int c = col + dy[d];
if (r < 0 || r >= ROWS || c < 0 || c >= COLS || board[r][c] == "." || board[r][c] == player) {
continue;
}
while (r >= 0 && r < ROWS && c >= 0 && c < COLS && board[r][c] != ".") {
if (board[r][c] == player) {
int nr = row + dx[d];
int nc = col + dy[d];
while (nr != r || nc != c) {
board[nr][nc] = player;
nr += dx[d];
nc += dy[d];
}
break;
}
r += dx[d];
c += dy[d];
}
}
}
bool has_valid_moves(string player) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (is_valid_move(i, j, player)) {
return true;
}
}
}
return false;
}
int count_discs(string player) {
int count = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == player) {
count++;
}
}
}
return count;
}
string get_winner() {
int black_count = count_discs("X");
int white_count = count_discs("O");
if (black_count == white_count) {
return "Draw";
} else if (black_count > white_count) {
return "Black";
} else {
return "White";
}
}
private:
vector<vector<string>> board;
};
class Player {
public:
virtual void make_move(Board& board) = 0;
};
class HumanPlayer : public Player {
public:
HumanPlayer(string player) : player(player) {}
void make_move(Board& board) {
while (true) {
cout << player << " player's turn." << endl;
cout << "Enter row and column (e.g. 2 3): ";
int row, col;
cin >> row >> col;
if (board.is_valid_move(row, col, player)) {
board.make_move(row, col, player);
break;
} else {
cout << "Invalid move. Try again." << endl;
}
}
}
private:
string player;
};
class ComputerPlayer : public Player {
public:
ComputerPlayer(string player) : player(player) {}
void make_move(Board& board) {
cout << player << " player's turn." << endl;
int best_row = -1, best_col = -1, best_score = -1;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board.is_valid_move(i, j, player)) {
Board copy = board;
copy.make_move(i, j, player);
int score = evaluate(copy, player);
if (score > best_score) {
best_row = i;
best_col = j;
best_score = score;
}
}
}
}
board.make_move(best_row, best_col, player);
cout << "Computer played at (" << best_row << ", " << best_col << ")." << endl;
}
private:
int evaluate(Board& board, string player) {
int score = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board.is_valid_move(i, j, player)) {
Board copy = board;
copy.make_move(i, j, player);
int disc_diff = copy.count_discs(player) - copy.count_discs(get_opponent(player));
score += disc_diff;
}
}
}
return score;
}
string get_opponent(string player) {
return player == "X" ? "O" : "X";
}
string player;
};
int main() {
Board board;
board.print();
string player1, player2;
cout << "Select player1 (X for human, O for computer): ";
cin >> player1;
cout << "Select player2 (X for human, O for computer): ";
cin >> player2;
Player* p1, *p2;
if (player1 == "X") {
p1 = new HumanPlayer("X");
} else {
p1 = new ComputerPlayer("X");
}
if (player2 == "O") {
p2 = new HumanPlayer("O");
} else {
p2 = new ComputerPlayer("O");
}
while (board.has_valid_moves("X") || board.has_valid_moves("O")) {
p1->make_move(board);
board.print();
if (!board.has_valid_moves("O")) {
break;
}
p2->make_move(board);
board.print();
}
cout << "Game over." << endl;
cout << "Winner: " << board.get_winner() << endl;
delete p1;
delete p2;
return 0;
}
```
该程序定义了一个 Board 类来表示棋盘,其中包含了打印棋盘、判断是否为有效步数、执行步数、判断是否有有效步数、统计棋子数等函数。该程序还定义了两个玩家类 HumanPlayer 和 ComputerPlayer,分别表示人类玩家和电脑玩家。其中,电脑玩家使用了 Minimax 算法来计算最佳下棋位置。
在 main 函数中,程序首先创建一个 Board 对象,然后让玩家选择玩家类型,如果是人类玩家就创建 HumanPlayer 对象,如果是电脑玩家就创建 ComputerPlayer 对象。接着,程序进入游戏循环,每个玩家轮流执行 make_move 函数,直到没有有效步数为止。最后,程序输出胜者并释放玩家对象的内存。
运行程序后,你可以选择人机对战或双人对战,然后根据提示输入行列坐标来下棋。如果选择的是人机对战,程序会自动计算电脑的最佳下棋位置。
阅读全文