e12b477678edec971737305217782b6320fab670
[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
22 /**
23  *
24  *
25  * @author Dmitry Samoshin aka gotty
26  */
27 public class DiagramPosition implements Position {
28
29     protected final Map<Square, ColorPiece> squares = new HashMap<Square, ColorPiece>() {
30         {
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);
47         }
48     };
49
50     private Color move;
51
52     @Override
53     public Map<Square, ColorPiece> getSquares() {
54         return squares;
55     }
56
57     @Override
58     public Map<Square, ColorPiece> getSquarePieces(Color color) {
59         return getSquarePieces(color, null);
60     }
61
62     @Override
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);
72             }
73         }
74         return squarePieces;
75     }
76
77     @Override
78     public void setPiece(Color color, Piece piece, Square square) {
79         squares.put(square, ColorPiece.getColorPiece(color, piece));
80     }
81
82     @Override
83     public void setPiece(ColorPiece colorPiece, Square square) {
84         squares.put(square, colorPiece);
85     }
86
87     @Override
88     public Color getMove() {
89         return move;
90     }
91
92     @Override
93     public void setMove(Color color) {
94         move = color;
95     }
96
97     @Override
98     public boolean isDiagram() {
99         return true;
100     }
101
102     @Override
103     public Castle getCastle(Color color) {
104         return Castle.NONE;
105     }
106
107     @Override
108     public void setCastle(Color color, Castle castle) {
109         // do not supported in this class
110     }
111
112     @Override
113     public Square getEnPassant() {
114         return null;
115     }
116
117     @Override
118     public void setEnPassant(Square square) {
119         // do not supported in this class
120     }
121
122     @Override
123     public int getHalfMove() {
124         return 0;
125     }
126
127     @Override
128     public void setHalfMove(int count) {
129         // do not supported in this class
130     }
131
132     @Override
133     public int getFullMove() {
134         return 0;
135     }
136
137     @Override
138     public void setFullMove(int number) {
139         // do not supported in this class
140     }
141
142     @Override
143     public void clear() {
144         for (Square square : Square.values()) {
145             squares.put(square, null);
146         }
147     }
148
149     @Override
150     public void initial() {
151         PositionBuilder.buildInitial(
152                 this
153         );
154     }
155
156     static class PositionBuilder {
157
158         public static void buildInitial(Position position) {
159             position.clear();
160
161             Map<Square, ColorPiece> squares = position.getSquares();
162
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);
171
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);
180
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);
186             }
187
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);
194         }
195
196     }
197
198 }