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

    /**This Integer defines which character can currently draw on a tile.
     * 
     */
    int currentPlayer;

    /**Constructor of the Board class.
     * Creates the Tiles and sets the player who starts the game.
     */
    Board() {
        for(int i = 0; i < tiles.length; i++) tiles[i] = new Tile();
        currentPlayer = 1;
    }

    /**Checks wether the current player has won the game after placing a mark.
     * When the player won the game is reset, else the current player is switched.
     */
    public void victoryCheck() {
        boolean victory = false;
        for(int i = 0; i < 3; i++) {
            if(currentPlayer == tiles[0+i*3].get_owner() && currentPlayer == tiles[1+i*3].get_owner() && currentPlayer == tiles[2+i*3].get_owner()) victory = true;
            if(currentPlayer == tiles[0+i].get_owner() && currentPlayer == tiles[3+i].get_owner() && currentPlayer == tiles[6+i].get_owner()) victory = true;
        }
        if(currentPlayer == tiles[0].get_owner() && currentPlayer == tiles[4].get_owner() && currentPlayer == tiles[8].get_owner()) victory = true;
        if(currentPlayer == tiles[2].get_owner() && currentPlayer == tiles[4].get_owner() && currentPlayer == tiles[6].get_owner()) victory = true;
        if(victory) {
            System.out.println("You win Player " + currentPlayer); 
        } else {
            currentPlayer = currentPlayer == 1 ? 2 : 1;
        }
    }
Josua Oppermann's avatar
Josua Oppermann committed
}