2 * Copyright (c) 2018. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.chess.position;
19 import java.util.HashMap;
25 * @author Dmitry Samoshin aka gotty
27 public class DiagramPosition implements Position {
29 protected final Map<Square, ColorPiece> squares = new HashMap<Square, ColorPiece>() {
31 put(Square.A1, null); put(Square.B1, null); put(Square.C1, null); put(Square.D1, null);
32 put(Square.E1, null); put(Square.F1, null); put(Square.G1, null); put(Square.H1, null);
33 put(Square.A2, null); put(Square.B2, null); put(Square.C2, null); put(Square.D2, null);
34 put(Square.E2, null); put(Square.F2, null); put(Square.G2, null); put(Square.H2, null);
35 put(Square.A3, null); put(Square.B3, null); put(Square.C3, null); put(Square.D3, null);
36 put(Square.E3, null); put(Square.F3, null); put(Square.G3, null); put(Square.H3, null);
37 put(Square.A4, null); put(Square.B4, null); put(Square.C4, null); put(Square.D4, null);
38 put(Square.E4, null); put(Square.F4, null); put(Square.G4, null); put(Square.H4, null);
39 put(Square.A5, null); put(Square.B5, null); put(Square.C5, null); put(Square.D5, null);
40 put(Square.E5, null); put(Square.F5, null); put(Square.G5, null); put(Square.H5, null);
41 put(Square.A6, null); put(Square.B6, null); put(Square.C6, null); put(Square.D6, null);
42 put(Square.E6, null); put(Square.F6, null); put(Square.G6, null); put(Square.H6, null);
43 put(Square.A7, null); put(Square.B7, null); put(Square.C7, null); put(Square.D7, null);
44 put(Square.E7, null); put(Square.F7, null); put(Square.G7, null); put(Square.H7, null);
45 put(Square.A8, null); put(Square.B8, null); put(Square.C8, null); put(Square.D8, null);
46 put(Square.E8, null); put(Square.F8, null); put(Square.G8, null); put(Square.H8, null);
53 public Map<Square, ColorPiece> getSquares() {
58 public Map<Square, ColorPiece> getSquarePieces(Color color) {
59 return getSquarePieces(color, null);
63 public Map<Square, ColorPiece> getSquarePieces(Color color, Piece piece) {
64 Map<Square, ColorPiece> squarePieces = new HashMap<>();
65 for (Map.Entry<Square, ColorPiece> entry : squares.entrySet()) {
66 Square square = entry.getKey();
67 ColorPiece colorPiece = entry.getValue();
68 if (colorPiece != null) {
69 if (color == null || color.equals(colorPiece.color()))
70 if (piece == null || piece.equals(colorPiece.piece()))
71 squarePieces.put(square, colorPiece);
78 public void setPiece(Color color, Piece piece, Square square) {
79 squares.put(square, ColorPiece.getColorPiece(color, piece));
83 public void setPiece(ColorPiece colorPiece, Square square) {
84 squares.put(square, colorPiece);
88 public Color getMove() {
93 public void setMove(Color color) {
98 public boolean isDiagram() {
103 public Castle getCastle(Color color) {
108 public void setCastle(Color color, Castle castle) {
109 // do not supported in this class
113 public Square getEnPassant() {
118 public void setEnPassant(Square square) {
119 // do not supported in this class
123 public int getHalfMove() {
128 public void setHalfMove(int count) {
129 // do not supported in this class
133 public int getFullMove() {
138 public void setFullMove(int number) {
139 // do not supported in this class
143 public void clear() {
144 for (Square square : Square.values()) {
145 squares.put(square, null);
150 public void initial() {
151 PositionBuilder.buildInitial(
156 static class PositionBuilder {
158 public static void buildInitial(Position position) {
161 Map<Square, ColorPiece> squares = position.getSquares();
163 squares.put(Square.A1, ColorPiece.WHITE_ROOK);
164 squares.put(Square.B1, ColorPiece.WHITE_KNIGHT);
165 squares.put(Square.C1, ColorPiece.WHITE_BISHOP);
166 squares.put(Square.D1, ColorPiece.WHITE_QUEEN);
167 squares.put(Square.E1, ColorPiece.WHITE_KING);
168 squares.put(Square.F1, ColorPiece.WHITE_BISHOP);
169 squares.put(Square.G1, ColorPiece.WHITE_KNIGHT);
170 squares.put(Square.H1, ColorPiece.WHITE_ROOK);
172 squares.put(Square.A8, ColorPiece.BLACK_ROOK);
173 squares.put(Square.B8, ColorPiece.BLACK_KNIGHT);
174 squares.put(Square.C8, ColorPiece.BLACK_BISHOP);
175 squares.put(Square.D8, ColorPiece.BLACK_QUEEN);
176 squares.put(Square.E8, ColorPiece.BLACK_KING);
177 squares.put(Square.F8, ColorPiece.BLACK_BISHOP);
178 squares.put(Square.G8, ColorPiece.BLACK_KNIGHT);
179 squares.put(Square.H8, ColorPiece.BLACK_ROOK);
181 for (Square square : Square.values()) {
182 if (square.getH() == Square.A2.getH())
183 squares.put(square, ColorPiece.WHITE_PAWN);
184 else if (square.getH() == Square.A7.getH())
185 squares.put(square, ColorPiece.BLACK_PAWN);
188 position.setMove(Color.WHITE);
189 position.setCastle(Color.WHITE, Castle.BOTH);
190 position.setCastle(Color.BLACK, Castle.BOTH);
191 position.setEnPassant(null);
192 position.setHalfMove(0);
193 position.setFullMove(1);