当前位置:   article > 正文

用c++做简易的游戏_c++简单小游戏代码

c++简单小游戏代码

一、石头剪刀布

1.主要功能

和电脑进行猜拳游戏,并进行统计输赢次数

2.代码

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. int main() {
  5. srand(time(0)); // 初始化随机种子
  6. std::cout << "欢迎来到剪刀石头布游戏!" << std::endl;
  7. int playerWins = 0;
  8. int computerWins = 0;
  9. while (true) {
  10. int playerChoice;
  11. std::cout << "请选择你的出拳(1-剪刀,2-石头,3-布;输入其他数字结束游戏):";
  12. std::cin >> playerChoice;
  13. if (playerChoice < 1 || playerChoice > 3) {
  14. break;
  15. }
  16. int computerChoice = rand() % 3 + 1; // 生成1到3之间的随机数,代表电脑的出拳
  17. std::cout << "你选择了:";
  18. switch (playerChoice) {
  19. case 1:
  20. std::cout << "剪刀" << std::endl;
  21. break;
  22. case 2:
  23. std::cout << "石头" << std::endl;
  24. break;
  25. case 3:
  26. std::cout << "布" << std::endl;
  27. break;
  28. default:
  29. std::cout << "无效的选择" << std::endl;
  30. return 0;
  31. }
  32. std::cout << "电脑选择了:";
  33. switch (computerChoice) {
  34. case 1:
  35. std::cout << "剪刀" << std::endl;
  36. break;
  37. case 2:
  38. std::cout << "石头" << std::endl;
  39. break;
  40. case 3:
  41. std::cout << "布" << std::endl;
  42. break;
  43. }
  44. if (playerChoice == computerChoice) {
  45. std::cout << "平局!" << std::endl;
  46. } else if ((playerChoice == 1 && computerChoice == 3) || (playerChoice == 2 && computerChoice == 1) || (playerChoice == 3 && computerChoice == 2)) {
  47. std::cout << "你赢了!" << std::endl;
  48. playerWins++;
  49. } else {
  50. std::cout << "你输了!" << std::endl;
  51. computerWins++;
  52. }
  53. std::cout << "得分情况:" << std::endl;
  54. std::cout << "玩家:" << playerWins << " 电脑:" << computerWins << std::endl;
  55. std::cout << std::endl;
  56. }
  57. std::cout << "游戏结束!" << std::endl;
  58. return 0;
  59. }

二、扫雷

1.主要功能

输入坐标,进行扫雷

2.代码

  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <ctime>
  5. const int BOARD_SIZE = 10;
  6. const int NUM_MINES = 10;
  7. const char MINE_SYMBOL = '*';
  8. const char HIDDEN_SYMBOL = '#';
  9. // 初始化游戏板
  10. void initializeBoard(std::vector<std::vector<char>>& board) {
  11. // 初始化所有格子为隐藏状态
  12. for (int i = 0; i < BOARD_SIZE; i++) {
  13. std::vector<char> row(BOARD_SIZE, HIDDEN_SYMBOL);
  14. board.push_back(row);
  15. }
  16. }
  17. // 显示游戏板
  18. void displayBoard(const std::vector<std::vector<char>>& board) {
  19. std::cout << " ";
  20. for (int i = 0; i < BOARD_SIZE; i++) {
  21. std::cout << i << " ";
  22. }
  23. std::cout << std::endl;
  24. for (int i = 0; i < BOARD_SIZE; i++) {
  25. std::cout << i << " ";
  26. for (int j = 0; j < BOARD_SIZE; j++) {
  27. std::cout << board[i][j] << " ";
  28. }
  29. std::cout << std::endl;
  30. }
  31. }
  32. // 放置地雷
  33. void placeMines(std::vector<std::vector<char>>& board) {
  34. int count = 0;
  35. while (count < NUM_MINES) {
  36. int x = rand() % BOARD_SIZE;
  37. int y = rand() % BOARD_SIZE;
  38. // 如果该位置没有地雷,则放置地雷
  39. if (board[x][y] != MINE_SYMBOL) {
  40. board[x][y] = MINE_SYMBOL;
  41. count++;
  42. }
  43. }
  44. }
  45. // 获取指定位置周围地雷的数量
  46. int getAdjacentMinesCount(const std::vector<std::vector<char>>& board, int x, int y) {
  47. int count = 0;
  48. // 检查该位置周围的8个方向
  49. for (int i = -1; i <= 1; i++) {
  50. for (int j = -1; j <= 1; j++) {
  51. int newX = x + i;
  52. int newY = y + j;
  53. // 跳过超出边界的位置以及当前位置本身
  54. if (newX < 0 || newX >= BOARD_SIZE || newY < 0 || newY >= BOARD_SIZE || (i == 0 && j == 0)) {
  55. continue;
  56. }
  57. // 如果周围位置有地雷,则计数加一
  58. if (board[newX][newY] == MINE_SYMBOL) {
  59. count++;
  60. }
  61. }
  62. }
  63. return count;
  64. }
  65. // 揭示指定位置的格子
  66. void revealCell(std::vector<std::vector<char>>& board, std::vector<std::vector<bool>>& visited, int x, int y) {
  67. // 跳过已经访问过的格子和已经揭示出来的格子
  68. if (visited[x][y] || board[x][y] != HIDDEN_SYMBOL) {
  69. return;
  70. }
  71. visited[x][y] = true;
  72. // 获取该位置周围地雷的数量
  73. int adjacentMines = getAdjacentMinesCount(board, x, y);
  74. // 如果该位置周围有地雷,则将该数量显示在格子上
  75. if (adjacentMines > 0) {
  76. board[x][y] = '0' + adjacentMines;
  77. }
  78. // 否则递归揭示周围的格子
  79. else {
  80. board[x][y] = ' ';
  81. for (int i = -1; i <= 1; i++) {
  82. for (int j = -1; j <= 1; j++) {
  83. int newX = x + i;
  84. int newY = y + j;
  85. if (newX >= 0 && newX < BOARD_SIZE && newY >= 0 && newY < BOARD_SIZE) {
  86. revealCell(board, visited, newX, newY);
  87. }
  88. }
  89. }
  90. }
  91. }
  92. // 扫雷游戏主循环
  93. void gameLoop() {
  94. std::vector<std::vector<char>> board;
  95. std::vector<std::vector<bool>> visited;
  96. initializeBoard(board);
  97. placeMines(board);
  98. for (int i = 0; i < BOARD_SIZE; i++) {
  99. std::vector<bool> row(BOARD_SIZE, false);
  100. visited.push_back(row);
  101. }
  102. bool gameOver = false;
  103. while (!gameOver) {
  104. displayBoard(board);
  105. int x, y;
  106. std::cout << "请输入要揭示的格子坐标 (x, y): ";
  107. std::cin >> x >> y;
  108. // 检查输入是否合法
  109. if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
  110. std::cout << "无效的坐标!" << std::endl;
  111. continue;
  112. }
  113. // 如果该位置是地雷,游戏结束
  114. if (board[x][y] == MINE_SYMBOL) {
  115. gameOver = true;
  116. std::cout << "很遗憾,踩到地雷了!游戏结束。" << std::endl;
  117. break;
  118. }
  119. // 如果该位置周围没有地雷,则递归揭示周围的格子
  120. if (board[x][y] == HIDDEN_SYMBOL) {
  121. revealCell(board, visited, x, y);
  122. }
  123. // 检查是否胜利
  124. int hiddenCount = 0;
  125. for (int i = 0; i < BOARD_SIZE; i++) {
  126. for (int j = 0; j < BOARD_SIZE; j++) {
  127. if (board[i][j] == HIDDEN_SYMBOL || board[i][j] == MINE_SYMBOL) {
  128. hiddenCount++;
  129. }
  130. }
  131. }
  132. if (hiddenCount == NUM_MINES) {
  133. gameOver = true;
  134. std::cout << "恭喜您,成功找到所有的地雷!游戏胜利。" << std::endl;
  135. }
  136. }
  137. }
  138. int main() {
  139. srand(time(0)); // 初始化随机种子
  140. std::cout << "欢迎来到扫雷游戏!" << std::endl;
  141. std::cout << "请输入要揭示的格子坐标以避免踩到地雷。" << std::endl;
  142. std::cout << "揭示一个格子将会显示出周围地雷的数量。找到所有的地雷即可胜利!" << std::endl;
  143. gameLoop();
  144. return 0;
  145. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/336460
推荐阅读
相关标签
  

闽ICP备14008679号