/*
 * @topic W120121 Java <a href="http://www.c-jump.com/bcc/c123c/c123sample/Java/Week11/tic_tac_toe_class_violet.html" target="_blank">Tic-Tac-Toe Game</a>
 * @brief class Game
 */
package tictactoeapp;

public class Game {

    // data attributes
    private Board board;
    private Player playerX;
    private Player playerO;

    // constructors
    public Game( Board board ) {
        this.board = board;
        playerX = new PlayerAI(
                "X",             // nickname
                Board.PIECE_X,   // piece type
                PlayerAI.STRATEGY_RANDOM, // strategy
                board
        );
        playerO = new PlayerAI(
                "O",             // nickname
                Board.PIECE_O,   // piece type
                PlayerAI.STRATEGY_RANDOM, // strategy
                board
        );
    }// Game constructor
    
    // operations
    public void start() {
        System.out.println( "-----------------------------------" );
        // limit moves to the numbers of squares on the board
        int moveCount = board.size();
        for (;;) {
            // first player moves
            playerX.move();
            --moveCount;
            if ( board.isGameOver() == true ) {
                board.print();
                System.out.println( "Player" + playerX.getNickname() + " wins!" );
                return;
            }
            if ( moveCount == 0 ) break; // no more moves left

            // second player moves
            playerO.move();
            --moveCount;
            if ( board.isGameOver() == true ) {
                board.print();
                System.out.println( "Player" + playerO.getNickname() + " wins!" );
                return;
            }
            if ( moveCount == 0 ) break; // no more moves left
        }//while

        board.print();
        System.out.println( "A draw!" );
    }//start
    
}//class Game