        // Run a game of noughts and crosses.
        // Squares are chosen randomly.
class TTTManager {
    // How big the game's board should be.
    public TTTManager(int size){
        game = new TTTGame(size);
        // Create a display for the game.
        display = new TTTDisplay(size);
    }

    // Play a single game.
    public void play(){
        final TTTGame game = getGame();
        game.clearTheBoard();
        getDisplay().clear();

        // Who plays next.
        char player = TTTGame.Nought;
        while(game.gameState() == TTTGame.Unfinished){
            makeARandomMove(player);
            if(!game.hasWon(player)){
                player = game.nextPlayer(player);
            }
        }
        reportTheResult();
    }

        // Make a random move for player.
    private void makeARandomMove(char player){
        final TTTGame game = getGame();
        // We shall generate a random row and column for each move.
        int row, col;
        // Generate a valid random move.
        do{
            row = getRandomPosition();
            col = getRandomPosition();
        } while(!game.isSquareFree(row,col));
        // Play in the chosen square.
        game.play(row,col,player);
        getDisplay().showSquare(row,col,player);
    }

    private void reportTheResult(){
        final TTTGame game = getGame();
        int result = game.gameState();

        if(result == TTTGame.Drawn){
            System.out.println("The game was drawn.");
        }
        else if(result == TTTGame.NoughtWon){
            System.out.println(TTTGame.Nought+" won.");
        }
        else if(result == TTTGame.CrossWon){
            System.out.println(TTTGame.Cross+" won.");
        }
        else{
            System.out.println("Unknown result: "+result);
        }
    }

    // Return a valid random number as part of a move.
    private int getRandomPosition(){
        return (int) (Math.random() * getGame().getBoardSize());
    }

    private TTTGame getGame(){
        return game;
    }

    private TTTDisplay getDisplay(){
        return display;
    }

    // The board. Initialized in the constructor.
    private final TTTGame game;
    // A GUI for the game.
    private final TTTDisplay display;
}
