From f465bdecae30073371dede3e18f5501aa2bd5295 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Tue, 25 Aug 2026 19:15:00 +0200 Subject: [PATCH] feat(rules): add draw detection --- Draw.scala | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Draw.scala diff --git a/Draw.scala b/Draw.scala new file mode 100644 index 0000000..e0c1704 --- /dev/null +++ b/Draw.scala @@ -0,0 +1,24 @@ +package gambit + +object Draw: + def isFiftyMove(pos: Position): Boolean = pos.halfmove >= 100 + + def isInsufficientMaterial(pos: Position): Boolean = + var minors = 0 + var sq = 0 + while sq < 128 do + if Square.onBoard(sq) then + Piece.typeOf(pos.board(sq)) match + case Piece.Pawn | Piece.Rook | Piece.Queen => return false + case Piece.Knight | Piece.Bishop => minors += 1 + case _ => () + sq += 1 + minors <= 1 + + def isThreefold(history: collection.Seq[Long], current: Long): Boolean = + var count = 0 + var i = 0 + while i < history.length do + if history(i) == current then count += 1 + i += 1 + count >= 3