chesskit-app / chesskit-swift

♟️ Swift package for implementing chess logic.
MIT License
7 stars 6 forks source link

Fixed insufficient material logic #27

Closed joee-ca closed 1 month ago

joee-ca commented 1 month ago

I just noticed that after your refactoring the logic to check if there is a draw by insufficient material is wrong. If there are no knights and the pieces are >= 4, the draw can happen only if all the bishops on the board are on the same color square. If all the white bishops are on the light squares and all the black bishops are on the dark squares it’s still not a draw. Let me show you:

Screenshot 2024-05-10 at 20 34 16

In this position black can make the move bishop a2, to which white can answer with bishop d5 checkmate.


// white has all bishops on light squares while black has all bishops on dark squares

let allWBLight = set.B & .dark == 0 // true
let allWBDark = set.B & .light == 0 // false
let allBBLight = set.b & .dark == 0 // false
let allBBDark = set.b & .light == 0 // true

(allWBLight && (allBBDark || allBBLight)) || (allWBDark && (allBBDark || allBBLight)) ->
(true && (true || false)) || (false && (true || false)) -> true //wrong

// as we just saw in the previous example this can't be true since it's not a forced draw

In addition to what I just said, there is also another problem. Let's say that white has 2...N bishops while black has none. The variables let allBBDark = set.b & .light == 0 and let allBBLight = set.b & .light == 0 will be true, which is not correct.

I hope everything is clear now. Let me know if you have any other doubt 😄

pdil commented 1 month ago

Oh yeah that's true, thanks for catching that!

joee-ca commented 1 month ago

I just fixed the XCTAssert tests in the BoardTests.swift file

joee-ca commented 1 month ago

I completely missed that 😅. Just fixed!