C++ 写黑白棋,要求绘制6*6的棋盘,检测玩家的所有走法,人机对弈

时间: 2023-11-14 12:04:22 浏览: 66
下面是一个简单的C++黑白棋游戏代码示例,包括绘制棋盘、检测玩家走法和人机对弈。 ```c++ #include <iostream> #include <cstring> using namespace std; const int N = 6; // 棋盘大小 const char EMPTY = '-'; // 空格 const char BLACK = 'X'; // 黑色棋子 const char WHITE = 'O'; // 白色棋子 char board[N][N]; // 棋盘 bool turn = true; // true 表示黑棋,false 表示白棋 // 初始化棋盘 void init() { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) board[i][j] = EMPTY; board[N/2-1][N/2-1] = WHITE; board[N/2][N/2] = WHITE; board[N/2-1][N/2] = BLACK; board[N/2][N/2-1] = BLACK; } // 输出棋盘 void print_board() { cout << " "; for (int i = 0; i < N; i++) cout << i+1 << " "; cout << endl; for (int i = 0; i < N; i++) { cout << i+1 << " "; for (int j = 0; j < N; j++) cout << board[i][j] << " "; cout << endl; } } // 判断是否能落子 bool is_valid(int x, int y) { if (board[x][y] != EMPTY) return false; char cur = turn ? BLACK : WHITE; char opp = turn ? WHITE : BLACK; int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; bool flag = false; for (int i = 0; i < 8; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || nx >= N || ny < 0 || ny >= N) continue; if (board[nx][ny] != opp) continue; while (nx >= 0 && nx < N && ny >= 0 && ny < N && board[nx][ny] == opp) { nx += dx[i], ny += dy[i]; } if (nx >= 0 && nx < N && ny >= 0 && ny < N && board[nx][ny] == cur) { flag = true; break; } } return flag; } // 落子 void place(int x, int y) { char cur = turn ? BLACK : WHITE; char opp = turn ? WHITE : BLACK; board[x][y] = cur; int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; for (int i = 0; i < 8; i++) { int nx = x + dx[i], ny = y + dy[i]; if (nx < 0 || nx >= N || ny < 0 || ny >= N) continue; if (board[nx][ny] != opp) continue; int tx = nx, ty = ny; while (tx >= 0 && tx < N && ty >= 0 && ty < N && board[tx][ty] == opp) { tx += dx[i], ty += dy[i]; } if (tx >= 0 && tx < N && ty >= 0 && ty < N && board[tx][ty] == cur) { while (tx != x || ty != y) { board[tx][ty] = cur; tx -= dx[i], ty -= dy[i]; } } } } // 判断游戏是否结束 bool is_game_over() { int cnt1 = 0, cnt2 = 0; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (board[i][j] == BLACK) cnt1++; else if (board[i][j] == WHITE) cnt2++; return cnt1 == 0 || cnt2 == 0 || cnt1 + cnt2 == N*N; } // 获取当前棋盘上黑棋和白棋的数目 void get_score(int& score1, int& score2) { score1 = score2 = 0; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (board[i][j] == BLACK) score1++; else if (board[i][j] == WHITE) score2++; } // AI落子 void ai_place() { int score = -1, x = -1, y = -1; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (is_valid(i, j)) { char cur = turn ? BLACK : WHITE; char opp = turn ? WHITE : BLACK; int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1}; int cnt = 0; for (int k = 0; k < 8; k++) { int nx = i + dx[k], ny = j + dy[k]; if (nx < 0 || nx >= N || ny < 0 || ny >= N) continue; if (board[nx][ny] == opp) { while (nx >= 0 && nx < N && ny >= 0 && ny < N && board[nx][ny] == opp) { nx += dx[k], ny += dy[k]; } if (nx >= 0 && nx < N && ny >= 0 && ny < N && board[nx][ny] == cur) { while (nx != i || ny != j) { cnt++; nx -= dx[k], ny -= dy[k]; } } } } if (cnt > score) { score = cnt; x = i, y = j; } } if (x != -1 && y != -1) place(x, y); } int main() { init(); print_board(); while (!is_game_over()) { if (turn) cout << "Black's turn: "; else cout << "White's turn: "; if (turn) { bool flag = false; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (is_valid(i, j)) { flag = true; break; } if (!flag) { turn = !turn; continue; } int x, y; cin >> x >> y; x--, y--; if (is_valid(x, y)) { place(x, y); } else { cout << "Invalid move!" << endl; continue; } } else { ai_place(); } print_board(); int score1, score2; get_score(score1, score2); cout << "Black: " << score1 << ", White: " << score2 << endl; turn = !turn; } int score1, score2; get_score(score1, score2); if (score1 > score2) cout << "Black wins!" << endl; else if (score1 < score2) cout << "White wins!" << endl; else cout << "Draw!" << endl; return 0; } ``` 该代码实现了基本的黑白棋游戏功能,包括绘制棋盘、检测玩家走法和人机对弈。玩家通过输入坐标来落子,AI通过搜索所有合法落子来选择最优的位置。

相关推荐

最新推荐

recommend-type

linux系统中c++写日志文件功能分享

主要介绍了linux系统中c++写日志文件功能,简化了glog,只保留了写日志文件的功能,只是改写了linux版本,需要的朋友可以参考下
recommend-type

基于C++实现五子棋AI算法思想

主要为大家详细介绍了基于C++实现五子棋AI算法思想,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

基于c++ ege图形库实现五子棋游戏

主要为大家详细介绍了基于c++ ege图形库实现五子棋游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C++面向对象实现五子棋小游戏

本文介绍了如何运用面向对象思想进行五子棋游戏的设计与开发,与面向过程程序设计比较,面向对象程序设计更易于实现对现实世界的描述,提高软件的扩展性和可维护性。附上最终的程序源码,推荐给大家,有需要的小伙伴...
recommend-type

C/C++中*和&的用法详解

在本篇文章中我们给大家总结了C/C++中*和&的用法以及相关的代码分享,有兴趣的朋友赶紧学习下吧。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略

![MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略](http://xiaoyuge.work/explain-sql/index/2.png) # 1. MySQL数据库性能优化概述** MySQL数据库性能优化是一项至关重要的任务,可以显著提高应用程序的响应时间和整体用户体验。优化涉及识别和解决影响数据库性能的因素,包括硬件资源瓶颈、软件配置不当和数据库设计缺陷。通过采取适当的优化策略,可以显著提升数据库性能,满足业务需求并提高用户满意度。 # 2. MySQL数据库性能下降的幕后真凶 ### 2.1 硬件资源瓶颈 #### 2.1.1 CPU利用率过高 **症状:
recommend-type

如何在unity创建按钮

在 Unity 中创建按钮的步骤如下: 1. 在 Unity 中创建一个 UI Canvas,选择 GameObject -> UI -> Canvas。 2. 在 Canvas 中创建一个按钮,选择 GameObject -> UI -> Button。 3. 在场景视图中调整按钮的位置和大小。 4. 在 Inspector 中设置按钮的文本、颜色、字体等属性。 5. 添加按钮的响应事件,选择按钮,在 Inspector 的 On Click () 中添加相应的方法。 这样就可以创建一个按钮了,你可以在游戏中使用它来触发相应的操作。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。