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;
21 import java.util.TreeMap;
26 * @author Dmitry Samoshin aka gotty
28 public class DiagramPosition implements Position {
30 protected final Map<Square, ColorPiece> squares = new HashMap<Square, ColorPiece>() {
32 put(Square.A1, null); put(Square.B1, null); put(Square.C1, null); put(Square.D1, null);
33 put(Square.E1, null); put(Square.F1, null); put(Square.G1, null); put(Square.H1, null);
34 put(Square.A2, null); put(Square.B2, null); put(Square.C2, null); put(Square.D2, null);
35 put(Square.E2, null); put(Square.F2, null); put(Square.G2, null); put(Square.H2, null);
36 put(Square.A3, null); put(Square.B3, null); put(Square.C3, null); put(Square.D3, null);
37 put(Square.E3, null); put(Square.F3, null); put(Square.G3, null); put(Square.H3, null);
38 put(Square.A4, null); put(Square.B4, null); put(Square.C4, null); put(Square.D4, null);
39 put(Square.E4, null); put(Square.F4, null); put(Square.G4, null); put(Square.H4, null);
40 put(Square.A5, null); put(Square.B5, null); put(Square.C5, null); put(Square.D5, null);
41 put(Square.E5, null); put(Square.F5, null); put(Square.G5, null); put(Square.H5, null);
42 put(Square.A6, null); put(Square.B6, null); put(Square.C6, null); put(Square.D6, null);
43 put(Square.E6, null); put(Square.F6, null); put(Square.G6, null); put(Square.H6, null);
44 put(Square.A7, null); put(Square.B7, null); put(Square.C7, null); put(Square.D7, null);
45 put(Square.E7, null); put(Square.F7, null); put(Square.G7, null); put(Square.H7, null);
46 put(Square.A8, null); put(Square.B8, null); put(Square.C8, null); put(Square.D8, null);
47 put(Square.E8, null); put(Square.F8, null); put(Square.G8, null); put(Square.H8, null);
54 public Map<Square, ColorPiece> getSquares() {
59 public Map<Square, ColorPiece> getSquares(SquareSort sorting) {
61 sorting = SquareSort.defaultSort();
63 TreeMap<Square, ColorPiece> sortedSquares = new TreeMap<>(
66 sortedSquares.putAll(squares);
72 public Map<Square, ColorPiece> getSquarePieces(Color color) {
73 return getSquarePieces(color, null);
77 public Map<Square, ColorPiece> getSquarePieces(Color color, Piece piece) {
78 Map<Square, ColorPiece> squarePieces = new HashMap<>();
79 for (Map.Entry<Square, ColorPiece> entry : squares.entrySet()) {
80 Square square = entry.getKey();
81 ColorPiece colorPiece = entry.getValue();
82 if (colorPiece != null) {
83 if (color == null || color.equals(colorPiece.color()))
84 if (piece == null || piece.equals(colorPiece.piece()))
85 squarePieces.put(square, colorPiece);
92 public void setPiece(Color color, Piece piece, Square square) {
93 squares.put(square, ColorPiece.getColorPiece(color, piece));
97 public void setPiece(ColorPiece colorPiece, Square square) {
98 squares.put(square, colorPiece);
102 public Color getMove() {
107 public void setMove(Color color) {
112 public boolean isDiagram() {
117 public Castle getCastle(Color color) {
122 public void setCastle(Color color, Castle castle) {
123 // do not supported in this class
127 public Square getEnPassant() {
132 public void setEnPassant(Square square) {
133 // do not supported in this class
137 public int getHalfMove() {
142 public void setHalfMove(int count) {
143 // do not supported in this class
147 public int getFullMove() {
152 public void setFullMove(int number) {
153 // do not supported in this class
157 public void clear() {
158 for (Square square : Square.values()) {
159 squares.put(square, null);
164 public void initial() {
165 PositionBuilder.buildInitial(
170 static class PositionBuilder {
172 public static void buildInitial(Position position) {
175 Map<Square, ColorPiece> squares = position.getSquares();
177 squares.put(Square.A1, ColorPiece.WHITE_ROOK);
178 squares.put(Square.B1, ColorPiece.WHITE_KNIGHT);
179 squares.put(Square.C1, ColorPiece.WHITE_BISHOP);
180 squares.put(Square.D1, ColorPiece.WHITE_QUEEN);
181 squares.put(Square.E1, ColorPiece.WHITE_KING);
182 squares.put(Square.F1, ColorPiece.WHITE_BISHOP);
183 squares.put(Square.G1, ColorPiece.WHITE_KNIGHT);
184 squares.put(Square.H1, ColorPiece.WHITE_ROOK);
186 squares.put(Square.A8, ColorPiece.BLACK_ROOK);
187 squares.put(Square.B8, ColorPiece.BLACK_KNIGHT);
188 squares.put(Square.C8, ColorPiece.BLACK_BISHOP);
189 squares.put(Square.D8, ColorPiece.BLACK_QUEEN);
190 squares.put(Square.E8, ColorPiece.BLACK_KING);
191 squares.put(Square.F8, ColorPiece.BLACK_BISHOP);
192 squares.put(Square.G8, ColorPiece.BLACK_KNIGHT);
193 squares.put(Square.H8, ColorPiece.BLACK_ROOK);
195 for (Square square : Square.values()) {
196 if (square.getH() == Square.A2.getH())
197 squares.put(square, ColorPiece.WHITE_PAWN);
198 else if (square.getH() == Square.A7.getH())
199 squares.put(square, ColorPiece.BLACK_PAWN);
202 position.setMove(Color.WHITE);
203 position.setCastle(Color.WHITE, Castle.BOTH);
204 position.setCastle(Color.BLACK, Castle.BOTH);
205 position.setEnPassant(null);
206 position.setHalfMove(0);
207 position.setFullMove(1);