nicatronTg icon

Untitled

nicatronTg | PRO | 04/19/12 05:16:25 AM UTC | 0 ⭐ | 205 👁️ | Never ⏰ | []
text |

3.78 KB

|

None

|

0 👍

/

0 👎

// a 3x3 array of characters represents the board
char[][] board = new char[3][3];
// how big is each box, and where did you click
int boxSize, clickX, clickY;
// is it X's turn (true) or O's turn (false)
boolean turnX = true;
int backgroundColor = 155;
int strokeColor = 0;
boolean xWon = false;
boolean oWon = false;
 void setup() {
  size(300,300);
  boxSize = width/3;
  strokeWeight(2.0);
  stroke(strokeColor);
  background(backgroundColor);
  resetBoard();
}
 void checkWinHorizontally()
{
  for (int i = 0; i < 3; i++) {
    int tempX = 0;
    int tempY = 0;
    for (int j = 0; j < 3; j++) {
      if (board[j][i] == 'X') {
        tempX++;
      } else if (board[j][i] == 'O') {
        tempY++;
      }
       if (j == 2) {
        if (tempX == 3)
        {
          print("Win: X," + i);
          xWon = true;
        } else if (tempY == 3) {
          print("Win: Y," + i);
          oWon = true;
        }
        tempX = 0;
        tempY = 0;
      }
    }
  }
}
 void checkWinVertically()
{
  for (int i = 0; i < 3; i++) {
    int tempX = 0;
    int tempY = 0;
    for (int j = 0; j < 3; j++) {
      if (board[i][j] == 'X') {
        tempX++;
      } else if (board[i][j] == 'O') {
        tempY++;
      }
       if (j == 2) {
        if (tempX == 3)
        {
          print("Win: X," + i);
          xWon = true;
        } else if (tempY == 3) {
          print("Win: Y," + i);
          xWon = false;
        }
        tempX = 0;
        tempY = 0;
      }
    }
  }
}
 // set all the squares to '?' (no player has used them)
void resetBoard() {
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      board[i][j] = '?';
    }
  }
}
 // draw an X
void drawX(int x, int y) {
  if (xWon) {
    stroke(255);
  } else {
    stroke(strokeColor);
  }
  line(x - boxSize/4, y - boxSize/4, x + boxSize/4, y + boxSize/4);
  line(x - boxSize/4, y + boxSize/4, x + boxSize/4, y - boxSize/4);
  stroke(strokeColor);
}
 // draw an O
void drawO(int x, int y) {
  if (oWon) {
    fill(255);
  } else {
    fill(155);
  }
  ellipse(x, y, boxSize/2, boxSize/2);
}
 void drawBoard() {
  // draw the grid lines
  line(boxSize, 0, boxSize, height);
  line(2*boxSize, 0, 2*boxSize, height);
  line(0, boxSize, width, boxSize);
  line(0, 2*boxSize, width, 2*boxSize);
  // populate the board
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      if (board[i][j] == 'X') {
        drawX(boxSize*i + boxSize/2, boxSize*j + boxSize/2);
      }
      else if (board[i][j] == 'O') {
        drawO(boxSize*i + boxSize/2, boxSize*j + boxSize/2);
      }
    }
  }
}
 // when a square is clicked, save the mouse coords and update
void mousePressed() {
  if ((xWon == true) || (oWon == true))
  {
    board = new char[3][3];
    resetBoard();
    xWon = false;
    oWon = false;
    return;
  }
  clickX = mouseX;
  clickY = mouseY;
  updateBoard();
  checkWinVertically();
  checkWinHorizontally();
}
 // update the board[][] array with the clicked square
void updateBoard() {
   int i, j;
   // which 3rd of the board did the X coord land on
  if (clickX < boxSize) i = 0;
  else if (clickX < 2*boxSize) i = 1;
  else if (clickX < width) i = 2;
  else i = -1;
   // which 3rd of the board did the Y coord land on
  if (clickY < boxSize) j = 0;
  else if (clickY < 2*boxSize) j = 1;
  else if (clickY < width) j = 2;
  else j = -1;
   // if the click was inside the grid and the space hasn't been used
  if (i >= 0 && j >= 0 & board[i][j] == '?') {
    // set the square based on whose turn it is
    if (turnX) {
      board[i][j] = 'X';
    }
    else {
      board[i][j] = 'O';
    }
    turnX = !turnX;
  }
}
 void draw() {
  drawBoard();
}

Comments