@@ -198,9 +198,32 @@ private void AddLegalMoves(List<Move> moves, int squareIndex)
198198 AddWhitePawnMoves ( moves , squareIndex ) ;
199199 AddWhitePawnAttacks ( moves , squareIndex ) ;
200200 break ;
201+ case Piece . BlackKing :
202+ case Piece . WhiteKing :
203+ AddKingMoves ( moves , squareIndex ) ;
204+ break ;
201205 }
202206 }
203207
208+ //*****************
209+ //** KING MOVES ***
210+ //*****************
211+
212+ int [ ] KING_MOVES_X = new int [ 8 ] { - 1 , 0 , 1 , 1 , 1 , 0 , - 1 , - 1 } ;
213+ int [ ] KING_MOVES_Y = new int [ 8 ] { - 1 , - 1 , - 1 , 0 , 1 , 1 , 1 , 0 } ;
214+
215+ private void AddKingMoves ( List < Move > moves , int index )
216+ {
217+ Piece self = _state [ index ] ;
218+ for ( int i = 0 ; i < 8 ; i ++ )
219+ if ( TryTransform ( index , KING_MOVES_X [ i ] , KING_MOVES_Y [ i ] , out int target ) && IsValidTarget ( self , target ) )
220+ moves . Add ( new Move ( index , target ) ) ;
221+ }
222+
223+ //*****************
224+ //** PAWN MOVES ***
225+ //*****************
226+
204227 private void AddWhitePawnMoves ( List < Move > moves , int index )
205228 {
206229 //if the square above isn't free there are no legal moves
@@ -276,6 +299,19 @@ private void AddWhitePawnMove(List<Move> moves, Move move)
276299
277300 private bool IsWhitePiece ( in int index ) => _state [ index ] != Piece . None && _state [ index ] < Piece . BlackPawn ;
278301
302+ private bool IsValidTarget ( Piece self , int index )
303+ {
304+ //white * white > 0 && < 5 * 6
305+ //black * black >
306+ Piece target = _state [ index ] ;
307+ if ( target == Piece . None )
308+ return true ;
309+ if ( target < Piece . BlackPawn ) //occupied by white!
310+ return self >= Piece . BlackPawn ; //self must be black or it can't land there
311+ else //occupied by black
312+ return self < Piece . BlackPawn ;
313+ }
314+
279315 //Index Helper
280316
281317 private int Up ( in int index ) => index + 8 ;
0 commit comments