Skip to content
Snippets Groups Projects
Board.java 3.23 KiB
Newer Older
/**
 * This class is an Object that defines the logical enviroment of the application.
 */
JimmyTheCat's avatar
JimmyTheCat committed
public class Board extends Thread {
Josua Oppermann's avatar
Josua Oppermann committed
    
    /**
     * This Array contains the Tiles on which the players draw.
JimmyTheCat's avatar
JimmyTheCat committed
     * The tiles are aranged two dimensionally in this form:
     * 0 1 2
     * 3 4 5
     * 6 7 8
     */
    Tile tiles[] = new Tile[9];

    /**
     * This Array contains the Players.
JimmyTheCat's avatar
JimmyTheCat committed
     */
    Player players[] = new Player[2];

    /**
     * This Integer defines which character gets to draw first.
    /**
     * This Integer defines which character can currently draw on a tile.
JimmyTheCat's avatar
JimmyTheCat committed
     */
    int currentPlayer;

    /**
     * This Integer tracts the amount of marks that were already placed.
    /**
     * Constructor of the Board class.
     * Creates a board with new players and a set turn order.
JimmyTheCat's avatar
JimmyTheCat committed
     */
    Board() {
JimmyTheCat's avatar
JimmyTheCat committed
        for(int i = 0; i < players.length; i++) players[i] = new Player(i+1);
JimmyTheCat's avatar
JimmyTheCat committed
    }
   
    /**
     * Constructor of the Board class.
     * Creates a board with set players and an inverted turn order.
     * 
     * @param players the players of the previous game.
     * @param firstPlayer the first turn assignment of the previous game.
     */
    Board(Player[] players, int firstPlayer) {
JimmyTheCat's avatar
JimmyTheCat committed
        this.players = players;
        this.firstPlayer = firstPlayer == 1 ? 0 : 1;
        initBoard();
    }

    /**
     * Creats the tiles and assigns the first turn.
     */
        for(int i = 0; i < tiles.length; i++) tiles[i] = new Tile();
JimmyTheCat's avatar
JimmyTheCat committed
        marks = 0;
    }

    /**
     * Ends the turn by increasing the mark counter and changing the player.
     * Defines an end state and returns it.
     * end states:
     * 0 = no draw, no winner
     * 1 = current player won
     * 2 = draw
     * @return end state of the turn.
JimmyTheCat's avatar
JimmyTheCat committed
     */
    public int turnEnd() {
        int endState = 0;
        if(marks > 4 && victoryCheck()) {
            //System.out.println("You win Player " + currentPlayer);
JimmyTheCat's avatar
JimmyTheCat committed
            players[currentPlayer].playerScoreInc();
            endState = 1;
        } else if(marks == 9) {
            //System.out.println("A draw");
            endState = 2;
JimmyTheCat's avatar
JimmyTheCat committed
            currentPlayer = currentPlayer == 1 ? 0 : 1;
        return endState;
    /**
     * Checks whether the current player has won the game.
     * 
     * @return current player won
     */
    public boolean victoryCheck() {
JimmyTheCat's avatar
JimmyTheCat committed
        boolean victory = false;
        for(int i = 0; i < 3; i++) {
            if(currentPlayer == tiles[0+i*3].getOwner() && currentPlayer == tiles[1+i*3].getOwner() && currentPlayer == tiles[2+i*3].getOwner()) victory = true;
            if(currentPlayer == tiles[0+i].getOwner() && currentPlayer == tiles[3+i].getOwner() && currentPlayer == tiles[6+i].getOwner()) victory = true;
JimmyTheCat's avatar
JimmyTheCat committed
        }
        if(currentPlayer == tiles[0].getOwner() && currentPlayer == tiles[4].getOwner() && currentPlayer == tiles[8].getOwner()) victory = true;
        if(currentPlayer == tiles[2].getOwner() && currentPlayer == tiles[4].getOwner() && currentPlayer == tiles[6].getOwner()) victory = true;
JimmyTheCat's avatar
JimmyTheCat committed
    }
Josua Oppermann's avatar
Josua Oppermann committed
}