当前位置:   article > 正文

C++ AI五子棋_c++五子棋ai算法

c++五子棋ai算法

 AI五子棋

WASD移动;

空格下棋;

 

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <conio.h>
  4. #include <iostream>
  5. #include <cstring>
  6. using namespace std;
  7. #define Forij(x) for(int i=1;i<=x;i++)for(int j=1;j<=x;j++)
  8. #define N 25
  9. typedef long long LL;
  10. LL fx[4][2]= {{1,1},{1,0},{0,1},{1,-1}};
  11. LL Q,GG;
  12. string C[20]= {"●","○","﹢","═","║","╔","╚","╗","╝","?"}; //╋
  13. void color(LL a) //颜色函数
  14. {
  15. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
  16. }
  17. void gotoxy(LL x,LL y)
  18. {
  19. COORD pos;
  20. pos.X=2*x;
  21. pos.Y=y;
  22. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
  23. }
  24. struct Gomoku
  25. {
  26. LL m[50][50],nx,ny;
  27. void reset()
  28. {
  29. system("cls");
  30. memset(m,-1,sizeof(m));
  31. color(7);
  32. for (LL i=1; i<=N; i++)
  33. {
  34. gotoxy(0,i);
  35. cout<<C[4];
  36. gotoxy(N+1,i);
  37. cout<<C[4];
  38. gotoxy(i,0);
  39. cout<<C[3];
  40. gotoxy(i,N+1);
  41. cout<<C[3];
  42. }
  43. gotoxy(0,0);
  44. cout<<C[5];
  45. gotoxy(0,N+1);
  46. cout<<C[6];
  47. gotoxy(N+1,0);
  48. cout<<C[7];
  49. gotoxy(N+1,N+1);
  50. cout<<C[8];
  51. color(3);
  52. Forij(N)
  53. {
  54. gotoxy(i,j);
  55. cout<<C[2];
  56. }
  57. nx=ny=N/2+1;
  58. gotoxy(nx,ny);
  59. }
  60. void _drop(LL x,LL i,LL j)
  61. {
  62. m[i][j]=x;
  63. gotoxy(i,j);
  64. color(15);
  65. cout<<C[x];
  66. }
  67. LL check()
  68. {
  69. Forij(N)
  70. {
  71. for (LL Fx=0,tmp,lst,xx,yy; Fx<4; Fx++) if(m[i][j]!=-1)
  72. {
  73. xx=i,yy=j,tmp=0,lst=m[i][j];
  74. for (LL k=1; k<=5; k++)
  75. {
  76. if (xx>N || yy>N) break;
  77. if (m[xx][yy]==(lst^1))
  78. {
  79. break;
  80. }
  81. if (m[xx][yy]==lst) tmp++;
  82. xx+=fx[Fx][0],yy+=fx[Fx][1];
  83. }
  84. if (tmp==5)
  85. {
  86. return lst;
  87. }
  88. }
  89. }
  90. return -1;
  91. }
  92. LL arnd(LL x,LL y)
  93. {
  94. LL cnt=0;
  95. for (LL i=x-1; i<=x+1; i++) if (i>0 && i<=N)
  96. for (LL j=y-1; j<=y+1; j++) if (j>0 && j<=N)
  97. if (m[i][j]>-1) cnt++;
  98. return cnt;
  99. }
  100. void get_val(LL x,LL y,LL &val)
  101. {
  102. val=0;
  103. Forij(N)
  104. {
  105. for (LL Fx=0,tmp,tk,xx,yy; Fx<4; Fx++)
  106. {
  107. xx=i,yy=j,tmp=tk=0;
  108. for (LL k=1; k<=5; k++)
  109. {
  110. if (xx>N || yy>N)
  111. {
  112. tmp=0;
  113. break;
  114. }
  115. if (m[xx][yy]==(x^1))
  116. {
  117. tmp=0;
  118. break;
  119. }
  120. if (m[xx][yy]==x) tmp++,tk+=(1<<(k-1));
  121. xx+=fx[Fx][0],yy+=fx[Fx][1];
  122. }
  123. switch(tmp)
  124. {
  125. case 5:
  126. val+=8000000000;
  127. break;
  128. case 4:
  129. val+=1000+350*y;
  130. break;
  131. case 3:
  132. val+=(tk==14)?(300+600*y):(300+200*y);
  133. break;
  134. case 2:
  135. val+=3+2*y;
  136. break;
  137. case 1:
  138. val+=1+y;
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. void AI(LL x)
  145. {
  146. LL best,brnd,bi,bj,v1,v2,kkk;
  147. best=-2147483647;
  148. brnd=-2147483647;
  149. Forij(N) if (m[i][j]==-1)
  150. {
  151. m[i][j]=x;
  152. get_val(x,10,v1); //gotoxy(N+5,N/2);printf("%d ",v1);
  153. get_val(x^1,80,v2); //gotoxy(N+5,N/2+1);printf("%d ",v2);
  154. if (v1-v2>best) bi=i,bj=j,best=v1-v2;
  155. if (v1-v2==best)
  156. if ((kkk=arnd(i,j))>brnd)
  157. brnd=kkk,bi=i,bj=j;
  158. m[i][j]=-1;
  159. }
  160. _drop(x,bi,bj);
  161. }
  162. void HM(LL x)
  163. {
  164. char ch=getch();
  165. for (;; ch=getch())
  166. {
  167. if (ch=='w')
  168. {
  169. if (ny>1) ny--;
  170. }
  171. else if (ch=='s')
  172. {
  173. if (ny<N) ny++;
  174. }
  175. else if (ch=='a')
  176. {
  177. if (nx>1) nx--;
  178. }
  179. else if (ch=='d')
  180. {
  181. if (nx<N)nx++;
  182. }
  183. else if (m[nx][ny]==-1)
  184. {
  185. _drop(x,nx,ny);
  186. return;
  187. }
  188. gotoxy(nx,ny);
  189. }
  190. }
  191. } A;
  192. int main()
  193. {
  194. for (;;)
  195. {
  196. A.reset();
  197. for (GG=-1;;)
  198. {
  199. gotoxy(A.nx,A.ny);
  200. A.HM(0);
  201. GG=A.check();
  202. if (GG>-1) break;
  203. A.AI(1);
  204. GG=A.check();
  205. if (GG>-1) break;
  206. }
  207. gotoxy(5,N+3);
  208. if (GG==0) printf("AI_1 win!");
  209. if (GG==1) printf("AI_2 wins!");
  210. while (kbhit()) getch();
  211. gotoxy(5,N+3);
  212. printf("Press anything to continue.");
  213. getch();
  214. }
  215. }

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

闽ICP备14008679号