Design Tic Tac Toe

Questions

  • Is it Timed?
  • Is undo possible?
  • Spectate?
  • Is there any statistics?
  • Is Tournament possible?
  • Is AI possible?
  • Is Elo Rating possible?
  • How many players?

Objects

  • Piece: X, O, Z, $ etc. (Any symbol and any number of players)
  • Board: M*M
  • Player

Board

class Board
{
    public:
    
    Board() {
        // Initialize the board
    }
 
    // returns the whole board
    int[][] getBoard(); 
 
    // returns one of the following:
    // "First", "Second", "Draw", "Undecided"
    String getWinner();
 
    // returns: "First" or "Second"
    String getCurrentPlayer();
 
    // Idea for makeMove() functions
    void makeMove(Move move);
}

Classes

PlayingPiece

- pieceType: PieceType
+ PlayingPiece(PieceType T)
 
Maybe create two more classes::
PlayingPieceX extends PlayingPiece
PlayingPieceX() {
     super(PieceType.CROSS);
}
 
PlayingPieceO extends PlayingPiece
PlayingPieceO() {
     super(PieceType.NOUGHTS);
}
 

Board

- size: int
+ board: PlayingPiece[][]
+ Board(size: int)
+ addPiece(i, j, playingPiece)

Player

- name: String
- playingPiece: PlayingPiece

Game (main class)

class TicTacGame {
    // helpful to remove from front and insert at the end
    private Deque<Player> players;
    private Board gameBoard;
 
    TicTacToeGame() {
        players = new ArrayDeque<>();
        Player player1 = new Player("John", new PlayingPieceX());
        Player player2 = new Player("Jane", new PlayingPieceO());
        
        players.add(player1);
        players.add(player2);
    
        gameBoard = new Board(3); // 3 * 3 board
    }
 
    public String startGame() {
        boolean noWinner = true;
        while (noWinner) {
            Player playerTurn = players.removeFirst();
 
            gameBoard.printBoard();
 
            if (gameBoard.getEmptyCellsCount() == 0) {
                // game has ended
                // no winner found
                noWinner = false;
                continue;
            }
 
            // read user input
 
            // add piece
            boolean pieceAddedSuccessfully = gameBoard.addPiece(
                inputRow,
                inputCol,
                playerTurn.getPlayingPiece()
            );
 
            if (!pieceAddedSuccessfully) {
                // handle error
                // add back to the first of list
                players.addFirst(playerTurn);
                continue;
            }
 
            players.addLast(playerTurn);
            if (isThereWinner(...)) {
                return playerTurn.getName();
            }
        }
    }
 
    private isThereWinner(....) { ... }
}