Advertisement
obernardovieira

Tic-Tac-Toe against arduino (Using GUI Processing)

Mar 26th, 2016
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. //don't miss the next post to get the Processing code
  2.  
  3. #define PLAYER_MOVE     1
  4. #define COMPUTER_MOVE   2
  5.  
  6. void setup()
  7. {
  8.     Serial.begin(9600);
  9.     Serial.print("Write you chooses using the format ");
  10.     Serial.println("'line column' (in numbers) without \" ' \" ");
  11. }
  12.  
  13. void loop()
  14. {
  15.     while(Serial.available() >= 1)
  16.     {
  17.         int value;
  18.  
  19.         value = Serial.read();
  20.         playPlayer(value);
  21.     }
  22. }
  23.  
  24. void playPlayer(int pos)
  25. {
  26.     static int board[9] = {
  27.         0, 0, 0,
  28.         0, 0, 0,
  29.         0, 0, 0
  30.     };
  31.  
  32.     board[pos] = PLAYER_MOVE;
  33.  
  34.     if(findWinner(board))
  35.     {
  36.         //Serial.println("Player is the winner!");
  37.     }
  38.     playComputer(board);
  39. }
  40.  
  41. void playComputer(int board[9])
  42. {
  43.     if(!chooseNextMove(board))
  44.     {
  45.         computerStrategicMove(board);
  46.     }
  47.  
  48.     if(findWinner(board))
  49.     {
  50.         //Serial.println("Computer is the winner!");
  51.     }
  52. }
  53.  
  54. bool chooseNextMove(int board[9])
  55. {
  56.     int p;
  57.  
  58.     for(p = 0; p < 9; p++)
  59.     {
  60.         if(!board[p])
  61.         {
  62.             board[p] = COMPUTER_MOVE;
  63.             if(findWinner(board))
  64.             {
  65.                 Serial.write(p);
  66.                 return true;
  67.             }
  68.             board[p] = PLAYER_MOVE;
  69.             if(findWinner(board))
  70.             {
  71.                 Serial.write(p);
  72.                 board[p] = COMPUTER_MOVE;
  73.                 return true;
  74.             }
  75.             board[p] = 0;
  76.         }
  77.     }
  78.     return false;
  79. }
  80.  
  81. void computerStrategicMove(int board[9])
  82. {
  83.     bool ahead = false;
  84.     int p = 4;
  85.  
  86.     //center
  87.  
  88.     if(!board[p])
  89.     {
  90.         board[p] = COMPUTER_MOVE;
  91.         Serial.write(p);
  92.         return;
  93.     }
  94.  
  95.     //diagonal
  96.  
  97.     for( p = 0; p < 9; p += ((!ahead) ? (2) : (4)) )
  98.     {
  99.         if(!board[p])
  100.         {
  101.             board[p] = COMPUTER_MOVE;
  102.             Serial.write(p);
  103.             return;
  104.         }
  105.  
  106.         ahead ^= true;
  107.     }
  108.  
  109.     //mid lines
  110.  
  111.     for(p = 1; p < 9; p+=2)
  112.     {
  113.         if(!board[p])
  114.         {
  115.             board[p] = COMPUTER_MOVE;
  116.             Serial.write(p);
  117.             return;
  118.         }
  119.     }
  120. }
  121.  
  122. bool findWinner(const int board[9])
  123. {
  124.     int a;
  125.  
  126.     for(a = 0; a < 9; a+=3)
  127.     {
  128.         if(!board[a])
  129.         {
  130.             continue;
  131.         }
  132.         if( board[a] == board[a + 1] &&
  133.             board[a] == board[a + 2])
  134.         {
  135.             return true;
  136.         }
  137.     }
  138.  
  139.     //
  140.  
  141.     for(a = 0; a < 3; a++)
  142.     {
  143.         if(!board[a])
  144.         {
  145.             continue;
  146.         }
  147.         if( board[a] == board[a + 3] &&
  148.             board[a] == board[a + 6])
  149.         {
  150.             return true;
  151.         }
  152.     }
  153.  
  154.     //
  155.  
  156.     if( board[0] == board[4] &&
  157.         board[0] == board[8] && board[0] != 0)
  158.     {
  159.         return true;
  160.     }
  161.     if( board[2] == board[4] &&
  162.         board[2] == board[6] && board[2] != 0)
  163.     {
  164.         return true;
  165.     }
  166.     return false;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement