/* * Copyright (c) 2018. Developed by Hedgecode. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.hedgecode.chess.position; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; /** * * * @author Dmitry Samoshin aka gotty */ public class DiagramPosition implements Position { protected final Map squares = new HashMap() { { put(Square.A1, null); put(Square.B1, null); put(Square.C1, null); put(Square.D1, null); put(Square.E1, null); put(Square.F1, null); put(Square.G1, null); put(Square.H1, null); put(Square.A2, null); put(Square.B2, null); put(Square.C2, null); put(Square.D2, null); put(Square.E2, null); put(Square.F2, null); put(Square.G2, null); put(Square.H2, null); put(Square.A3, null); put(Square.B3, null); put(Square.C3, null); put(Square.D3, null); put(Square.E3, null); put(Square.F3, null); put(Square.G3, null); put(Square.H3, null); put(Square.A4, null); put(Square.B4, null); put(Square.C4, null); put(Square.D4, null); put(Square.E4, null); put(Square.F4, null); put(Square.G4, null); put(Square.H4, null); put(Square.A5, null); put(Square.B5, null); put(Square.C5, null); put(Square.D5, null); put(Square.E5, null); put(Square.F5, null); put(Square.G5, null); put(Square.H5, null); put(Square.A6, null); put(Square.B6, null); put(Square.C6, null); put(Square.D6, null); put(Square.E6, null); put(Square.F6, null); put(Square.G6, null); put(Square.H6, null); put(Square.A7, null); put(Square.B7, null); put(Square.C7, null); put(Square.D7, null); put(Square.E7, null); put(Square.F7, null); put(Square.G7, null); put(Square.H7, null); put(Square.A8, null); put(Square.B8, null); put(Square.C8, null); put(Square.D8, null); put(Square.E8, null); put(Square.F8, null); put(Square.G8, null); put(Square.H8, null); } }; private Color move; @Override public Map getSquares() { return squares; } @Override public Map getSquares(SquareSort sorting) { if (sorting == null) sorting = SquareSort.defaultSort(); TreeMap sortedSquares = new TreeMap<>( sorting.comparator() ); sortedSquares.putAll(squares); return sortedSquares; } @Override public Map getSquarePieces(Color color) { return getSquarePieces(color, null); } @Override public Map getSquarePieces(Color color, Piece piece) { Map squarePieces = new HashMap<>(); for (Map.Entry entry : squares.entrySet()) { Square square = entry.getKey(); ColorPiece colorPiece = entry.getValue(); if (colorPiece != null) { if (color == null || color.equals(colorPiece.color())) if (piece == null || piece.equals(colorPiece.piece())) squarePieces.put(square, colorPiece); } } return squarePieces; } @Override public void setPiece(Color color, Piece piece, Square square) { squares.put(square, ColorPiece.getColorPiece(color, piece)); } @Override public void setPiece(ColorPiece colorPiece, Square square) { squares.put(square, colorPiece); } @Override public Color getMove() { return move; } @Override public void setMove(Color color) { move = color; } @Override public boolean isDiagram() { return true; } @Override public Castle getCastle(Color color) { return Castle.NONE; } @Override public void setCastle(Color color, Castle castle) { // do not supported in this class } @Override public Square getEnPassant() { return null; } @Override public void setEnPassant(Square square) { // do not supported in this class } @Override public int getHalfMove() { return 0; } @Override public void setHalfMove(int count) { // do not supported in this class } @Override public int getFullMove() { return 0; } @Override public void setFullMove(int number) { // do not supported in this class } @Override public void clear() { for (Square square : Square.values()) { squares.put(square, null); } } @Override public void initial() { PositionBuilder.buildInitial( this ); } static class PositionBuilder { public static void buildInitial(Position position) { position.clear(); Map squares = position.getSquares(); squares.put(Square.A1, ColorPiece.WHITE_ROOK); squares.put(Square.B1, ColorPiece.WHITE_KNIGHT); squares.put(Square.C1, ColorPiece.WHITE_BISHOP); squares.put(Square.D1, ColorPiece.WHITE_QUEEN); squares.put(Square.E1, ColorPiece.WHITE_KING); squares.put(Square.F1, ColorPiece.WHITE_BISHOP); squares.put(Square.G1, ColorPiece.WHITE_KNIGHT); squares.put(Square.H1, ColorPiece.WHITE_ROOK); squares.put(Square.A8, ColorPiece.BLACK_ROOK); squares.put(Square.B8, ColorPiece.BLACK_KNIGHT); squares.put(Square.C8, ColorPiece.BLACK_BISHOP); squares.put(Square.D8, ColorPiece.BLACK_QUEEN); squares.put(Square.E8, ColorPiece.BLACK_KING); squares.put(Square.F8, ColorPiece.BLACK_BISHOP); squares.put(Square.G8, ColorPiece.BLACK_KNIGHT); squares.put(Square.H8, ColorPiece.BLACK_ROOK); for (Square square : Square.values()) { if (square.getH() == Square.A2.getH()) squares.put(square, ColorPiece.WHITE_PAWN); else if (square.getH() == Square.A7.getH()) squares.put(square, ColorPiece.BLACK_PAWN); } position.setMove(Color.WHITE); position.setCastle(Color.WHITE, Castle.BOTH); position.setCastle(Color.BLACK, Castle.BOTH); position.setEnPassant(null); position.setHalfMove(0); position.setFullMove(1); } } }