Advertisement
Guest User

Untitled

a guest
Jan 30th, 2019
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. /*
  4.  
  5. 2.0
  6.  
  7. Minimax working
  8. Multiple matches without having to close the program
  9.  
  10. */
  11. int clearboard(int board[3][3])//Clears the board
  12. {
  13.    
  14.     int x,y;
  15.     for(x=0;x<3;x++)
  16.         {
  17.             for(y=0;y<3;y++)
  18.                 {
  19.                     board[x][y] = 0;
  20.                 }
  21.         }
  22. }
  23. int max(int board[3][3],int player)//Best sum for the player passed as parameter
  24. {
  25.    
  26.     int x,y,minscore,maxscore,tempscore;
  27.    
  28.     y = 0;
  29.     minscore = -4;
  30.     maxscore = 4;
  31.    
  32.     for(x=0;x<3;x++)
  33.         {
  34.             tempscore = board[x][0] + board[x][1] + board[x][2];
  35.                 if(player==1)
  36.                 {  
  37.                     if(tempscore>minscore)
  38.                         {
  39.                             minscore = tempscore;
  40.                         }
  41.                 }else
  42.                     {
  43.                         if(tempscore<maxscore)
  44.                         {
  45.                             maxscore = tempscore;
  46.                         }
  47.                     }
  48.             tempscore = board[0][x] + board[1][x] + board[2][x];
  49.             if(player==1)
  50.                 {  
  51.                     if(tempscore>minscore)
  52.                         {
  53.                             minscore = tempscore;
  54.                         }
  55.                 }else
  56.                     {
  57.                         if(tempscore<maxscore)
  58.                         {
  59.                             maxscore = tempscore;
  60.                         }
  61.                     }                                                  
  62.         }
  63.     for(x=0;x<2;x++)
  64.         {
  65.             tempscore = board[0][y-0]+board[1][1]+board[2][2-y];
  66.             if(player==1)
  67.                 {  
  68.                     if(tempscore>minscore)
  69.                         {
  70.                             minscore = tempscore;
  71.                         }
  72.                 }else
  73.                     {
  74.                         if(tempscore<maxscore)
  75.                         {
  76.                             maxscore = tempscore;
  77.                         }
  78.                     }  
  79.             y+=2;                  
  80.         }
  81.     if(player == 1)
  82.         {
  83.            
  84.             return minscore;           
  85.         }else
  86.             {
  87.                
  88.                 return maxscore;
  89.             }
  90. }
  91. int drawboard(int board[3][3])//Displays the current state of the board
  92. {
  93.    
  94.     int x,y;
  95.     char c[3] = {'X',' ','O'};
  96.     printf("---------------\n");
  97.     for(x=0;x<3;x++)
  98.         {
  99.             for(y=0;y<3;y++)
  100.                 {
  101.                     printf("| %c |",c[board[x][y]+1]);
  102.                 }
  103.             printf("\n---------------\n");
  104.         }
  105.            
  106. }
  107. int pcplay(int board[3][3])
  108. {
  109.    
  110.     int x,y,score,tempscore,movex,movey,player;
  111.     player = 1;
  112.     score = -4;
  113.    
  114.     for(x=0;x<3;x++)
  115.         {
  116.             for(y=0;y<3;y++)
  117.                 {
  118.                     if(board[x][y]==0)
  119.                         {
  120.                             board[x][y]=1;
  121.                         }else
  122.                             {
  123.                                 continue;
  124.                             }  
  125.                     tempscore = -minimax(board,player*-1);
  126.                     board[x][y] = 0;                   
  127.                     if(tempscore>score)
  128.                         {  
  129.                             score = tempscore;
  130.                             movex = x;
  131.                             movey = y;                         
  132.                         }      
  133.                 }  
  134.         }  
  135.         board[movex][movey] = 1;
  136. }
  137. int minimax(int board[3][3],int player)
  138. {
  139.    
  140.     int x,y,tempscore,score,move = 0;
  141.    
  142.     score = -4;
  143.     if(max(board,player*-1)== 3 || max(board,player*-1)==-3)
  144.         {
  145.             return -1;
  146.         }else
  147.             {
  148.            
  149.             for(x=0;x<3;x++)
  150.                 {
  151.                     for(y=0;y<3;y++)
  152.                         {
  153.                             if(board[x][y]==0)
  154.                                 {
  155.                                     board[x][y]=player;
  156.                                     move = 1;
  157.                                 }else
  158.                                     {
  159.                                         continue;
  160.                                     }
  161.                             tempscore = -minimax(board,player*-1);
  162.                             board[x][y] = 0;   
  163.                             if(tempscore>score)
  164.                                 {
  165.                                     score = tempscore;
  166.                                 }                              
  167.                         }
  168.                 }
  169.                 if(move == 0)
  170.                     {
  171.                         return 0;
  172.                     }else
  173.                         {
  174.                             return score;
  175.                         }
  176.             }  
  177.     }
  178. int main()
  179. {
  180.    
  181.     int round,player,x,y;
  182.     int board[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
  183.     player = -1;
  184.     round = 1;
  185.     while(1)
  186.         {
  187.             if(round %2 == 0)
  188.                 {
  189.                     pcplay(board);
  190.                     if(max(board,player*-1)==3)
  191.                         {  
  192.                             drawboard(board);
  193.                             printf("Player 2 wins\n");
  194.                             system("pause");
  195.                             system("cls");
  196.                             clearboard(board);
  197.                             round = 1;
  198.                         }else
  199.                             {
  200.                                 round++;
  201.                             }
  202.                 }else
  203.                     {  
  204.                         drawboard(board);
  205.                         printf("Column : ");
  206.                         scanf("%i",&x);
  207.                         printf("Line : ");
  208.                         scanf("%i",&y);
  209.                         if(board[x][y]==0)
  210.                             {
  211.                                 board[x][y] = player;
  212.                                 if(max(board,player)==-3)
  213.                                     {
  214.                                         drawboard(board);
  215.                                         printf("Player 1 wins\n");
  216.                                         system("pause");
  217.                                         system("cls");
  218.                                         clearboard(board);
  219.                                         round = 1;
  220.                                     }else
  221.                                         {
  222.                                             round++;
  223.                                         }
  224.                             }
  225.                     }
  226.             if(round == 10)
  227.                 {
  228.                     drawboard(board);
  229.                     printf("It's a tie\n");
  230.                     system("pause");
  231.                     system("cls");
  232.                     clearboard(board);
  233.                     round = 1;
  234.                 }          
  235.         }  
  236.        
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement