Advertisement
obernardovieira

Tic-Tac-Toe against arduino

Mar 26th, 2016
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. //look the previous post to get the code to use in arduino
  2.  
  3. import processing.serial.*;
  4.  
  5. short TOTAL_CELLS = 9;
  6. short FIRST_CELL  = 25;
  7. short CELL_LEN    = 50;
  8. short CELL_SPACE  = 10;
  9.  
  10. short FIRST_TEXT_X = 35;
  11. short FIRST_TEXT_Y = 70;
  12. short TEXT_LEN     = 60;
  13.  
  14. short RED  = 1;
  15. short BLUE = 2;
  16.  
  17. int bPos[][] = {
  18.     {
  19.         FIRST_CELL,
  20.         FIRST_CELL
  21.     },
  22.     {
  23.         FIRST_CELL + CELL_LEN + CELL_SPACE,
  24.         FIRST_CELL
  25.     },
  26.     {
  27.         FIRST_CELL + CELL_LEN*2 + CELL_SPACE*2,
  28.         FIRST_CELL
  29.     },
  30.     {
  31.         FIRST_CELL,
  32.         FIRST_CELL + CELL_LEN + CELL_SPACE
  33.     },
  34.     {
  35.         FIRST_CELL + CELL_LEN + CELL_SPACE,
  36.         FIRST_CELL + CELL_LEN + CELL_SPACE
  37.     },
  38.     {
  39.         FIRST_CELL + CELL_LEN*2 + CELL_SPACE*2,
  40.         FIRST_CELL + CELL_LEN + CELL_SPACE
  41.     },
  42.     {
  43.         FIRST_CELL,
  44.         FIRST_CELL + CELL_LEN*2 + CELL_SPACE*2
  45.     },
  46.     {
  47.         FIRST_CELL + CELL_LEN + CELL_SPACE,
  48.         FIRST_CELL + CELL_LEN*2 + CELL_SPACE*2
  49.     },
  50.     {
  51.         FIRST_CELL + CELL_LEN*2 + CELL_SPACE*2,
  52.         FIRST_CELL + CELL_LEN*2 + CELL_SPACE*2
  53.     }
  54. };
  55. int tPos[][] = {
  56.     {
  57.         FIRST_TEXT_X,
  58.         FIRST_TEXT_Y,
  59.         0
  60.     },
  61.     {
  62.         FIRST_TEXT_X + TEXT_LEN,
  63.         FIRST_TEXT_Y,
  64.         0
  65.     },
  66.     {
  67.         FIRST_TEXT_X + TEXT_LEN*2,
  68.         FIRST_TEXT_Y,
  69.         0
  70.     },
  71.     {
  72.         FIRST_TEXT_X,
  73.         FIRST_TEXT_Y + TEXT_LEN,
  74.         0
  75.     },
  76.     {
  77.         FIRST_TEXT_X + TEXT_LEN,
  78.         FIRST_TEXT_Y + TEXT_LEN,
  79.         0
  80.     },
  81.     {
  82.         FIRST_TEXT_X + TEXT_LEN*2,
  83.         FIRST_TEXT_Y + TEXT_LEN,
  84.         0
  85.     },
  86.     {
  87.         FIRST_TEXT_X,
  88.         FIRST_TEXT_Y + TEXT_LEN*2,
  89.         0
  90.     },
  91.     {
  92.         FIRST_TEXT_X + TEXT_LEN,
  93.         FIRST_TEXT_Y + TEXT_LEN*2,
  94.         0
  95.     },
  96.     {
  97.         FIRST_TEXT_X + TEXT_LEN*2,
  98.         FIRST_TEXT_Y + TEXT_LEN*2,
  99.         0
  100.     }
  101. };
  102. Serial myPort;
  103.  
  104. void setup()
  105. {
  106.     size(220, 220);
  107.     short x;
  108.     short portIndex = 0;
  109.     String portName = Serial.list()[portIndex];
  110.  
  111.     printArray(Serial.list());
  112.     println("Connecting to -> " + Serial.list()[portIndex]);
  113.     myPort = new Serial(this, portName, 9600);
  114.  
  115.     for(x = 0; x < TOTAL_CELLS; x++)
  116.     {
  117.         rect(bPos[x][0], bPos[x][1], CELL_LEN, CELL_LEN);
  118.     }
  119.     textSize(50);
  120. }
  121.  
  122. void draw()
  123. {
  124.     short t;
  125.  
  126.     while(myPort.available() >= 1)
  127.     {
  128.         int pos = myPort.read();
  129.         //debug
  130.         print(pos + " ");
  131.         //
  132.         tPos[pos][2] = BLUE;
  133.     }
  134.  
  135.     for(t = 0; t < TOTAL_CELLS; t++)
  136.     {
  137.         if(tPos[t][2] == RED)
  138.         {
  139.             fill(255, 0, 0);
  140.             text("X", tPos[t][0], tPos[t][1]);
  141.         }
  142.         else if(tPos[t][2] == BLUE)
  143.         {
  144.             fill(0, 0, 255);
  145.             text("0", tPos[t][0], tPos[t][1]);
  146.         }
  147.     }
  148. }
  149.  
  150. void mouseClicked()
  151. {
  152.     int t = verifyCell(mouseX, mouseY);
  153.     if(t != -1)
  154.     {
  155.         tPos[t][2] = RED;
  156.         myPort.write(t);
  157.         println("Clicked!");
  158.         //the computer plays (received on draw)
  159.     }
  160. }
  161.  
  162. int verifyCell(int x, int y)
  163. {
  164.     int i;
  165.  
  166.     for(i = 0; i < TOTAL_CELLS; i++)
  167.     {
  168.         if( x >= bPos[i][0] && x <= bPos[i][0]+CELL_LEN &&
  169.             y >= bPos[i][1] && y <= bPos[i][1]+CELL_LEN )
  170.         {
  171.             return i;
  172.         }
  173.     }
  174.  
  175.     return -1;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement