99import boardgame .Position ;
1010import chess .pieces .Bishop ;
1111import chess .pieces .King ;
12+ import chess .pieces .Knight ;
1213import chess .pieces .Pawn ;
1314import chess .pieces .Rook ;
1415
@@ -81,7 +82,7 @@ public ChessPiece performChessMove(ChessPosition sourcePosition, ChessPosition t
8182 checkMate = true ;
8283 }
8384 else {
84- nextTurn ();
85+ nextTurn ();
8586 }
8687
8788 return (ChessPiece )capturedPiece ;
@@ -101,12 +102,11 @@ private Piece makeMove(Position source, Position target) {
101102 }
102103
103104 private void undoMove (Position source , Position target , Piece capturedPiece ) {
104- ChessPiece p = (ChessPiece )board .removePiece (target );
105- p .decreaseMoveCount ();
105+ Piece p = board .removePiece (target );
106106 board .placePiece (p , source );
107-
108- if ( CapturedPieces != null ) {
109- board .placePiece (capturedPiece , source );
107+
108+ if ( capturedPiece != null ) {
109+ board .placePiece (capturedPiece , target );
110110 CapturedPieces .remove (capturedPiece );
111111 piecesOnTheBoard .add (capturedPiece );
112112 }
@@ -139,54 +139,53 @@ private void nextTurn() {
139139 private Color opponent (Color color ) {
140140 return (color == Color .WHITE ) ? Color .BLACK : Color .WHITE ;
141141 }
142-
143- private ChessPiece King (Color color ) {
142+
143+ private ChessPiece king (Color color ) {
144144 List <Piece > list = piecesOnTheBoard .stream ().filter (x -> ((ChessPiece )x ).getColor () == color ).collect (Collectors .toList ());
145- for (Piece p : list ) {
146- if (p instanceof King ) {
145+ for (Piece p : list ) {
146+ if (p instanceof King ) {
147147 return (ChessPiece )p ;
148148 }
149-
150- }throw new IllegalStateException ("There is no " + color + " king on the board" );
151-
149+ }
150+ throw new IllegalStateException ("There is no " + color + " king on the board" );
152151 }
153-
152+
154153 private boolean testCheck (Color color ) {
155- Position kingPosition = King (color ).getChessPosition ().toPosition ();
154+ Position kingPosition = king (color ).getChessPosition ().toPosition ();
156155 List <Piece > opponentPieces = piecesOnTheBoard .stream ().filter (x -> ((ChessPiece )x ).getColor () == opponent (color )).collect (Collectors .toList ());
157156 for (Piece p : opponentPieces ) {
158157 boolean [][] mat = p .possibleMoves ();
159- if (mat [kingPosition .getRow ()][kingPosition .getColumn ()]) {
158+ if (mat [kingPosition .getRow ()][kingPosition .getColumn ()]) {
160159 return true ;
161160 }
162161 }
163162 return false ;
164163 }
165164
166165 private boolean testCheckMate (Color color ) {
167- if (!testCheck (color )) {
166+ if (!testCheck (color )) {
168167 return false ;
169168 }
170169 List <Piece > list = piecesOnTheBoard .stream ().filter (x -> ((ChessPiece )x ).getColor () == color ).collect (Collectors .toList ());
171- for (Piece p : list ) {
170+ for (Piece p : list ) {
172171 boolean [][] mat = p .possibleMoves ();
173- for (int i =0 ; i <board .getRows (); i ++) {
174- for (int j =0 ; j <board .getColumns (); j ++) {
175- if (mat [i ][j ]) {
172+ for (int i =0 ; i <board .getRows (); i ++) {
173+ for (int j =0 ; j <board .getColumns (); j ++) {
174+ if (mat [i ][j ]) {
176175 Position source = ((ChessPiece )p ).getChessPosition ().toPosition ();
177176 Position target = new Position (i , j );
178177 Piece capturedPiece = makeMove (source , target );
179178 boolean testCheck = testCheck (color );
180179 undoMove (source , target , capturedPiece );
181- if (!testCheck ) {
180+ if (!testCheck ) {
182181 return false ;
183182 }
184183 }
185184 }
186185 }
187186 }
188187 return true ;
189- }
188+ }
190189
191190 private void placeNewPiece (char column , int row , ChessPiece piece ) {
192191 board .placePiece (piece , new ChessPosition (column , row ).toPosition ());
@@ -195,9 +194,11 @@ private void placeNewPiece(char column, int row, ChessPiece piece) {
195194
196195 private void initialSetup () {
197196 placeNewPiece ('a' , 1 , new Rook (board , Color .WHITE ));
197+ placeNewPiece ('b' , 1 , new Knight (board , Color .WHITE ));
198198 placeNewPiece ('c' , 1 , new Bishop (board , Color .WHITE ));
199199 placeNewPiece ('e' , 1 , new King (board , Color .WHITE ));
200200 placeNewPiece ('f' , 1 , new Bishop (board , Color .WHITE ));
201+ placeNewPiece ('g' , 1 , new Knight (board , Color .WHITE ));
201202 placeNewPiece ('h' , 1 , new Rook (board , Color .WHITE ));
202203 placeNewPiece ('a' , 2 , new Pawn (board , Color .WHITE ));
203204 placeNewPiece ('b' , 2 , new Pawn (board , Color .WHITE ));
@@ -210,9 +211,11 @@ private void initialSetup() {
210211
211212
212213 placeNewPiece ('a' , 8 , new Rook (board , Color .BLACK ));
214+ placeNewPiece ('b' , 8 , new Knight (board , Color .BLACK ));
213215 placeNewPiece ('c' , 8 , new Bishop (board , Color .BLACK ));
214216 placeNewPiece ('e' , 8 , new King (board , Color .BLACK ));
215217 placeNewPiece ('f' , 8 , new Bishop (board , Color .BLACK ));
218+ placeNewPiece ('g' , 8 , new Knight (board , Color .BLACK ));
216219 placeNewPiece ('h' , 8 , new Rook (board , Color .BLACK ));
217220 placeNewPiece ('a' , 7 , new Pawn (board , Color .BLACK ));
218221 placeNewPiece ('b' , 7 , new Pawn (board , Color .BLACK ));
0 commit comments