当前位置:   article > 正文

【C语言】三子棋实现AI智能落子(简单语法)_c语言落子算法

c语言落子算法

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


提示:以下是本篇文章正文内容,下面案例可供参考

一、实现逻辑

  • 落子优先级(叙述为电脑视角)
    • 一手占中,若中心被占则占角(默认为电脑后手)
    • 有三连机会,尝试三连
    • 玩家有三连机会,进行拦截
    • 靠近己方棋子落子
  • 实现方法均为基础的循环和判断语句,并且适用于不同规格的棋盘
  • 基本逻辑:判断同行、同列、正副对角线是否有两棋相同,且可能三连的位置为空


    • 以同列且两棋不相邻为例(为确保一般性,这里使用4x4棋盘)。
    • 棋盘中实心红点为判断起始点(if语句的参考点)的所有可能位置,右侧为三连机会示例,中心为可能存在的制胜棋位置

     其余情况均为此思考模式,注意讨论对角线时需区分正副对角线

二、实现代码

1.一手占中,若中心被占则占角

代码如下:

  1. //电脑第一步
  2. void FirstMove(char board[ROW][COL], int row, int col)
  3. {
  4. int ar1[2] = { 0, row - 1 };
  5. int ar2[2] = { 0, col - 1 };
  6. int x = ar1[rand() % (row - 1)], y = ar2[rand() % (col - 1)];
  7. //判断中心点是否被占,玩家先中则不走边
  8. if (board[row / 2][col / 2] == '*')
  9. {
  10. while (1)
  11. {
  12. if (board[x][y] == ' ')
  13. {
  14. board[x][y] = '#';
  15. break;
  16. }
  17. }
  18. }
  19. //反则占中心
  20. else
  21. board[row / 2][col / 2] = '#';
  22. }

2.有三连机会,尝试三连

代码如下:

  1. //电脑三连机会,三连成功返回0,反之返回1
  2. int ComputerWin(char board[ROW][COL], int row, int col)
  3. {
  4. //1.同一行
  5. int i = 0, j = 0;
  6. //1.1一行相同两枚棋子相邻,判断参考点起点[0][0],终点[row-1][col-2]
  7. for (i = 0; i < row; i++)
  8. {
  9. for (j = 0; j < col - 1; j++)
  10. {
  11. if (board[i][j] == board[i][j + 1] && board[i][j] == '#')
  12. {
  13. if (j < col - 2 && board[i][j + 2] == ' ')
  14. {
  15. board[i][j + 2] = '#';
  16. return 0;
  17. }
  18. else
  19. if (board[i][j - 1] == ' ')
  20. {
  21. board[i][j - 1] = '#';
  22. return 0;
  23. }
  24. }
  25. }
  26. }
  27. //1.2一行相同两枚棋子不相邻,判断参考点起点[0][0],终点[row-1][col-3]
  28. for (i = 0; i < row; i++)
  29. {
  30. for (j = 0; j < col - 2; j++)
  31. {
  32. if (board[i][j] == board[i][j + 2] && board[i][j] == '#' )
  33. {
  34. if (board[i][j + 1] == ' ')
  35. {
  36. board[i][j + 1] = '#';
  37. return 0;
  38. }
  39. }
  40. }
  41. }
  42. //2.同一列
  43. //2.1一列相同两枚棋子相邻,判断参考点起点[0][0],终点[col - 2][row - 1]
  44. for (i = 0; i < row - 1; i++)
  45. {
  46. for (j = 0; j < col; j++)
  47. {
  48. if (board[i][j] == board[i + 1][j] && board[i][j] == '#')
  49. {
  50. if (i < col - 2 && board[i + 2][j] == ' ')
  51. {
  52. board[i + 2][j] = '#';
  53. return 0;
  54. }
  55. else
  56. if (board[i - 1][j] == ' ')
  57. {
  58. board[i - 1][j] = '#';
  59. return 0;
  60. }
  61. }
  62. }
  63. }
  64. //2.2一列相同两枚棋子不相邻,判断参考点起点[0][0],终点[col - 3][row - 1]
  65. for (i = 0; i < row - 2; i++)
  66. {
  67. for (j = 0; j < col; j++)
  68. {
  69. if (board[i][j] == board[i + 2][j] && board[i][j] == '#' && board[i + 1][j] == ' ')
  70. {
  71. board[i + 1][j] = '#';
  72. return 0;
  73. }
  74. }
  75. }
  76. //3.拦截对角线
  77. //3.1正对角线
  78. for (i = 0; i < row - 2; i++)
  79. {
  80. for (j = 0; j < col - 2; j++)
  81. {
  82. if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '#')
  83. {
  84. if (board[i + 2][j + 2] == ' ')
  85. {
  86. board[i + 2][j + 2] = '#';
  87. return 0;
  88. }
  89. }
  90. if (board[i][j] == board[i + 2][j + 2] && board[i][j] == '#')
  91. {
  92. if (board[i + 1][j + 1] == ' ')
  93. {
  94. board[i + 1][j + 1] = '#';
  95. return 0;
  96. }
  97. }
  98. }
  99. }
  100. for (i = 1; i < row - 1; i++)
  101. {
  102. for (j = 1; j < col - 1; j++)
  103. {
  104. if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '#')
  105. {
  106. if (board[i - 1][j - 1] == ' ')
  107. {
  108. board[i - 1][j - 1] = '#';
  109. return 0;
  110. }
  111. }
  112. }
  113. }
  114. //3.2副对角线
  115. for (i = 2; i < row; i++)
  116. {
  117. for (j = 0; j < col - 1; j++)
  118. {
  119. if (board[i][j] == board[i - 1][j + 1] && board[i][j] == '#')
  120. {
  121. if (board[i - 2][j + 2] == ' ')
  122. {
  123. board[i - 2][j + 2] = '#';
  124. return 0;
  125. }
  126. }
  127. if (board[i][j] == board[i - 2][j + 2] && board[i][j] == '#')
  128. {
  129. if (board[i - 1][j + 1] == ' ')
  130. {
  131. board[i - 1][j + 1] = '#';
  132. return 0;
  133. }
  134. }
  135. }
  136. }
  137. for (i = 1; i < row - 1; i++)
  138. {
  139. for (j = 1; j < col - 1; j++)
  140. {
  141. if (board[i][j] == board[i - 1][j + 1] && board[i][j] == '#')
  142. {
  143. if (board[i + 1][j - 1] == ' ')
  144. {
  145. board[i + 1][j - 1] = '#';
  146. return 0;
  147. }
  148. }
  149. }
  150. }
  151. return 1;
  152. }

3.玩家有三连机会,进行拦截

首先判断玩家是否存在三连机会,代码如下:

  1. //判断是否需要拦截,需要即返回1,反之返回0
  2. int IsDanger(char board[ROW][COL], int row, int col)
  3. {
  4. int i = 0, j = 0;
  5. //1.同一行三连
  6. //1.1一行相同两枚棋子相邻,判断参考点起点[0][0],终点[row-1][col-2]
  7. for (i = 0; i < row; i++)
  8. {
  9. for (j = 0; j < col - 1; j++)
  10. {
  11. if (board[i][j] == board[i][j + 1] && board[i][j] == '*')
  12. {
  13. if (j < col - 2 && board[i][j + 2] == ' ')
  14. {
  15. return 1;
  16. }
  17. else
  18. if (board[i][j - 1] == ' ')
  19. {
  20. return 1;
  21. }
  22. }
  23. }
  24. }
  25. //1.2一行相同两枚棋子不相邻,判断参考点起点[0][0],终点[row-1][col-3]
  26. for (i = 0; i < row; i++)
  27. {
  28. for (j = 0; j < col - 2; j++)
  29. {
  30. if (board[i][j] == board[i][j + 2] && board[i][j] == '*' && board[i][j + 1] == ' ')
  31. {
  32. return 1;
  33. }
  34. }
  35. }
  36. //2.同一列三连
  37. //2.1一列相同两枚棋子相邻,判断参考点起点[0][0],终点[col - 2][row - 1]
  38. for (i = 0; i < row - 1; i++)
  39. {
  40. for (j = 0; j < col; j++)
  41. {
  42. if (board[i][j] == board[i + 1][j] && board[i][j] == '*')
  43. {
  44. if (i < col - 2 && board[i + 2][j] == ' ')
  45. {
  46. return 1;
  47. }
  48. else
  49. if (board[i - 1][j] == ' ')
  50. {
  51. return 1;
  52. }
  53. }
  54. }
  55. }
  56. //2.2一列相同两枚棋子不相邻,判断参考点起点[0][0],终点[col - 3][row - 1]
  57. for (i = 0; i < row - 2; i++)
  58. {
  59. for (j = 0; j < col; j++)
  60. {
  61. if (board[i][j] == board[i + 2][j] && board[i][j] == '*' && board[i + 1][j] == ' ')
  62. {
  63. return 1;
  64. }
  65. }
  66. }
  67. //3.对角线
  68. //3.1正对角线
  69. for (i = 0; i < row - 2; i++)
  70. {
  71. for (j = 0; j < col - 2; j++)
  72. {
  73. if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '*')
  74. {
  75. if (board[i + 2][j + 2] == ' ')
  76. {
  77. return 1;
  78. }
  79. }
  80. if (board[i][j] == board[i + 2][j + 2] && board[i][j] == '*')
  81. {
  82. if (board[i + 1][j + 1] == ' ')
  83. {
  84. return 1;
  85. }
  86. }
  87. }
  88. }
  89. for (i = 1; i < row - 1; i++)
  90. {
  91. for (j = 1; j < col - 1; j++)
  92. {
  93. if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '*')
  94. {
  95. if (board[i - 1][j - 1] == ' ')
  96. {
  97. return 1;
  98. }
  99. }
  100. }
  101. }
  102. //3.2副对角线
  103. for (i = 2; i < row; i++)
  104. {
  105. for (j = 0; j < col - 1; j++)
  106. {
  107. if (board[i][j] == board[i - 1][j + 1] && board[i][j] == '*')
  108. {
  109. if (board[i - 2][j + 2] == ' ')
  110. {
  111. return 1;
  112. }
  113. }
  114. if (board[i][j] == board[i - 2][j + 2] && board[i][j] == '*')
  115. {
  116. if (board[i - 1][j + 1] == ' ')
  117. {
  118. return 1;
  119. }
  120. }
  121. }
  122. }
  123. for (i = 1; i < row - 1; i++)
  124. {
  125. for (j = 1; j < col - 1; j++)
  126. {
  127. if (board[i][j] == board[i - 1][j + 1] && board[i][j] == '*')
  128. {
  129. if (board[i + 1][j - 1] == ' ')
  130. {
  131. return 1;
  132. }
  133. }
  134. }
  135. }
  136. return 0;
  137. }

然后进行拦截,代码如下:

  1. //拦截玩家函数
  2. int BlockPlayer(char board[ROW][COL], int row, int col)
  3. {
  4. //1.同一行
  5. int i = 0, j = 0;
  6. //1.1一行相同两枚棋子相邻,判断参考点起点[0][0],终点[row-1][col-2]
  7. for (i = 0; i < row; i++)
  8. {
  9. for (j = 0; j < col-1; j++)
  10. {
  11. if (board[i][j] == board[i][j + 1] && board[i][j] == '*')
  12. {
  13. if (j < col - 2 && board[i][j + 2] == ' ')
  14. {
  15. board[i][j + 2] = '#';
  16. return 0;
  17. }
  18. else
  19. if (board[i][j - 1] == ' ')
  20. {
  21. board[i][j - 1] = '#';
  22. return 0;
  23. }
  24. }
  25. }
  26. }
  27. //1.2一行相同两枚棋子不相邻,判断参考点起点[0][0],终点[row-1][col-3]
  28. for (i = 0; i < row; i++)
  29. {
  30. for (j = 0; j < col - 2; j++)
  31. {
  32. if (board[i][j] == board[i][j + 2] && board[i][j] == '*' && board[i][j + 1] == ' ')
  33. {
  34. board[i][j + 1] = '#';
  35. return 0;
  36. }
  37. }
  38. }
  39. //2.同一列
  40. //2.1一列相同两枚棋子相邻,判断参考点起点[0][0],终点[col - 2][row - 1]
  41. for (i = 0; i < row - 1; i++)
  42. {
  43. for (j = 0; j < col; j++)
  44. {
  45. if (board[i][j] == board[i + 1][j] && board[i][j] == '*')
  46. {
  47. if (i < col - 2 && board[i + 2][j] == ' ')
  48. {
  49. board[i + 2][j] = '#';
  50. return 0;
  51. }
  52. else
  53. if (board[i - 1][j] == ' ')
  54. {
  55. board[i - 1][j] = '#';
  56. return 0;
  57. }
  58. }
  59. }
  60. }
  61. //2.2一列相同两枚棋子不相邻,判断参考点起点[0][0],终点[col - 3][row - 1]
  62. for (i = 0; i < row - 2; i++)
  63. {
  64. for (j = 0; j < col; j++)
  65. {
  66. if (board[i][j] == board[i + 2][j] && board[i][j] == '*' && board[i + 1][j] == ' ')
  67. {
  68. board[i + 1][j] = '#';
  69. return 0;
  70. }
  71. }
  72. }
  73. //3.对角线
  74. //3.1正对角线
  75. for (i = 0; i < row - 2; i++)
  76. {
  77. for (j = 0; j < col - 2; j++)
  78. {
  79. if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '*')
  80. {
  81. if (board[i + 2][j + 2] == ' ')
  82. {
  83. board[i + 2][j + 2] = '#';
  84. return 0;
  85. }
  86. }
  87. if (board[i][j] == board[i + 2][j + 2] && board[i][j] == '*')
  88. {
  89. if (board[i + 1][j + 1] == ' ')
  90. {
  91. board[i + 1][j + 1] = '#';
  92. return 0;
  93. }
  94. }
  95. }
  96. }
  97. for (i = 1; i < row - 1; i++)
  98. {
  99. for (j = 1; j < col - 1; j++)
  100. {
  101. if (board[i][j] == board[i + 1][j + 1] && board[i][j] == '*')
  102. {
  103. if (board[i - 1][j - 1] == ' ')
  104. {
  105. board[i - 1][j - 1] = '#';
  106. return 0;
  107. }
  108. }
  109. }
  110. }
  111. //3.2副对角线
  112. for (i = 2; i < row; i++)
  113. {
  114. for (j = 0; j < col - 1; j++)
  115. {
  116. if (board[i][j] == board[i - 1][j + 1] && board[i][j] == '*')
  117. {
  118. if (board[i - 2][j + 2] == ' ')
  119. {
  120. board[i - 2][j + 2] = '#';
  121. return 0;
  122. }
  123. }
  124. if (board[i][j] == board[i - 2][j + 2] && board[i][j] == '*')
  125. {
  126. if (board[i - 1][j + 1] == ' ')
  127. {
  128. board[i - 1][j + 1] = '#';
  129. return 0;
  130. }
  131. }
  132. }
  133. }
  134. for (i = 1; i < row - 1; i++)
  135. {
  136. for (j = 1; j < col - 1; j++)
  137. {
  138. if (board[i][j] == board[i - 1][j + 1] && board[i][j] == '*')
  139. {
  140. if (board[i + 1][j - 1] == ' ')
  141. {
  142. board[i + 1][j - 1] = '#';
  143. return 0;
  144. }
  145. }
  146. }
  147. }
  148. return 1;
  149. }

4.无三连机会,且无需拦截,则靠近己方棋子落子,创造三连机会

代码如下:

  1. //无三连机会,无需拦截,自行制造机会
  2. int CreatWin(char board[ROW][COL], int row, int col)
  3. {
  4. int i = 0, j = 0;
  5. int a = 0, b = 0;
  6. for (i = 0; i < row; i++)
  7. {
  8. for (j = 0; j < col; j++)
  9. {
  10. if (board[i][j] == '#')
  11. {
  12. for (a = 0; a < row; a++)
  13. {
  14. for (b = 0; b < col; b++)
  15. {
  16. if ((a - i == -1 && b - j == -1)
  17. || (a - i == -1 && b - j == 0)
  18. || (a - i == -1 && b - j == 1)
  19. || (a - i == 0 && b - j == -1)
  20. || (a - i == 0 && b - j == 1)
  21. || (a - i == 1 && b - j == -1)
  22. || (a - i == 1 && b - j == 0)
  23. || (a - i == 1 && b - j == 1))
  24. {
  25. if (board[a][b] == ' ')
  26. {
  27. board[a][b] = '#';
  28. return 0;
  29. }
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }
  36. }

总结

1-3步均使用一套判断模式,第4步可应用在扫雷中的排查雷的步骤中。

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

闽ICP备14008679号