final static int XTURN = 0; final static int OTURN = 1; int turn = XTURN; boolean gameOver = false; int w = 400; int h = 400; int xsquares = 3; int ysquares = xsquares; // TicTacToe has to be square TicTacSquare[][] ttSquares = new TicTacSquare[ysquares][xsquares]; int squareWidth, squareHeight; int lineWidth = 4; int boardWidth, boardHeight; color backColor = color(0); void setup() { squareWidth = w/xsquares; squareHeight = h/ysquares; boardWidth = squareWidth * xsquares + lineWidth * (xsquares+1); boardHeight = squareHeight * ysquares + lineWidth * (ysquares+1); for (int y = 0; y < ysquares; y++) { for (int x = 0; x < xsquares; x++) { ttSquares[y][x] = new TicTacSquare(x*squareWidth+(x+1)*lineWidth,y*squareHeight+(y+1)*lineWidth,squareWidth,squareHeight); } } size(boardWidth,boardHeight); noStroke(); noLoop(); } void draw() { background(backColor); drawBoard(); } void drawBoard() { for (int y = 0; y < ysquares; y++) { for (int x = 0; x < xsquares; x++) { ttSquares[y][x].drawSquare(); } } } void checkForWin() { // Check X rows for (int y = 0; y < ttSquares.length; y++) { boolean isWin = false; for (int x = 0; x < ttSquares[y].length && !isWin; x++) { if (ttSquares[y][x].marker != ttSquares[y][ttSquares[y].length-1].marker || ttSquares[y][ttSquares[y].length-1].marker == TicTacSquare.NOMARK) { // We definitely don't have a win so break out break; } else if (x == ttSquares[y].length-1) { // We definitely have a win isWin = true; } } if (isWin) { for (int xx = 0; xx < ttSquares[y].length; xx++) { ttSquares[y][xx].markWin(); } gameOver = true; } } // Check Y rows for (int x = 0; x < ttSquares[0].length; x++) { boolean isWin = false; for (int y = 0; y < ttSquares.length && !isWin; y++) { if (ttSquares[y][x].marker != ttSquares[ttSquares.length-1][x].marker || ttSquares[ttSquares.length-1][x].marker == TicTacSquare.NOMARK) { break; } else if (y == ttSquares.length-1) { isWin = true; } } if (isWin) { for (int yy = 0; yy < ttSquares.length; yy++) { ttSquares[yy][x].markWin(); } gameOver = true; } } // Check Diagonals boolean isWin = false; for (int s = 0; s < ttSquares.length && !isWin; s++) { if (ttSquares[s][s].marker != ttSquares[ttSquares.length-1][ttSquares.length-1].marker || ttSquares[s][s].marker == TicTacSquare.NOMARK) { break; } else if (s == ttSquares.length-1) { isWin = true; } } if (isWin) { for (int ss = 0; ss < ttSquares.length; ss++) { ttSquares[ss][ss].markWin(); } gameOver = true; } isWin = false; for (int s = 0; s < ttSquares.length && !isWin; s++) { if (ttSquares[s][ttSquares.length-(s+1)].marker != ttSquares[ttSquares.length-1][0].marker || ttSquares[s][ttSquares.length-(s+1)].marker == TicTacSquare.NOMARK) { break; } else if (s == ttSquares.length-1) { isWin = true; } } if (isWin) { for (int ss = 0; ss < ttSquares.length; ss++) { ttSquares[ss][ttSquares.length-(ss+1)].markWin(); } gameOver = true; } } void mousePressed() { if (!gameOver) { for (int y = 0; y < ysquares; y++) { for (int x = 0; x < xsquares; x++) { if (ttSquares[y][x].contains(mouseX,mouseY)) { if (ttSquares[y][x].marker == TicTacSquare.NOMARK) { if (turn == XTURN) { ttSquares[y][x].changeMark(TicTacSquare.XMARK); turn = OTURN; } else if (turn == OTURN) { ttSquares[y][x].changeMark(TicTacSquare.OMARK); turn = XTURN; } } } } } checkForWin(); redraw(); } }