当前位置:   article > 正文

C语言实现三子棋(人机对战)_c语言井字棋游戏怎么做人机

c语言井字棋游戏怎么做人机

一,前言:

三子棋也叫井字棋,玩法比较简单,满足三子一条线即可。接下来我们将使用C语言来实现控制台版本的人机对战三子棋小游戏。

本篇博客会提供分段代码方便大家理解,同时在文章的末尾提供完整代码和三合一完全体代码,需要的小伙伴可以自取^-^

二,代码实施:

1.创建项目,添加源文件text.c ,game.c ,game.h。

他们的作用分别是:

text.c

测试代码能否正确执行

game.c

创建支持游戏运行的代码

game.h

声明自定义函数

2.搭建游戏基本框架
  1. void menu();//游戏菜单
  2. void game();//游戏母体
  3. void test();//测试
  4. int main()
  5. {
  6. test();
  7. return 0;
  8. }
3.框架代码实现
  1. void menu()//菜单代码
  2. {
  3. printf("*****************************\n");
  4. printf("******** 1. play ******\n");
  5. printf("******** 0. eixt ******\n");
  6. printf("*****************************\n");
  7. }
  8. void game()//游戏母体代码
  9. {
  10. char board[ROW][COL];//存放玩家或者电脑的落子情况,board即为棋盘
  11. char ret = 0;
  12. //初始化一下棋盘为全空格
  13. init_board(board, ROW, COL);
  14. print_board(board, ROW, COL);
  15. while (1)
  16. {
  17. player_move(board, ROW, COL);
  18. print_board(board, ROW, COL);
  19. //判断输赢
  20. ret = is_win(board, ROW, COL);
  21. if (ret != 'C')
  22. {
  23. break;
  24. }
  25. computer_move(board, ROW, COL);
  26. print_board(board, ROW, COL);
  27. //判断输赢
  28. ret = is_win(board, ROW, COL);
  29. if (ret != 'C')
  30. {
  31. break;
  32. }
  33. }
  34. if (ret == '#')
  35. printf("电脑赢了\n");
  36. else if (ret == '*')
  37. printf("玩家赢了\n");
  38. else if (ret == 'Q')
  39. printf("平局\n");
  40. }
  41. void test()//测试代码
  42. {
  43. srand((unsigned int)time(NULL));
  44. int input = 0;
  45. do
  46. {
  47. menu();
  48. printf("请选择:>");
  49. scanf("%d", &input);
  50. switch (input)
  51. {
  52. case 1:
  53. game();
  54. break;
  55. case 0:
  56. printf("退出游戏\n");
  57. break;
  58. default:
  59. printf("选择错误\n");
  60. break;
  61. }
  62. } while (input);
  63. }
4.功能代码实现

void init_board(char board[ROW][COL], int row, int col)

棋盘初始化

void print_board(char board[ROW][COL], int row, int col)

打印棋盘

void player_move(char board[ROW][COL], int row, int col)

玩家走棋

void computer_move(char board[ROW][COL], int row, int col)

电脑走棋

static int is_full(char board[ROW][COL], int row, int col)

判断棋盘是否满子

char is_win(char board[ROW][COL], int row, int col)

判断是否产生赢家

各功能代码具体信息:
  1. void init_board(char board[ROW][COL], int row, int col)
  2. {
  3. int i = 0;
  4. for (i = 0; i < row; i++)
  5. {
  6. int j = 0;
  7. for (j = 0; j < col; j++)
  8. {
  9. board[i][j] = ' ';
  10. }
  11. }
  12. }
  1. void print_board(char board[ROW][COL], int row, int col)
  2. {
  3. int i = 0;
  4. for (i = 0; i < row; i++)
  5. {
  6. //printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
  7. int j = 0;
  8. for (j = 0; j < col; j++)
  9. {
  10. printf(" %c ", board[i][j]);
  11. if (j < col - 1)
  12. printf("|");
  13. }
  14. printf("\n");
  15. if (i < row - 1)
  16. {
  17. //printf("---|---|---\n");
  18. for (j = 0; j < col; j++)
  19. {
  20. printf("---");
  21. if (j < col - 1)
  22. printf("|");
  23. }
  24. printf("\n");
  25. }
  26. }
  27. }
  1. void player_move(char board[ROW][COL], int row, int col)
  2. {
  3. printf("玩家下棋\n");
  4. while (1)
  5. {
  6. printf("请输入要下棋的坐标:>");
  7. int x = 0;
  8. int y = 0;
  9. scanf("%d %d", &x, &y);
  10. //坐标合法
  11. if (x >= 1 && x <= row && y >= 1 && y <= col)
  12. {
  13. if (board[x - 1][y - 1] == ' ')
  14. {
  15. board[x - 1][y - 1] = '*';
  16. break;
  17. }
  18. else
  19. {
  20. printf("该坐标被占用,请重新输入\n");
  21. }
  22. }
  23. else//坐标非法
  24. {
  25. printf("坐标非法\n");
  26. }
  27. }
  28. }
  1. //随机生成坐标,只要坐标没有被占用,就下棋
  2. void computer_move(char board[ROW][COL], int row, int col)
  3. {
  4. printf("电脑下棋:\n");
  5. while (1)
  6. {
  7. int x = rand() % row;
  8. int y = rand() % col;
  9. if (board[x][y] == ' ')
  10. {
  11. board[x][y] = '#';
  12. break;
  13. }
  14. }
  15. }
  1. //判断棋盘是否满了
  2. static int is_full(char board[ROW][COL], int row, int col)
  3. {
  4. int i = 0;
  5. int j = 0;
  6. for (i = 0; i < row; i++)
  7. {
  8. for (j = 0; j < col; j++)
  9. {
  10. if (board[i][j] == ' ')
  11. return 0;
  12. }
  13. }
  14. return 1;
  15. }
  1. char is_win(char board[ROW][COL], int row, int col)
  2. {
  3. int i = 0;
  4. //判断三行
  5. for (i = 0; i < row; i++)
  6. {
  7. if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
  8. {
  9. return board[i][0];
  10. }
  11. }
  12. //判断三列
  13. for (i = 0; i < col; i++)
  14. {
  15. if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
  16. {
  17. return board[0][i];
  18. }
  19. }
  20. //判断对角线
  21. if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
  22. {
  23. return board[1][1];
  24. }
  25. if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
  26. {
  27. return board[1][1];
  28. }
  29. //平局?
  30. if (is_full(board, row, col) == 1)
  31. {
  32. return 'Q';
  33. }
  34. //继续
  35. //没有玩家或者电脑赢,也没有平局,游戏继续
  36. return 'C';
  37. }
5.头文件
  1. #pragma once
  2. #define ROW 3
  3. #define COL 3
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. //头文件中声明函数
  8. //初始化棋盘
  9. void init_board(char board[ROW][COL], int row, int col);
  10. //打印棋盘
  11. void print_board(char board[ROW][COL], int row, int col);
  12. //玩家下棋
  13. void player_move(char board[ROW][COL], int row, int col);
  14. //电脑下棋
  15. void computer_move(char board[ROW][COL], int row, int col);
  16. //判断输赢的函数
  17. char is_win(char board[ROW][COL], int row, int col);

三,总结(完整代码提供)

1.text.c
  1. #include "game.h"
  2. void menu()
  3. {
  4. printf("*****************************\n");
  5. printf("******** 1. play ******\n");
  6. printf("******** 0. eixt ******\n");
  7. printf("*****************************\n");
  8. }
  9. void game()
  10. {
  11. char board[ROW][COL];
  12. char ret = 0;
  13. init_board(board, ROW, COL);
  14. print_board(board, ROW, COL);
  15. while (1)
  16. {
  17. player_move(board, ROW, COL);
  18. print_board(board, ROW, COL);
  19. ret = is_win(board, ROW, COL);
  20. if (ret != 'C')
  21. {
  22. break;
  23. }
  24. computer_move(board, ROW, COL);
  25. print_board(board, ROW, COL);
  26. ret = is_win(board, ROW, COL);
  27. if (ret != 'C')
  28. {
  29. break;
  30. }
  31. }
  32. if (ret == '#')
  33. printf("电脑赢了\n");
  34. else if (ret == '*')
  35. printf("玩家赢了\n");
  36. else if (ret == 'Q')
  37. printf("平局\n");
  38. }
  39. void test()
  40. {
  41. srand((unsigned int)time(NULL));
  42. int input = 0;
  43. do
  44. {
  45. menu();
  46. printf("请选择:>");
  47. scanf("%d", &input);
  48. switch (input)
  49. {
  50. case 1:
  51. game();
  52. break;
  53. case 0:
  54. printf("退出游戏\n");
  55. break;
  56. default:
  57. printf("选择错误\n");
  58. break;
  59. }
  60. } while (input);
  61. }
  62. int main()
  63. {
  64. test();
  65. return 0;
  66. }
2.game.c
  1. #include "game.h"
  2. void init_board(char board[ROW][COL], int row, int col)
  3. {
  4. int i = 0;
  5. for (i = 0; i < row; i++)
  6. {
  7. int j = 0;
  8. for (j = 0; j < col; j++)
  9. {
  10. board[i][j] = ' ';
  11. }
  12. }
  13. }
  14. void print_board(char board[ROW][COL], int row, int col)
  15. {
  16. int i = 0;
  17. for (i = 0; i < row; i++)
  18. {
  19. int j = 0;
  20. for (j = 0; j < col; j++)
  21. {
  22. printf(" %c ", board[i][j]);
  23. if (j < col - 1)
  24. printf("|");
  25. }
  26. printf("\n");
  27. if (i < row - 1)
  28. {
  29. for (j = 0; j < col; j++)
  30. {
  31. printf("---");
  32. if (j < col - 1)
  33. printf("|");
  34. }
  35. printf("\n");
  36. }
  37. }
  38. }
  39. void player_move(char board[ROW][COL], int row, int col)
  40. {
  41. printf("玩家下棋\n");
  42. while (1)
  43. {
  44. printf("请输入要下棋的坐标:>");
  45. int x = 0;
  46. int y = 0;
  47. scanf("%d %d", &x, &y);
  48. if (x >= 1 && x <= row && y >= 1 && y <= col)
  49. {
  50. if (board[x - 1][y - 1] == ' ')
  51. {
  52. board[x - 1][y - 1] = '*';
  53. break;
  54. }
  55. else
  56. {
  57. printf("该坐标被占用,请重新输入\n");
  58. }
  59. }
  60. else
  61. {
  62. printf("坐标非法\n");
  63. }
  64. }
  65. }
  66. void computer_move(char board[ROW][COL], int row, int col)
  67. {
  68. printf("电脑下棋:\n");
  69. while (1)
  70. {
  71. int x = rand() % row;
  72. int y = rand() % col;
  73. if (board[x][y] == ' ')
  74. {
  75. board[x][y] = '#';
  76. break;
  77. }
  78. }
  79. }
  80. static int is_full(char board[ROW][COL], int row, int col)
  81. {
  82. int i = 0;
  83. int j = 0;
  84. for (i = 0; i < row; i++)
  85. {
  86. for (j = 0; j < col; j++)
  87. {
  88. if (board[i][j] == ' ')
  89. return 0;
  90. }
  91. }
  92. return 1;
  93. }
  94. char is_win(char board[ROW][COL], int row, int col)
  95. {
  96. int i = 0;
  97. for (i = 0; i < row; i++)
  98. {
  99. if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
  100. {
  101. return board[i][0];
  102. }
  103. }
  104. for (i = 0; i < col; i++)
  105. {
  106. if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
  107. {
  108. return board[0][i];
  109. }
  110. }
  111. if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
  112. {
  113. return board[1][1];
  114. }
  115. if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
  116. {
  117. return board[1][1];
  118. }
  119. if (is_full(board, row, col) == 1)
  120. {
  121. return 'Q';
  122. }
  123. return 'C';
  124. }
3.game.h
  1. #define ROW 3
  2. #define COL 3
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. void init_board(char board[ROW][COL], int row, int col);
  7. void print_board(char board[ROW][COL], int row, int col);
  8. void player_move(char board[ROW][COL], int row, int col);
  9. void computer_move(char board[ROW][COL], int row, int col);
  10. char is_win(char board[ROW][COL], int row, int col);
4.三合一
  1. #define ROW 3
  2. #define COL 3
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. void init_board(char board[ROW][COL], int row, int col)
  7. {
  8. int i = 0;
  9. for (i = 0; i < row; i++)
  10. {
  11. int j = 0;
  12. for (j = 0; j < col; j++)
  13. {
  14. board[i][j] = ' ';
  15. }
  16. }
  17. }
  18. void print_board(char board[ROW][COL], int row, int col)
  19. {
  20. int i = 0;
  21. for (i = 0; i < row; i++)
  22. {
  23. //printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
  24. int j = 0;
  25. for (j = 0; j < col; j++)
  26. {
  27. printf(" %c ", board[i][j]);
  28. if (j < col - 1)
  29. printf("|");
  30. }
  31. printf("\n");
  32. if (i < row - 1)
  33. {
  34. //printf("---|---|---\n");
  35. for (j = 0; j < col; j++)
  36. {
  37. printf("---");
  38. if (j < col - 1)
  39. printf("|");
  40. }
  41. printf("\n");
  42. }
  43. }
  44. }
  45. void player_move(char board[ROW][COL], int row, int col)
  46. {
  47. printf("玩家下棋\n");
  48. while (1)
  49. {
  50. printf("请输入要下棋的坐标:>");
  51. int x = 0;
  52. int y = 0;
  53. scanf("%d %d", &x, &y);
  54. //坐标合法
  55. if (x >= 1 && x <= row && y >= 1 && y <= col)
  56. {
  57. if (board[x - 1][y - 1] == ' ')
  58. {
  59. board[x - 1][y - 1] = '*';
  60. break;
  61. }
  62. else
  63. {
  64. printf("该坐标被占用,请重新输入\n");
  65. }
  66. }
  67. else//坐标非法
  68. {
  69. printf("坐标非法\n");
  70. }
  71. }
  72. }
  73. //随机生成坐标,只要坐标没有被占用,就下棋
  74. void computer_move(char board[ROW][COL], int row, int col)
  75. {
  76. printf("电脑下棋:\n");
  77. while (1)
  78. {
  79. int x = rand() % row;
  80. int y = rand() % col;
  81. if (board[x][y] == ' ')
  82. {
  83. board[x][y] = '#';
  84. break;
  85. }
  86. }
  87. }
  88. static int is_full(char board[ROW][COL], int row, int col)
  89. {
  90. int i = 0;
  91. int j = 0;
  92. for (i = 0; i < row; i++)
  93. {
  94. for (j = 0; j < col; j++)
  95. {
  96. if (board[i][j] == ' ')
  97. return 0;
  98. }
  99. }
  100. return 1;
  101. }
  102. char is_win(char board[ROW][COL], int row, int col)
  103. {
  104. int i = 0;
  105. //判断三行
  106. for (i = 0; i < row; i++)
  107. {
  108. if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
  109. {
  110. return board[i][0];
  111. }
  112. }
  113. //判断三列
  114. for (i = 0; i < col; i++)
  115. {
  116. if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
  117. {
  118. return board[0][i];
  119. }
  120. }
  121. //判断对角线
  122. if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
  123. {
  124. return board[1][1];
  125. }
  126. if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
  127. {
  128. return board[1][1];
  129. }
  130. //平局?
  131. if (is_full(board, row, col) == 1)
  132. {
  133. return 'Q';
  134. }
  135. //继续
  136. //没有玩家或者电脑赢,也没有平局,游戏继续
  137. return 'C';
  138. }
  139. void menu()//菜单代码
  140. {
  141. printf("*****************************\n");
  142. printf("******** 1. play ******\n");
  143. printf("******** 0. eixt ******\n");
  144. printf("*****************************\n");
  145. }
  146. void game()//游戏母体代码
  147. {
  148. char board[ROW][COL];//存放玩家或者电脑的落子情况,board即为棋盘
  149. char ret = 0;
  150. //初始化一下棋盘为全空格
  151. init_board(board, ROW, COL);
  152. print_board(board, ROW, COL);
  153. while (1)
  154. {
  155. player_move(board, ROW, COL);
  156. print_board(board, ROW, COL);
  157. //判断输赢
  158. ret = is_win(board, ROW, COL);
  159. if (ret != 'C')
  160. {
  161. break;
  162. }
  163. computer_move(board, ROW, COL);
  164. print_board(board, ROW, COL);
  165. //判断输赢
  166. ret = is_win(board, ROW, COL);
  167. if (ret != 'C')
  168. {
  169. break;
  170. }
  171. }
  172. if (ret == '#')
  173. printf("电脑赢了\n");
  174. else if (ret == '*')
  175. printf("玩家赢了\n");
  176. else if (ret == 'Q')
  177. printf("平局\n");
  178. }
  179. void test()//测试代码
  180. {
  181. srand((unsigned int)time(NULL));
  182. int input = 0;
  183. do
  184. {
  185. menu();
  186. printf("请选择:>");
  187. scanf("%d", &input);
  188. switch (input)
  189. {
  190. case 1:
  191. game();
  192. break;
  193. case 0:
  194. printf("退出游戏\n");
  195. break;
  196. default:
  197. printf("选择错误\n");
  198. break;
  199. }
  200. } while (input);
  201. }
  202. int main()
  203. {
  204. test();
  205. return 0;
  206. }
'
运行
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/1017764
推荐阅读
相关标签
  

闽ICP备14008679号