[LIB-9] Chessboard squares sorting
[chesshog.git] / src / main / java / org / hedgecode / chess / position / DiagramPosition.java
1 /*
2  * Copyright (c) 2018. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.position;
18
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.TreeMap;
22
23 /**
24  *
25  *
26  * @author Dmitry Samoshin aka gotty
27  */
28 public class DiagramPosition implements Position {
29
30     protected final Map<Square, ColorPiece> squares = new HashMap<Square, ColorPiece>() {
31         {
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);
48         }
49     };
50
51     private Color move;
52
53     @Override
54     public Map<Square, ColorPiece> getSquares() {
55         return squares;
56     }
57
58     @Override
59     public Map<Square, ColorPiece> getSquares(SquareSort sorting) {
60         if (sorting == null)
61             sorting = SquareSort.defaultSort();
62
63         TreeMap<Square, ColorPiece> sortedSquares = new TreeMap<>(
64                 sorting.comparator()
65         );
66         sortedSquares.putAll(squares);
67
68         return sortedSquares;
69     }
70
71     @Override
72     public Map<Square, ColorPiece> getSquarePieces(Color color) {
73         return getSquarePieces(color, null);
74     }
75
76     @Override
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);
86             }
87         }
88         return squarePieces;
89     }
90
91     @Override
92     public void setPiece(Color color, Piece piece, Square square) {
93         squares.put(square, ColorPiece.getColorPiece(color, piece));
94     }
95
96     @Override
97     public void setPiece(ColorPiece colorPiece, Square square) {
98         squares.put(square, colorPiece);
99     }
100
101     @Override
102     public Color getMove() {
103         return move;
104     }
105
106     @Override
107     public void setMove(Color color) {
108         move = color;
109     }
110
111     @Override
112     public boolean isDiagram() {
113         return true;
114     }
115
116     @Override
117     public Castle getCastle(Color color) {
118         return Castle.NONE;
119     }
120
121     @Override
122     public void setCastle(Color color, Castle castle) {
123         // do not supported in this class
124     }
125
126     @Override
127     public Square getEnPassant() {
128         return null;
129     }
130
131     @Override
132     public void setEnPassant(Square square) {
133         // do not supported in this class
134     }
135
136     @Override
137     public int getHalfMove() {
138         return 0;
139     }
140
141     @Override
142     public void setHalfMove(int count) {
143         // do not supported in this class
144     }
145
146     @Override
147     public int getFullMove() {
148         return 0;
149     }
150
151     @Override
152     public void setFullMove(int number) {
153         // do not supported in this class
154     }
155
156     @Override
157     public void clear() {
158         for (Square square : Square.values()) {
159             squares.put(square, null);
160         }
161     }
162
163     @Override
164     public void initial() {
165         PositionBuilder.buildInitial(
166                 this
167         );
168     }
169
170     static class PositionBuilder {
171
172         public static void buildInitial(Position position) {
173             position.clear();
174
175             Map<Square, ColorPiece> squares = position.getSquares();
176
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);
185
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);
194
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);
200             }
201
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);
208         }
209
210     }
211
212 }