Package eu.semberal.migmang.logic

Source Code of eu.semberal.migmang.logic.Board

package eu.semberal.migmang.logic;

import eu.semberal.migmang.enums.GameColor;

/**
* Board representation
*
* @author lukas.sembera
*
*/
public class Board {

    private GameColor[] squares;

    /**
     * Constructs board in initial setup
     */
    public Board() {
        squares = new GameColor[81];
        for (int i = 0; i < 81; i++) {
            if (i % 9 == 0) {
                squares[i] = GameColor.White;
            } else if (i % 9 == 8) {
                squares[i] = GameColor.Black;
            } else if (i / 9 == 8) {
                squares[i] = GameColor.Black;
            } else if (i / 9 == 0) {
                squares[i] = GameColor.White;
            } else {
                squares[i] = null;
            }

        }
    }

    /**
     * Copy constructor
     */
    public Board(Board s) {
        squares = new GameColor[81];
        for (int i = 0; i < 81; i++) {
            this.squares[i] = s.getSquare(i);
        }
    }

    /**
     * Applies a move. Just moves a piece, doesn't check validity, captures, etc.
     */
    public Board addMoveWithoutCaptures(Move t) {
        this.setPiece(t.getTo(), this.getSquare(t.getFrom()));
        this.setPiece(t.getFrom(), null);
        return this;
    }

    /**
     * Applies a move, including captures
     */
    public Board addMoveWithCaptures(Move t) {
        this.addMoveWithoutCaptures(t);
        for (int i : t.getCapturedIndexes()) {
            this.setPiece(i, null);
        }
        return this;
    }

    /**
     * Calculates how many pieces of specific color there is on the board
     */
    public int calculatePiecesOnBoard(GameColor color) {
        int counter = 0;
        for (int i = 0; i < 81; i++) {
            if (this.getSquare(i) == color) {
                counter++;
            }
        }
        return counter;
    }

    /**
     * Reverts a move on current board
     */
    public Board removeMoveAdvanced(Move move) {
        GameColor deletedColor = (move.getColor() == GameColor.White ? GameColor.Black : GameColor.White);
        addMoveWithoutCaptures(new Move(move.getTo(), move.getFrom(), move.getColor()));
        for (int i : move.getCapturedIndexes()) {
            this.setPiece(i, deletedColor);
        }
        return this;
    }

    /**
     * Returns the piece from specified square
     */
    public GameColor getSquare(int index) {
        return squares[index];
    }

    /**
     * Returns the count of pieces of specific color on the board
     */
    public int getPiecesCount(GameColor color) {
        int count = 0;
        for (GameColor b : squares) {
            if (b == color) {
                count++;
            }
        }

        return count;
    }

    /**
     * Sets piece to a square
     */
    public void setPiece(int square, GameColor value) {
        squares[square] = value;
    }
}
TOP

Related Classes of eu.semberal.migmang.logic.Board

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.