Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.31 KB | None | 0 0
  1. #include<stdio.h>
  2. #define size 3
  3.  
  4. typedef struct move{
  5.  
  6.     int score;
  7.     int x;
  8.     int y;
  9.    
  10. }moveEval;
  11.  
  12. void printBoard(char board[size][size]){
  13.    
  14.     int i,j;
  15.     printf("\n");
  16.     printf("   0  1  2\n");
  17.     for(i=0;i<size;i++)
  18.         for(j=0;j<size;j++){
  19.             if(j == 0)
  20.                 printf("%d ",i);
  21.             printf("|%c|",board[i][j]);
  22.             if(j == 2)
  23.                 printf("\n");
  24.         }
  25.    
  26. }
  27.  
  28. char checkWin(char board[size][size]){
  29.    
  30.     if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' || board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')
  31.         return board[0][0];
  32.     else if(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' || board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')
  33.         return board[1][0];
  34.     else if(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X' || board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
  35.         return board[2][0];
  36.     else if(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' || board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')
  37.         return board[0][0];
  38.     else if(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' || board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')
  39.         return board[0][1];
  40.     else if(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' || board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')
  41.         return board[0][2];
  42.     else if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' || board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')
  43.         return board[0][0];
  44.     else if(board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X' || board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O')
  45.         return board[0][2];
  46.     return ' ';
  47.    
  48. }
  49.  
  50. int checkBoard(char board[size][size]){
  51.    
  52.     int i,j;
  53.    
  54.     for(i = 0 ; i < size ; i++)
  55.         for(j = 0 ; j < size ; j++)
  56.             if(board[i][j] == ' ')
  57.                 return 1;
  58.     return 0;
  59. }
  60.  
  61. int checkScore(char board[size][size], int x , int y){
  62.    
  63.     int i,j;
  64.     int northsouth = 0, eastwest = 0, northwestsoutheast = 0, northeastsouthwest = 0;
  65.    
  66.     //printBoard(board);
  67.  
  68.     for(i = x - 1, j = y ; i >= 0 ; i--){ // NORTH
  69.       if(board[i][j] == board[x][y]){
  70.         if(board[x][y] == 'X')
  71.             northsouth += 3;
  72.         else if(board[x][y] == 'O')
  73.             northsouth -= 3;
  74.       }else if(board[i][j] == ' '){
  75.         if(board[x][y] == 'X')
  76.             northsouth += 1;
  77.         else if(board[x][y] == 'O')
  78.             northsouth -= 1;     
  79.       }else{
  80.           northsouth = 0;    
  81.       }
  82.     }  
  83.    
  84.     for(i = x + 1, j = y; i < size; i++){ // SOUTH
  85.       if(board[i][j] == board[x][y]){
  86.         if(board[x][y] == 'X')
  87.             northsouth += 3;
  88.         else if(board[x][y] == 'O')
  89.             northsouth -= 3;
  90.       }else if(board[i][j] == ' '){
  91.         if(board[x][y] == 'X')
  92.             northsouth += 1;
  93.         else if(board[x][y] == 'O')
  94.             northsouth -= 1;     
  95.       }else{
  96.           northsouth = 0;    
  97.       }
  98.     }
  99.  
  100.        
  101.     for(j = y + 1, i = x; j < size; j++){ // EAST
  102.       if(board[i][j] == board[x][y]){
  103.         if(board[x][y] == 'X')
  104.             eastwest += 3;
  105.         else if(board[x][y] == 'O')
  106.             eastwest -= 3;
  107.       }else if(board[i][j] == ' '){
  108.         if(board[x][y] == 'X')
  109.             eastwest += 1;
  110.         else if(board[x][y] == 'O')
  111.             eastwest -= 1;   
  112.       }else{
  113.           eastwest = 0;  
  114.       }
  115.     }
  116.        
  117.     for(j = y - 1, i = x ; j >= 0 ; j--){ // WEST
  118.       if(board[i][j] == board[x][y]){
  119.         if(board[x][y] == 'X')
  120.             eastwest += 3;
  121.         else if(board[x][y] == 'O')
  122.             eastwest -= 3;
  123.       }else if(board[i][j] == ' '){
  124.         if(board[x][y] == 'X')
  125.             eastwest += 1;
  126.         else if(board[x][y] == 'O')
  127.             eastwest -= 1;   
  128.       }else{
  129.           eastwest = 0;  
  130.       }
  131.     }        
  132.  
  133.     for(i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--){ // NORTHWEST
  134.       if(board[i][j] == board[x][y]){
  135.         if(board[x][y] == 'X')
  136.             northwestsoutheast += 4;
  137.         else if(board[x][y] == 'O')
  138.             northwestsoutheast -= 4;
  139.       }else if(board[i][j] == ' '){
  140.         if(board[x][y] == 'X')
  141.             northwestsoutheast += 2;
  142.         else if(board[x][y] == 'O')
  143.             northwestsoutheast -= 2;     
  144.       }else{
  145.           northwestsoutheast = 0;    
  146.       }
  147.     }
  148.    
  149.     for(i = x + 1, j = y + 1; i < size && j < size; i++, j++){ // SOUTHEAST
  150.       if(board[i][j] == board[x][y]){
  151.         if(board[x][y] == 'X')
  152.             northwestsoutheast += 4;
  153.         else if(board[x][y] == 'O')
  154.             northwestsoutheast -= 4;
  155.       }else if(board[i][j] == ' '){
  156.         if(board[x][y] == 'X')
  157.             northwestsoutheast += 2;
  158.         else if(board[x][y] == 'O')
  159.             northwestsoutheast -= 2;     
  160.       }else{
  161.           northwestsoutheast = 0;    
  162.       }
  163.     }
  164.    
  165.     for(i = x - 1, j = y + 1; i >= 0 && j < size; i--, j++){ // NORTHEAST
  166.       if(board[i][j] == board[x][y]){
  167.         if(board[x][y] == 'X')
  168.             northeastsouthwest += 4;
  169.         else if(board[x][y] == 'O')
  170.             northeastsouthwest -= 4;
  171.       }else if(board[i][j] == ' '){
  172.         if(board[x][y] == 'X')
  173.             northeastsouthwest += 2;
  174.         else if(board[x][y] == 'O')
  175.             northeastsouthwest -= 2;     
  176.       }else{
  177.           northeastsouthwest = 0;    
  178.       }
  179.     }
  180.    
  181.     for(i = x + 1, j = y - 1; i < size && j >= 0; i++, j--){ // SOUTHWEST
  182.       if(board[i][j] == board[x][y]){
  183.         if(board[x][y] == 'X')
  184.             northeastsouthwest += 4;
  185.         else if(board[x][y] == 'O')
  186.             northeastsouthwest -= 4;
  187.       }else if(board[i][j] == ' '){
  188.         if(board[x][y] == 'X')
  189.             northeastsouthwest += 2;
  190.         else if(board[x][y] == 'O')
  191.             northeastsouthwest -= 2;     
  192.       }else{
  193.           northeastsouthwest = 0;    
  194.       }
  195.     }
  196.    
  197.     if(northsouth == 1 || northsouth == -1 || northsouth == 3 || northsouth == -3){
  198.         northsouth = 0;
  199.     }
  200.     if(eastwest == 1 || eastwest == -1 || northsouth == 3 || northsouth == -3){
  201.         eastwest = 0;
  202.     }
  203.     if(northwestsoutheast == 2 || northwestsoutheast == -2 || northwestsoutheast == 4 || northwestsoutheast == -4){
  204.         northwestsoutheast = 0;
  205.     }
  206.     if(northeastsouthwest == 2 || northeastsouthwest == -2 || northeastsouthwest == 4 || northeastsouthwest == -4){
  207.         northeastsouthwest = 0;
  208.     }
  209.            
  210.    
  211.     //printf("%c | NS: %d EW: %d NWSE: %d NESW: %d\n",board[x][y],northsouth,eastwest,northwestsoutheast,northeastsouthwest);
  212.     return northsouth+eastwest+northwestsoutheast+northeastsouthwest;
  213. }
  214.  
  215. moveEval evaluateWhiteMove(char board[size][size], int x, int y){
  216.    
  217.     moveEval move;
  218.     int i,j;
  219.    
  220.     move.score = 0;
  221.     move.x = x;
  222.     move.y = y;
  223.    
  224.     board[move.x][move.y] = 'X';
  225.    
  226.     if(checkWin(board) == 'X')
  227.         move.score += 2;
  228.     else if(checkWin(board) == 'O')
  229.         move.score -= 2;
  230.    
  231.     for(i=0;i<size;i++)
  232.         for(j=0;j<size;j++)
  233.             if(board[i][j] != ' ')
  234.                 move.score += checkScore(board,i,j);
  235.    
  236.     printf("\n(%d , %d) = %d\n",move.x,move.y,move.score);
  237.    
  238.     board[move.x][move.y] = ' ';
  239.  
  240.     return move;
  241.    
  242. }
  243.  
  244. void whiteMove(char board[size][size]){
  245.    
  246.     int i,j;
  247.     moveEval move;
  248.     moveEval checkmove;
  249.     int scorecheck;
  250.     int first = 1;
  251.     for(i=0;i<size;i++){
  252.         for(j=0;j<size;j++){
  253.             if(board[i][j] == ' '){
  254.                 checkmove = evaluateWhiteMove(board,i,j);
  255.                 scorecheck = checkmove.score;
  256.                 if(first){
  257.                     move = checkmove;
  258.                     first--;
  259.                 }
  260.                 if(move.score <= scorecheck){
  261.                     move = checkmove;
  262.                 }
  263.             }
  264.         }
  265.     }
  266.    
  267.     board[move.x][move.y] = 'X';
  268.    
  269. }
  270.  
  271. moveEval evaluateBlackMove(char board[size][size], int x, int y){
  272.    
  273.     moveEval move;
  274.     int i,j;
  275.    
  276.     move.score = 0;
  277.     move.x = x;
  278.     move.y = y;
  279.    
  280.     board[move.x][move.y] = 'O';
  281.    
  282.     if(checkWin(board) == 'X')
  283.         move.score += 2;
  284.     else if(checkWin(board) == 'O')
  285.         move.score -= 2;
  286.    
  287.     for(i=0;i<size;i++)
  288.         for(j=0;j<size;j++)
  289.             if(board[i][j] != ' ')
  290.                 move.score += checkScore(board,i,j);
  291.    
  292.     printf("\n(%d , %d) = %d\n",move.x,move.y,move.score);
  293.    
  294.     board[move.x][move.y] = ' ';
  295.  
  296.     return move;
  297.    
  298. }
  299.  
  300. void blackMove(char board[size][size]){
  301.    
  302.     int i,j;
  303.     moveEval move;
  304.     moveEval checkmove;
  305.     int scorecheck;
  306.     int first = 1;
  307.     for(i=0;i<size;i++){
  308.         for(j=0;j<size;j++){
  309.             if(board[i][j] == ' '){
  310.                 checkmove = evaluateBlackMove(board,i,j);
  311.                 scorecheck = checkmove.score;
  312.                 if(first){
  313.                     move = checkmove;
  314.                     first--;
  315.                 }
  316.                 if(move.score >= scorecheck){
  317.                     move = checkmove;
  318.                 }
  319.             }
  320.         }
  321.     }
  322.    
  323.     printf("Before %d %d \n",move.x,move.y);
  324.     board[move.x][move.y] = 'O';
  325.     printf("After\n");
  326.    
  327. }
  328.  
  329. int main(){
  330.  
  331.     char board[size][size];
  332.    
  333.     int i,j;
  334.    
  335.     for(i = 0 ; i < size ; i++)
  336.         for(j = 0 ; j < size ; j++)
  337.             board[i][j] = ' ';
  338.    
  339.     printf("## XO ##\n");
  340.  
  341.     board[1][1] = 'X';
  342.     board[2][0] = 'O';
  343.    
  344.     while(checkBoard(board) && checkWin(board) == ' '){
  345.         printBoard(board);
  346.         whiteMove(board);
  347.         printBoard(board);
  348.         blackMove(board);
  349.     }
  350.    
  351.     return 0;
  352.    
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement