Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //look the previous post to get the code to use in arduino
- import processing.serial.*;
- short TOTAL_CELLS = 9;
- short FIRST_CELL = 25;
- short CELL_LEN = 50;
- short CELL_SPACE = 10;
- short FIRST_TEXT_X = 35;
- short FIRST_TEXT_Y = 70;
- short TEXT_LEN = 60;
- short RED = 1;
- short BLUE = 2;
- int bPos[][] = {
- {
- FIRST_CELL,
- FIRST_CELL
- },
- {
- FIRST_CELL + CELL_LEN + CELL_SPACE,
- FIRST_CELL
- },
- {
- FIRST_CELL + CELL_LEN*2 + CELL_SPACE*2,
- FIRST_CELL
- },
- {
- FIRST_CELL,
- FIRST_CELL + CELL_LEN + CELL_SPACE
- },
- {
- FIRST_CELL + CELL_LEN + CELL_SPACE,
- FIRST_CELL + CELL_LEN + CELL_SPACE
- },
- {
- FIRST_CELL + CELL_LEN*2 + CELL_SPACE*2,
- FIRST_CELL + CELL_LEN + CELL_SPACE
- },
- {
- FIRST_CELL,
- FIRST_CELL + CELL_LEN*2 + CELL_SPACE*2
- },
- {
- FIRST_CELL + CELL_LEN + CELL_SPACE,
- FIRST_CELL + CELL_LEN*2 + CELL_SPACE*2
- },
- {
- FIRST_CELL + CELL_LEN*2 + CELL_SPACE*2,
- FIRST_CELL + CELL_LEN*2 + CELL_SPACE*2
- }
- };
- int tPos[][] = {
- {
- FIRST_TEXT_X,
- FIRST_TEXT_Y,
- 0
- },
- {
- FIRST_TEXT_X + TEXT_LEN,
- FIRST_TEXT_Y,
- 0
- },
- {
- FIRST_TEXT_X + TEXT_LEN*2,
- FIRST_TEXT_Y,
- 0
- },
- {
- FIRST_TEXT_X,
- FIRST_TEXT_Y + TEXT_LEN,
- 0
- },
- {
- FIRST_TEXT_X + TEXT_LEN,
- FIRST_TEXT_Y + TEXT_LEN,
- 0
- },
- {
- FIRST_TEXT_X + TEXT_LEN*2,
- FIRST_TEXT_Y + TEXT_LEN,
- 0
- },
- {
- FIRST_TEXT_X,
- FIRST_TEXT_Y + TEXT_LEN*2,
- 0
- },
- {
- FIRST_TEXT_X + TEXT_LEN,
- FIRST_TEXT_Y + TEXT_LEN*2,
- 0
- },
- {
- FIRST_TEXT_X + TEXT_LEN*2,
- FIRST_TEXT_Y + TEXT_LEN*2,
- 0
- }
- };
- Serial myPort;
- void setup()
- {
- size(220, 220);
- short x;
- short portIndex = 0;
- String portName = Serial.list()[portIndex];
- printArray(Serial.list());
- println("Connecting to -> " + Serial.list()[portIndex]);
- myPort = new Serial(this, portName, 9600);
- for(x = 0; x < TOTAL_CELLS; x++)
- {
- rect(bPos[x][0], bPos[x][1], CELL_LEN, CELL_LEN);
- }
- textSize(50);
- }
- void draw()
- {
- short t;
- while(myPort.available() >= 1)
- {
- int pos = myPort.read();
- //debug
- print(pos + " ");
- //
- tPos[pos][2] = BLUE;
- }
- for(t = 0; t < TOTAL_CELLS; t++)
- {
- if(tPos[t][2] == RED)
- {
- fill(255, 0, 0);
- text("X", tPos[t][0], tPos[t][1]);
- }
- else if(tPos[t][2] == BLUE)
- {
- fill(0, 0, 255);
- text("0", tPos[t][0], tPos[t][1]);
- }
- }
- }
- void mouseClicked()
- {
- int t = verifyCell(mouseX, mouseY);
- if(t != -1)
- {
- tPos[t][2] = RED;
- myPort.write(t);
- println("Clicked!");
- //the computer plays (received on draw)
- }
- }
- int verifyCell(int x, int y)
- {
- int i;
- for(i = 0; i < TOTAL_CELLS; i++)
- {
- if( x >= bPos[i][0] && x <= bPos[i][0]+CELL_LEN &&
- y >= bPos[i][1] && y <= bPos[i][1]+CELL_LEN )
- {
- return i;
- }
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement