@@ -187,41 +187,41 @@ func (brd *Board) ValidMove(m Move, inCheck bool) bool {
187187 case KING :
188188 if abs (to - from ) == 2 { // validate castle moves
189189 occ := brd .AllOccupied ()
190- if c == WHITE && (brd .castle & 12 ) > 0 {
190+ if c == WHITE && ! inCheck && (brd .castle & 12 ) > 0 {
191191 switch to {
192192 case C1 :
193- if ! inCheck && (brd .castle & C_WQ > uint8 (0 )) && castleQueensideIntervening [WHITE ]& brd .AllOccupied () == 0 &&
194- ! isAttackedBy (brd , occ , B1 , e , c ) && ! isAttackedBy (brd , occ , C1 , e , c ) && ! isAttackedBy (brd , occ , D1 , e , c ) {
193+ if (brd .castle & C_WQ > uint8 (0 )) && castleQueensideIntervening [WHITE ]& occ == 0 &&
194+ ! isAttackedBy (brd , occ , B1 , e , c ) && ! isAttackedBy (brd , occ , C1 , e , c ) &&
195+ ! isAttackedBy (brd , occ , D1 , e , c ) {
195196 return true
196197 }
197198 case G1 :
198- if ! inCheck && (brd .castle & C_WK > uint8 (0 )) && castleKingsideIntervening [WHITE ]& brd . AllOccupied () == 0 &&
199+ if (brd .castle & C_WK > uint8 (0 )) && castleKingsideIntervening [WHITE ]& occ == 0 &&
199200 ! isAttackedBy (brd , occ , F1 , e , c ) && ! isAttackedBy (brd , occ , G1 , e , c ) {
200201 return true
201202 }
202203 }
203- } else if c == BLACK && (brd .castle & 3 ) > 0 {
204+ } else if c == BLACK && ! inCheck && (brd .castle & 3 ) > 0 {
204205 switch to {
205206 case C8 :
206- if ! inCheck && (brd .castle & C_BQ > uint8 (0 )) && castleQueensideIntervening [BLACK ]& brd .AllOccupied () == 0 &&
207- ! isAttackedBy (brd , occ , B8 , e , c ) && ! isAttackedBy (brd , occ , C8 , e , c ) && ! isAttackedBy (brd , occ , D8 , e , c ) {
207+ if (brd .castle & C_BQ > uint8 (0 )) && castleQueensideIntervening [BLACK ]& occ == 0 &&
208+ ! isAttackedBy (brd , occ , B8 , e , c ) && ! isAttackedBy (brd , occ , C8 , e , c ) &&
209+ ! isAttackedBy (brd , occ , D8 , e , c ) {
208210 return true
209211 }
210212 case G8 :
211- if ! inCheck && (brd .castle & C_BK > uint8 (0 )) && castleKingsideIntervening [BLACK ]& brd . AllOccupied () == 0 &&
213+ if (brd .castle & C_BK > uint8 (0 )) && castleKingsideIntervening [BLACK ]& occ == 0 &&
212214 ! isAttackedBy (brd , occ , F8 , e , c ) && ! isAttackedBy (brd , occ , G8 , e , c ) {
213215 return true
214216 }
215217 }
216218 }
217- // fmt.Printf("Invalid castle move!{%s}. Castle rights: %d", m.ToString(), brd.castle)
218219 return false
219220 }
220- case KNIGHT : // no special treatment needed for knights.
221-
221+ case KNIGHT :
222+ // no special treatment needed for knights.
222223 default :
223224 if slidingAttacks (piece , brd .AllOccupied (), from )& sqMaskOn [to ] == 0 {
224- // fmt.Printf("Invalid sliding attack!{%s}", m.ToString())
225225 return false
226226 }
227227 }
0 commit comments