001    package test;
002    
003    import java.util.Collection;
004    import java.util.Map;
005    
006    import rules.GameState;
007    
008    import core.Color;
009    import core.Move;
010    import core.Piece;
011    import core.Position;
012    
013    import junit.framework.TestCase;
014    
015    /**
016     * Unit tests for the GameState class.
017     **/
018    public class GameStateTest extends TestCase {
019    
020        
021    
022        public GameStateTest(String name) {
023            super(name);
024        }
025    
026        // JUnit calls setUp() before each test__ method is run
027        protected void setUp() {
028            
029        }
030    
031        /**
032         * Check that the move number increases as desired,
033         * and decreases when reverting a move.
034         */
035        public void testGetMoveNum(){ 
036            GameState g = new GameState();
037            assertEquals(g.getMoveNum(), 0);
038            g.makeMove(new Move());
039            assertEquals(g.getMoveNum(), 1);
040            g.reversibleMakeMove((new Move()).toMap(g), true);
041            assertEquals(g.getMoveNum(), 2);
042            g.reversibleMakeMove((new Move()).toMap(g), false);
043            assertEquals(g.getMoveNum(), 1);
044        }
045    
046        /**
047         * Check that the GameState can find when the wrong player is in
048         * check.
049         */
050        public void testInvalidState(){
051            GameState g = new GameState();
052            assertFalse(g.invalidState());
053            Move m = new Move(Position.get(4, 0),
054                              Position.get(1, 5));
055            Map<Position, Piece> map = m.toMap(g);
056            g.reversibleMakeMove(map, true);
057            assertTrue(g.invalidState());
058            g.reversibleMakeMove(map, false);
059            assertFalse(g.invalidState());
060        }
061    
062        /**
063         * Test that legalMoves doesn't modify the state,
064         * and is correct for an easy case.
065         */
066        public void testLegalMoves(){
067            GameState g = new GameState();
068            Collection<Move> s = g.legalMoves();
069            assertEquals(s.size(), 20);
070            assertEquals(g.toString(),
071                         new GameState().toString());
072        }
073    
074        /**
075         * Test tostring after a move
076         */
077        public void testToString(){
078            GameState g = new GameState();
079            assertEquals(g.toString(), "rnbqkbnr\npppppppp\n        \n        \n        \n        \nPPPPPPPP\nRNBQKBNR\n");
080            Move m = new Move(Position.get(4, 0),
081                              Position.get(1, 5));
082            g.makeMove(m);
083            assertEquals(g.toString(), "rnbqkbnr\npppppppp\n K      \n        \n        \n        \nPPPPPPPP\nRNBQ BNR\n");
084        }
085    
086        /**
087         * Check that you can castle when not passing through check, only
088         * if neither rook nor king has moved before.
089         */
090        public void testCastle(){
091            GameState g = new GameState(Color.WHITE, 1,
092                                        "rnbqkbnr\n"+
093                                        "pppppppp\n"+
094                                        "        \n"+
095                                        "        \n"+
096                                        "        \n"+
097                                        "        \n"+
098                                        "PPPPPPPP\n"+
099                                        "R   K  R\n");
100            assertEquals(g.legalMoves().size(), 25);
101            Map<Position, Piece> map1 = (new Move(Position.get(7, 0),
102                                                  Position.get(6, 0))).toMap(g);
103            g.reversibleMakeMove(map1, true);
104            Map<Position, Piece> map2 = (new Move(Position.get(0, 6),
105                                                  Position.get(0, 5))).toMap(g);
106            assertEquals(g.legalMoves().size(), 20);
107            g.reversibleMakeMove(map2, true);
108            Map<Position, Piece> map3 = (new Move(Position.get(6, 0),
109                                                  Position.get(7, 0))).toMap(g);
110            assertEquals(g.legalMoves().size(), 24);
111            g.reversibleMakeMove(map3, true);
112            Map<Position, Piece> map4 = (new Move(Position.get(0, 5),
113                                                  Position.get(0, 4))).toMap(g);
114            assertEquals(g.legalMoves().size(), 19);
115            g.reversibleMakeMove(map4, true);  //Now that rook has moved, not possible
116            assertEquals(g.legalMoves().size(), 24);
117            g.reversibleMakeMove(map4, false);
118            g.reversibleMakeMove(map3, false);
119            g.reversibleMakeMove(map2, false);
120            g.reversibleMakeMove(map1, false);
121            assertEquals(g.legalMoves().size(), 25); //can undo to that state
122            
123    
124            g = new GameState(Color.WHITE, 1,
125                                        "rnbqkbn \n"+
126                                        "ppppprpp\n"+
127                                        "        \n"+
128                                        "        \n"+
129                                        "        \n"+
130                                        "        \n"+
131                                        "PPPPP PP\n"+
132                                        "R   K  R\n");
133            assertEquals(g.legalMoves().size(), 21);
134            g = new GameState(Color.BLACK, 1,
135                                        "r   k  r\n"+
136                                        "    P   \n"+
137                                        "   P    \n"+
138                                        "        \n"+
139                                        "        \n"+
140                                        "        \n"+
141                                        "        \n"+
142                                        "    K   \n");
143            assertEquals(g.legalMoves().size(), 21);
144            //
145            g = new GameState(Color.BLACK, 1,
146                                        "    k   \n"+
147                                        "        \n"+
148                                        "        \n"+
149                                        "     p  \n"+
150                                        "     P  \n"+
151                                        "        \n"+
152                                        "        \n"+
153                                        "    K   \n");
154            assertEquals(g.legalMoves().size(), 5);
155        }
156    
157        /**
158         * Test that the ability to capture forces the move.
159         */
160        public void testForcing(){
161            String s = "" +
162                "rnbqkbnr\n"+
163                "pppppppp\n"+
164                "    N   \n"+
165                "   B    \n"+
166                "        \n"+
167                "        \n"+
168                " PPPPPP \n"+
169                "R   K  R\n";
170            GameState g = new GameState(Color.WHITE, 1,
171                                        s);
172            g.legalMoves();
173            assertEquals(g.toString(), s);
174            assertEquals(g.legalMoves().size(), 7);
175            g = new GameState(Color.BLACK, 1,
176                                        s);
177            g.legalMoves();
178            assertEquals(g.toString(), s);
179            assertEquals(g.legalMoves().size(), 2);
180        }
181    
182        /**
183         * Test that en passant works, but expires.
184         */
185        public void testEnPassant(){
186            GameState g = new GameState(Color.BLACK, 10,
187                                        "r bqkbnr\n"+
188                                        "pppppppp\n"+
189                                        "        \n"+
190                                        " P      \n"+
191                                        "        \n"+
192                                        "        \n"+
193                                        "  PP PP \n"+
194                                        "  nK    \n");
195            assertEquals(g.legalMoves().size(), 22);
196            Move m = new Move(Position.get(2, 6),
197                              Position.get(2, 5));
198            Map<Position, Piece> map = m.toMap(g);
199            g.reversibleMakeMove(map, true);
200            //for(Move m2: g.legalMoves()){
201            //  System.out.println("Move:");
202            //  for(Map.Entry<Position, Piece> e: m2.entrySet()){
203            //    System.out.println(e.getKey() + ": " + e.getValue());
204            //  }
205            //}
206            assertEquals(g.legalMoves().size(), 2);
207            g.reversibleMakeMove(map, false);
208            m = new Move(Position.get(2, 6),
209                         Position.get(2, 4));
210            g.makeMove(m);
211            assertEquals(g.legalMoves().size(), 2);
212            m = new Move(Position.get(3, 0),
213                         Position.get(2, 0));
214            g.makeMove(m);
215            assertEquals(g.legalMoves().size(), 20);
216            m = new Move(Position.get(3, 7),
217                         Position.get(2, 6));
218            g.makeMove(m);
219            //En Passant has expired
220            assertEquals(g.legalMoves().size(), 12);
221            
222            
223        }
224    
225        /**
226         * Test that pawns promote at the end.
227         */
228        public void testQueening(){
229            GameState g = new GameState(Color.BLACK, 10,
230                                        "      kB\n"+
231                                        "      P \n"+
232                                        "      P \n"+
233                                        "        \n"+
234                                        "        \n"+
235                                        "        \n"+
236                                        "      p \n"+
237                                        "   K    \n");
238            Collection<Move> moves = g.legalMoves();
239            assertEquals(moves.size(), 1);
240            for(Move m: moves){
241                g.makeMove(m);
242                assertEquals(g.toString(),
243                             "      kB\n"+
244                             "      P \n"+
245                             "      P \n"+
246                             "        \n"+
247                             "        \n"+
248                             "        \n"+
249                             "        \n"+
250                             "   K  q \n");
251            }
252        }
253    
254        /**
255         * Test that isLegal works.
256         */
257        public void testIsLegal(){
258            GameState g = new GameState(Color.BLACK, 10,
259                                        "    k  r\n"+
260                                        "p       \n"+
261                                        "     P  \n"+
262                                        "   p    \n"+
263                                        "   P    \n"+
264                                        "        \n"+
265                                        "        \n"+
266                                        "   K    \n");
267            Move m;
268            m = new Move(Position.get(4, 7),
269                         Position.get(4, 6));
270            assertFalse(g.isLegal(m));
271    
272            m = new Move(Position.get(4, 7),
273                         Position.get(5, 6));
274            assertTrue(g.isLegal(m));
275            m = new Move(Position.get(4, 7),
276                         Position.get(6, 7));
277            assertTrue(g.isLegal(m));
278            m = new Move(Position.get(0, 6),
279                         Position.get(0, 5));
280            assertTrue(g.isLegal(m));
281            m = new Move(Position.get(0, 6),
282                         Position.get(0, 4));
283            assertTrue(g.isLegal(m));
284            m = new Move( Position.get(0, 6),
285                          Position.get(0, 7));
286            assertFalse(g.isLegal(m));
287            m = new Move( Position.get(0, 3),
288                          Position.get(0, 4));
289            assertFalse(g.isLegal(m));
290            
291            for(Move m2: g.legalMoves()){
292                assertTrue(g.isLegal(m2));
293            }
294        }
295    
296        /**
297         * Test when you are forced to pass.
298         */
299        public void testNoMoves(){
300            GameState g = new GameState(Color.BLACK, 10,
301                                        "      kB\n"+
302                                        "      P \n"+
303                                        "      P \n"+
304                                        "        \n"+
305                                        "        \n"+
306                                        "        \n"+
307                                        "        \n"+
308                                        "   K    \n");
309            assertEquals(g.legalMoves().size(), 1);
310            assertTrue(g.isLegal(new Move()));
311            assertFalse(g.canMove());
312        }
313        
314        /**
315         * Test the game's detection of the winner, in the various cases.
316         */
317        public void testWinner(){
318            GameState g = new GameState(Color.WHITE, 10,
319                                        "      kB\n"+
320                                        "      P \n"+
321                                        "      P \n"+
322                                        "        \n"+
323                                        "        \n"+
324                                        "        \n"+
325                                        "        \n"+
326                                        "   K    \n");
327            g = new GameState(Color.BLACK, 10,
328                                        "      kB\n"+
329                                        "p    PP \n"+
330                                        "      P \n"+
331                                        "        \n"+
332                                        "        \n"+
333                                        "        \n"+
334                                        "        \n"+
335                                        "   K    \n");
336    //      for(Move m2: g.legalMoves()){
337    //          System.out.println("Move:");
338    //          for(Map.Entry<Position, Piece> e: m2.entrySet()){
339    //              System.out.println(e.getKey() + ": " + e.getValue());
340    //          }
341    //      }
342            assertEquals(g.piecesWinner(), Color.NONE);
343            assertEquals(g.checkmater(), Color.WHITE);
344            assertEquals(g.getWinner(), Color.WHITE);
345            g = new GameState(Color.BLACK, 10,
346                                        "      kB\n"+
347                                        "     PP \n"+
348                                        "      P \n"+
349                                        "        \n"+
350                                        "        \n"+
351                                        "        \n"+
352                                        "        \n"+
353                                        "   K    \n");
354            assertEquals(g.piecesWinner(), Color.BLACK);
355            assertEquals(g.checkmater(), Color.WHITE);
356            assertEquals(g.getWinner(), Color.WHITE);
357        }
358            
359    
360        /**
361         * Test that pawns promote at the end.
362         */
363        public void testPowerups(){
364            GameState g = new GameState(Color.WHITE, 10,
365                                        "      k \n"+
366                                        "   r    \n"+
367                                        "  s     \n"+
368                                        "  P     \n"+
369                                        "        \n"+
370                                        "        \n"+
371                                        "        \n"+
372                                        "   K    \n");
373            assertEquals(g.legalMoves().size(), 5);
374            g.makeMove(Move.fromString("c5-c6"));
375            assertEquals(g.toString(),  "      k \n"+
376                                        "   r    \n"+
377                                        "  PP    \n"+
378                                        "        \n"+
379                                        "        \n"+
380                                        "        \n"+
381                                        "        \n"+
382                                        "   K    \n");
383    
384            g = new GameState(Color.WHITE, 10,
385                                        "       k\n"+
386                                        "        \n"+
387                                        "  d   p \n"+
388                                        "   B  P \n"+
389                                        "        \n"+
390                                        "        \n"+
391                                        "        \n"+
392                                        "   K    \n");
393            assertEquals(g.legalMoves().size(), 18);
394            g.makeMove(Move.fromString("d5-c6"));
395            assertEquals(g.toString(),  "       k\n"+
396                                        "        \n"+
397                                        "      p \n"+
398                                        "      P \n"+
399                                        "        \n"+
400                                        "        \n"+
401                                        "        \n"+
402                                        "   K    \n");
403    
404            g = new GameState(Color.WHITE, 10,
405                                        "      k \n"+
406                                        "        \n"+
407                                        "      p \n"+
408                                        "      P \n"+
409                                        " N      \n"+
410                                        "        \n"+
411                                        "  u     \n"+
412                                        "   K    \n");
413            assertEquals(g.legalMoves().size(), 11);
414            g.makeMove(Move.fromString("b4-c2"));
415            assertEquals(g.toString(),  "      k \n"+
416                                        "        \n"+
417                                        "      p \n"+
418                                        "      P \n"+
419                                        "        \n"+
420                                        "        \n"+
421                                        "  Q     \n"+
422                                        "   K    \n");
423        }
424    
425    
426    }