Advertisement
albertoanggi

Untitled

Oct 24th, 2018
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. private int minimax(bool flag)
  2.         {
  3.             int max_val=-1000,min_val=1000;
  4.             int i,j,value = 1;
  5.             if(checkVictory(computer) == 1)
  6.             {
  7.                 return 10;
  8.             }
  9.             else if(checkVictory(human) == 1)
  10.             {
  11.                 return -10;
  12.             }
  13.             else if(isFull()== 1)
  14.             {
  15.                 return 0;
  16.             }
  17.             int[] score = {1,1,1,1,1,1,1,1,1};//if score[i]=1 then it is empty
  18.        
  19.             for(i=0;i<9;i++)
  20.             {
  21.                 if(oriBoard[i] == "*")
  22.                 {
  23.                     if(min_val>max_val) // reverse of pruning condition.....
  24.                     {
  25.                         if(flag == true)
  26.                         {
  27.                             oriBoard[i] = "X";
  28.                             value = minimax(false);
  29.                         }
  30.                         else
  31.                         {
  32.                             oriBoard[i] = "O";
  33.                             value = minimax(true);
  34.                         }
  35.                         oriBoard[i] = "*";
  36.                         score[i] = value;
  37.                     }
  38.                 }
  39.             }
  40.             if(flag == true)
  41.             {
  42.                 max_val = -1000;
  43.                 for(j=0;j<9;j++)
  44.                 {
  45.                     if(score[j] > max_val && score[j] != 1)
  46.                     {
  47.                         max_val = score[j];
  48.                         bestMove = j;
  49.                     }
  50.                 }
  51.                 return max_val;
  52.             }
  53.             if(flag == false)
  54.             {
  55.                 min_val = 1000;
  56.                 for(j=0;j<9;j++)
  57.                 {
  58.                     if(score[j] < min_val && score[j] != 1)
  59.                     {
  60.                         min_val = score[j];
  61.                         bestMove = j;
  62.                     }
  63.                 }
  64.                 return min_val;
  65.             }
  66.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement