/** main class creating a board and the GUI
 * defines the game play
 */
public class NotaktoGame implements Game {

    /** currently active player */
    private int currentPlayer;

    /** total number of players */
    private int numPlayers;
    
    /** the board we play on */
    private NotaktoBoard board;
    
    /** the gui for board games */
    private UserInterface ui;
    
    /** constructor that gets the number of players */
    public NotaktoGame(int size) {
        this.currentPlayer = 1;
        this.numPlayers = 2;
        this.board = new NotaktoBoard(size);
    }

    @Override
    public String getTitle() {
        int size = this.board.getSize();
        return size+"x"+size+" sNotakto";
    }

    @Override
    public void addMove(Coordinate pos) {
        this.board.addMove(pos);
        this.currentPlayer = this.currentPlayer == 1 ? 2 : 1;
    }

    @Override
    public String getContent(Coordinate pos) {
        return this.board.isFree(pos) ? "" : "X";
    }

    @Override
    public int getHorizontalSize() {
        return this.board.getSize();
    }

    @Override
    public int getVerticalSize() {
        return this.board.getSize();
    }

    @Override
    public void checkResult() {
        if (this.board.checkFullDeprecated()) {
            this.ui.showResult("This should NEVER have happened!");
        }
        if (this.board.checkWinning()) {
            this.ui.showResult("Player "+this.currentPlayer+" wins!");
        }
    }

    @Override
    public boolean isFree(Coordinate pos) {
        return this.board.isFree(pos);
    }

    @Override
    public void setUserInterface(UserInterface ui) {
        this.ui = ui;
        
    }
    
    public String toString() {
        return "Board before Player "+this.currentPlayer+" of "+this.numPlayers+"'s turn:\n"+this.board.toString();
    }

}
