ec1117

Untitled

Jun 28th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. int[][] board=new int[3][3];
  2. int cols = 3;
  3. int rows = 3;
  4. boolean turn=true;
  5. void setup() {
  6. size(300, 300);
  7. background(255);
  8. line(width/3,0,width/3,width);
  9. line(2*width/3,0,2*width/3,width);
  10. line(0,width/3,width,width/3);
  11. line(0,2*width/3,width,2*width/3);
  12. board[2][2]=1;
  13. }
  14.  
  15. void draw() {
  16. for (int i = 0; i < cols; i++) {
  17. for (int j = 0; j < rows; j++) {
  18. if(board[i][j]==1){
  19. line(width/12+width/3*i,width/12+width/3*j,width/4+width/3*i,width/4+width/3*j);
  20. line(width/4+width/3*i,width/12+width/3*j,width/12+width/3*i,width/4+width/3*j);
  21. }
  22. if(board[i][j]==-1){
  23. circle(width/6+width/3*i,width/6+width/3*j,width/6);
  24. }
  25. }
  26. }
  27. }
  28.  
  29. void mousePressed() {
  30. if (turn) {
  31. int i=mouseX/(width/3);
  32. int j=mouseY/(width/3);
  33. if(board[i][j]==0){
  34. board[i][j]=-1;
  35. turn=false;
  36. float max=-101;
  37. int ni=-1;
  38. int nj=-1;
  39. int[][]board2=new int[3][3];
  40. for(int a=0;a<3;a++){
  41. for(int b=0;b<3;b++){
  42. board2[a][b]=board[a][b];
  43. }
  44. }
  45. for(int a=0;a<3;a++){
  46. for(int b=0;b<3;b++){
  47. if(board[a][b]==0){
  48. board2[a][b]=1;
  49. float score=score(board2,true);
  50. println(score+" "+a+" "+b);
  51. if(score>max){
  52. max=score;
  53. ni=a;
  54. nj=b;
  55. }
  56. board2[a][b]=0;
  57. }
  58. }
  59. }
  60. println("next"+ni+" "+nj);
  61. board[ni][nj]=1;
  62. turn=true;
  63. }
  64. }
  65. }
  66.  
  67. float score(int board[][],boolean isAI){
  68. if(isLost(board) && isAI)return -100;
  69. if(isLost(board) && !isAI)return 100;
  70. float sum=0;
  71. float count=0;
  72. for(int i=0;i<3;i++){
  73. for(int j=0;j<3;j++){
  74. if(board[i][j]==0){
  75. if(isAI){
  76. sum=Math.min(sum,score(change(board,i,j),!isAI));
  77. }
  78. else{
  79. sum=Math.max(sum,score(change(board,i,j),!isAI));
  80. }
  81. count++;
  82. }
  83. }
  84. }
  85. //println(sum+" "+count+" "+isAI);
  86. //for(int i=0;i<3;i++){
  87.  
  88. // for(int j=0;j<3;j++){
  89. // print(board[i][j]+" ");
  90. // }
  91. // println();
  92. //}
  93. if(count==0)return 0;//draw
  94. return sum;
  95. }
  96.  
  97. boolean isLost(int board[][]){
  98. if(board[0][0]==-1 && board[0][1]==-1 && board[0][2]==-1)return true;
  99. if(board[1][0]==-1 && board[1][1]==-1 && board[1][2]==-1)return true;
  100. if(board[2][0]==-1 && board[2][1]==-1 && board[2][2]==-1)return true;
  101. if(board[0][0]==-1 && board[1][0]==-1 && board[2][0]==-1)return true;
  102. if(board[0][1]==-1 && board[1][1]==-1 && board[2][1]==-1)return true;
  103. if(board[0][2]==-1 && board[1][2]==-1 && board[2][2]==-1)return true;
  104. if(board[0][0]==-1 && board[1][1]==-1 && board[2][2]==-1)return true;
  105. if(board[0][2]==-1 && board[1][1]==-1 && board[2][0]==-1)return true;
  106. return false;
  107. }
  108.  
  109. int[][] change(int board[][], int a, int b){
  110. int ret[][]=new int[3][3];
  111. for(int i=0;i<3;i++){
  112. for(int j=0;j<3;j++){
  113. ret[i][j]=-1*board[i][j];
  114. }
  115. }
  116. if(board[a][b]==0){
  117. ret[a][b]=1;
  118. }
  119. return ret;
  120. }
Add Comment
Please, Sign In to add comment