Ai.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. //下棋业务核心类,与界面棋盘对应,业务放在这里,可以和界面代码分离
  2. import 'dart:core';
  3. import 'dart:core';
  4. import 'package:gobang/flyweight/ChessFlyweightFactory.dart';
  5. import 'package:gobang/flyweight/Position.dart';
  6. class Ai{
  7. Ai._();
  8. static Ai? _ai;
  9. static Ai getInstance(){
  10. if(_ai==null){
  11. _ai = Ai._();
  12. }
  13. return _ai!;
  14. }
  15. static var CHESSBOARD_SIZE = 15;
  16. static var FIRST = 1;//先手,-1表示机器,1表示人类,与Position类中的对应
  17. var chessboard = List.generate(CHESSBOARD_SIZE, (i) => List.filled(CHESSBOARD_SIZE, 0, growable: false), growable: false);//与界面棋盘对应,0代表空,-1代表机器,1代表人类
  18. var score = List.generate(CHESSBOARD_SIZE, (i) => List.filled(CHESSBOARD_SIZE, 0, growable: false), growable: false);//每个位置得分
  19. void init(){
  20. FIRST = 1;//默认人类先手
  21. for(int i = 0; i < CHESSBOARD_SIZE; i++){
  22. for(int j = 0; j < CHESSBOARD_SIZE; j++){
  23. chessboard[i][j] = 0;
  24. score[i][j] = 0;
  25. }
  26. }
  27. }
  28. //落子
  29. void addChessman(int x, int y, int owner){
  30. chessboard[x][y] = owner;
  31. print("$x,$y,$owner");
  32. }
  33. //判断落子位置是否合法
  34. bool isLegal(int x, int y){
  35. if(x >=0 && x < CHESSBOARD_SIZE && y >= 0 && y < CHESSBOARD_SIZE && chessboard[x][y] == 0){
  36. return true;
  37. }
  38. return false;
  39. }
  40. //判断哪方赢了(必定有刚落的子引发,因此只需判断刚落子的周围),owner为-1代表机器,owner为1代表人类
  41. bool isWin(int x, int y, int owner){
  42. int sum = 0;
  43. //判断横向左边
  44. for(int i = x - 1; i >= 0; i--){
  45. if(chessboard[i][y] == owner){sum++;}
  46. else{break;}
  47. }
  48. //判断横向右边
  49. for(int i = x + 1; i < CHESSBOARD_SIZE; i++){
  50. if(chessboard[i][y] == owner){sum++;}
  51. else{break;}
  52. }
  53. if(sum >= 4) {return true;}
  54. sum = 0;
  55. //判断纵向上边
  56. for(int i = y - 1; i >= 0; i--){
  57. if(chessboard[x][i] == owner){sum++;}
  58. else{break;}
  59. }
  60. //判断纵向下边
  61. for(int i = y + 1; i < CHESSBOARD_SIZE; i++){
  62. if(chessboard[x][i] == owner){sum++;}
  63. else{break;}
  64. }
  65. if(sum >= 4) {return true;}
  66. sum = 0;
  67. //判断左上角到右下角方向上侧
  68. for(int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j-- ){
  69. if(chessboard[i][j] == owner){sum++;}
  70. else{break;}
  71. }
  72. //判断左上角到右下角方向下侧
  73. for(int i = x + 1, j = y + 1; i < CHESSBOARD_SIZE && j < CHESSBOARD_SIZE; i++, j++ ){
  74. if(chessboard[i][j] == owner){sum++;}
  75. else{break;}
  76. }
  77. if(sum >= 4) {return true;}
  78. sum = 0;
  79. //判断右上角到左下角方向上侧
  80. for(int i = x + 1, j = y - 1; i < CHESSBOARD_SIZE && j >= 0; i++, j-- ){
  81. if(chessboard[i][j] == owner){sum++;}
  82. else{break;}
  83. }
  84. //判断右上角到左下角方向下侧
  85. for(int i = x - 1, j = y + 1; i >= 0 && j < CHESSBOARD_SIZE; i--, j++ ){
  86. if(chessboard[i][j] == owner){sum++;}
  87. else{break;}
  88. }
  89. if(sum >= 4) {return true;}
  90. return false;
  91. }
  92. //【【【【【*******整个游戏的核心*******】】】】】______确定机器落子位置
  93. //使用五元组评分算法,该算法参考博客地址:https://blog.csdn.net/u011587401/article/details/50877828
  94. //算法思路:对15X15的572个五元组分别评分,一个五元组的得分就是该五元组为其中每个位置贡献的分数,
  95. // 一个位置的分数就是其所在所有五元组分数之和。所有空位置中分数最高的那个位置就是落子位置。
  96. Position searchPosition(){
  97. //每次都初始化下score评分数组
  98. for(int i = 0; i < CHESSBOARD_SIZE; i++){
  99. for(int j = 0; j < CHESSBOARD_SIZE; j++){
  100. score[i][j] = 0;
  101. }
  102. }
  103. //每次机器找寻落子位置,评分都重新算一遍(虽然算了很多多余的,因为上次落子时候算的大多都没变)
  104. //先定义一些变量
  105. int humanChessmanNum = 0;//五元组中的黑棋数量
  106. int machineChessmanNum = 0;//五元组中的白棋数量
  107. int tupleScoreTmp = 0;//五元组得分临时变量
  108. int goalX = -1;//目标位置x坐标
  109. int goalY = -1;//目标位置y坐标
  110. int maxScore = -1;//最大分数
  111. //1.扫描横向的15个行
  112. for(int i = 0; i < 15; i++){
  113. for(int j = 0; j < 11; j++){
  114. int k = j;
  115. while(k < j + 5){
  116. if(chessboard[i][k] == -1) machineChessmanNum++;
  117. else if(chessboard[i][k] == 1)humanChessmanNum++;
  118. k++;
  119. }
  120. tupleScoreTmp = tupleScore(humanChessmanNum, machineChessmanNum);
  121. //为该五元组的每个位置添加分数
  122. for(k = j; k < j + 5; k++){
  123. score[i][k] += tupleScoreTmp;
  124. }
  125. //置零
  126. humanChessmanNum = 0;//五元组中的黑棋数量
  127. machineChessmanNum = 0;//五元组中的白棋数量
  128. tupleScoreTmp = 0;//五元组得分临时变量
  129. }
  130. }
  131. //2.扫描纵向15行
  132. for(int i = 0; i < 15; i++){
  133. for(int j = 0; j < 11; j++){
  134. int k = j;
  135. while(k < j + 5){
  136. if(chessboard[k][i] == -1) machineChessmanNum++;
  137. else if(chessboard[k][i] == 1)humanChessmanNum++;
  138. k++;
  139. }
  140. tupleScoreTmp = tupleScore(humanChessmanNum, machineChessmanNum);
  141. //为该五元组的每个位置添加分数
  142. for(k = j; k < j + 5; k++){
  143. score[k][i] += tupleScoreTmp;
  144. }
  145. //置零
  146. humanChessmanNum = 0;//五元组中的黑棋数量
  147. machineChessmanNum = 0;//五元组中的白棋数量
  148. tupleScoreTmp = 0;//五元组得分临时变量
  149. }
  150. }
  151. //3.扫描右上角到左下角上侧部分
  152. for(int i = 14; i >= 4; i--){
  153. for(int k = i, j = 0; j < 15 && k >= 0; j++, k--){
  154. int m = k;
  155. int n = j;
  156. while(m > k - 5 && k - 5 >= -1){
  157. if(chessboard[m][n] == -1) machineChessmanNum++;
  158. else if(chessboard[m][n] == 1)humanChessmanNum++;
  159. m--;
  160. n++;
  161. }
  162. //注意斜向判断的时候,可能构不成五元组(靠近四个角落),遇到这种情况要忽略掉
  163. if(m == k-5){
  164. tupleScoreTmp = tupleScore(humanChessmanNum, machineChessmanNum);
  165. //为该五元组的每个位置添加分数
  166. m = k;
  167. n = j;
  168. for(; m > k - 5 ; m--, n++){
  169. score[m][n] += tupleScoreTmp;
  170. }
  171. }
  172. //置零
  173. humanChessmanNum = 0;//五元组中的黑棋数量
  174. machineChessmanNum = 0;//五元组中的白棋数量
  175. tupleScoreTmp = 0;//五元组得分临时变量
  176. }
  177. }
  178. //4.扫描右上角到左下角下侧部分
  179. for(int i = 1; i < 15; i++){
  180. for(int k = i, j = 14; j >= 0 && k < 15; j--, k++){
  181. int m = k;
  182. int n = j;
  183. while(m < k + 5 && k + 5 <= 15){
  184. if(chessboard[n][m] == -1) machineChessmanNum++;
  185. else if(chessboard[n][m] == 1)humanChessmanNum++;
  186. m++;
  187. n--;
  188. }
  189. //注意斜向判断的时候,可能构不成五元组(靠近四个角落),遇到这种情况要忽略掉
  190. if(m == k+5){
  191. tupleScoreTmp = tupleScore(humanChessmanNum, machineChessmanNum);
  192. //为该五元组的每个位置添加分数
  193. m = k;
  194. n = j;
  195. for(; m < k + 5; m++, n--){
  196. score[n][m] += tupleScoreTmp;
  197. }
  198. }
  199. //置零
  200. humanChessmanNum = 0;//五元组中的黑棋数量
  201. machineChessmanNum = 0;//五元组中的白棋数量
  202. tupleScoreTmp = 0;//五元组得分临时变量
  203. }
  204. }
  205. //5.扫描左上角到右下角上侧部分
  206. for(int i = 0; i < 11; i++){
  207. for(int k = i, j = 0; j < 15 && k < 15; j++, k++){
  208. int m = k;
  209. int n = j;
  210. while(m < k + 5 && k + 5 <= 15){
  211. if(chessboard[m][n] == -1) machineChessmanNum++;
  212. else if(chessboard[m][n] == 1)humanChessmanNum++;
  213. m++;
  214. n++;
  215. }
  216. //注意斜向判断的时候,可能构不成五元组(靠近四个角落),遇到这种情况要忽略掉
  217. if(m == k + 5){
  218. tupleScoreTmp = tupleScore(humanChessmanNum, machineChessmanNum);
  219. //为该五元组的每个位置添加分数
  220. m = k;
  221. n = j;
  222. for(; m < k + 5; m++, n++){
  223. score[m][n] += tupleScoreTmp;
  224. }
  225. }
  226. //置零
  227. humanChessmanNum = 0;//五元组中的黑棋数量
  228. machineChessmanNum = 0;//五元组中的白棋数量
  229. tupleScoreTmp = 0;//五元组得分临时变量
  230. }
  231. }
  232. //6.扫描左上角到右下角下侧部分
  233. for(int i = 1; i < 11; i++){
  234. for(int k = i, j = 0; j < 15 && k < 15; j++, k++){
  235. int m = k;
  236. int n = j;
  237. while(m < k + 5 && k + 5 <= 15){
  238. if(chessboard[n][m] == -1) machineChessmanNum++;
  239. else if(chessboard[n][m] == 1)humanChessmanNum++;
  240. m++;
  241. n++;
  242. }
  243. //注意斜向判断的时候,可能构不成五元组(靠近四个角落),遇到这种情况要忽略掉
  244. if(m == k + 5){
  245. tupleScoreTmp = tupleScore(humanChessmanNum, machineChessmanNum);
  246. //为该五元组的每个位置添加分数
  247. m = k;
  248. n = j;
  249. for(; m < k + 5; m++, n++){
  250. score[n][m] += tupleScoreTmp;
  251. }
  252. }
  253. //置零
  254. humanChessmanNum = 0;//五元组中的黑棋数量
  255. machineChessmanNum = 0;//五元组中的白棋数量
  256. tupleScoreTmp = 0;//五元组得分临时变量
  257. }
  258. }
  259. //从空位置中找到得分最大的位置
  260. for(int i = 0; i < 15; i++){
  261. for(int j = 0; j < 15; j++){
  262. if(chessboard[i][j] == 0 && score[i][j] > maxScore){
  263. goalX = i;
  264. goalY = j;
  265. maxScore = score[i][j];
  266. }
  267. }
  268. }
  269. print(maxScore);
  270. if(goalX != -1 && goalY != -1){
  271. return new Position(goalX.toDouble(), goalY.toDouble(), ChessFlyweightFactory.getInstance().getChess(""));
  272. }
  273. //没找到坐标说明平局了,笔者不处理平局
  274. print("没有找到");
  275. return new Position(-1, -1, ChessFlyweightFactory.getInstance().getChess(""));
  276. }
  277. //各种五元组情况评分表
  278. int tupleScore(int humanChessmanNum, int machineChessmanNum){
  279. //1.既有人类落子,又有机器落子,判分为0
  280. if(humanChessmanNum > 0 && machineChessmanNum > 0){
  281. return 0;
  282. }
  283. //2.全部为空,没有落子,判分为7
  284. if(humanChessmanNum == 0 && machineChessmanNum == 0){
  285. return 7;
  286. }
  287. //3.机器落1子,判分为35
  288. if(machineChessmanNum == 1){
  289. return 35;
  290. }
  291. //4.机器落2子,判分为800
  292. if(machineChessmanNum == 2){
  293. return 800;
  294. }
  295. //5.机器落3子,判分为15000
  296. if(machineChessmanNum == 3){
  297. return 15000;
  298. }
  299. //6.机器落4子,判分为800000
  300. if(machineChessmanNum == 4){
  301. return 800000;
  302. }
  303. //7.人类落1子,判分为15
  304. if(humanChessmanNum == 1){
  305. return 15;
  306. }
  307. //8.人类落2子,判分为400
  308. if(humanChessmanNum == 2){
  309. return 400;
  310. }
  311. //9.人类落3子,判分为1800
  312. if(humanChessmanNum == 3){
  313. return 1800;
  314. }
  315. //10.人类落4子,判分为100000
  316. if(humanChessmanNum == 4){
  317. return 100000;
  318. }
  319. return -1;//若是其他结果肯定出错了。这行代码根本不可能执行
  320. }
  321. }