All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.wolfraam.chessgame.board.BoardData Maven / Gradle / Ivy

There is a newer version: 2.2
Show newest version
package io.github.wolfraam.chessgame.board;

import java.io.Serializable;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Map;
import java.util.Set;

/**
 * Chess board data, uses two maps for increased performance.
 */
public class BoardData implements Serializable, Cloneable {

    private final EnumMap> piece2Squares = new EnumMap<>(Piece.class);
    private final EnumMap square2Piece = new EnumMap<>(Square.class);

    @Override
    @SuppressWarnings("all")
    public BoardData clone() {
        final BoardData boardData = new BoardData();
        for (final Map.Entry entry : square2Piece.entrySet()) {
            boardData.putPieceOnSquare(entry.getKey(), entry.getValue());
        }
        return boardData;
    }

    public Set getOccupiedSquares() {
        return square2Piece.keySet();
    }

    public Piece getPiece(final Square square) {
        return square2Piece.get(square);
    }

    public Set getSquares(final Piece piece) {
        return piece2Squares.computeIfAbsent(piece, k -> EnumSet.noneOf(Square.class));
    }

    public void putPieceOnSquare(final Square square, final Piece piece) {
        final Piece previousPiece;
        if (piece != null) {
            previousPiece = square2Piece.put(square, piece);
        } else {
            previousPiece = square2Piece.remove(square);
        }
        if (previousPiece != null) {
            getSquares(previousPiece).remove(square);
        }
        if (piece != null) {
            getSquares(piece).add(square);
        }
    }

    public void removePieceFromSquare(final Square square) {
        putPieceOnSquare(square, null);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy