public class TicTacToe { TicTacToe(int size) { this.size = size; board = new char [size] [size]; done = false; moveCount = 0; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { board[i][j] = EMPTY; } } turn = 'X'; } public boolean catsGame() { return (moveCount == size * size); } public boolean gameWon() { if (done) { return true; } for (int i = 0; i < size; i++) { if (inALine(i, 0, 0, 1)) // row { return true; } if (inALine(0, i, 1, 0)) // column { return true; } } if (inALine(0,0,1,1)) // major diagonal { return true; } if (inALine(0, size-1, 1, -1)) // minor diagonal { return true; } return false; } private boolean inALine (int startX, int startY, int incX, int incY) { int x = startX; int y = startY; char match = board[x][y]; if (match == EMPTY) { return false; } for (int i = 1; i < size; i++) { x += incX; y += incY; if (match != board[x][y]) { return false; } } return true; } public char getTurn () { return turn; } public int getSize() { return size; } public char getPlay (int x, int y) { return board[x][y]; } public void makeGuess (int x, int y) { if (board[x][y] == EMPTY) { moveCount++; board[x][y] = turn; turn = getOtherTurn (); } } public char getOtherTurn () { if (turn == 'X') { return 'O'; } else { return 'X'; } } private int moveCount; private boolean done; private int size; private char [] [] board; private char turn; private static final char EMPTY = 'e'; }