Skip to content

Commit a08df6b

Browse files
Add isDrawByFiftyMoves method (#477)
* feat(draw by fifty moves) : DONE - create a new function to check if it's a draw by fifty moves rule or not. - this will help UI devs to show exact reason of a draw. * - docs(add fifty moves rule example) : DONE --------- Co-authored-by: HarshilSySCreations <[email protected]>
1 parent 85c049e commit a08df6b

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Diff for: README.md

+10
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,16 @@ chess.isDraw()
492492
// -> true
493493
```
494494

495+
### .isDrawByFiftyMoves()
496+
497+
Returns true or false if the game is drawn by 50-move rule.
498+
499+
```ts
500+
const chess = new Chess('4k3/4P3/4K3/8/8/8/8/8 b - - 0 78')
501+
chess.isDrawByFiftyMoves()
502+
// -> true
503+
```
504+
495505
### .isInsufficientMaterial()
496506

497507
Returns true if the game is drawn due to insufficient material (K vs. K, K vs.

Diff for: src/chess.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1086,16 +1086,20 @@ export class Chess {
10861086
return this._getPositionCount(this.fen()) >= 3
10871087
}
10881088

1089-
isDraw() {
1089+
isDrawByFiftyMoves(): boolean {
1090+
return this._halfMoves >= 100 // 50 moves per side = 100 half moves
1091+
}
1092+
1093+
isDraw(): boolean {
10901094
return (
1091-
this._halfMoves >= 100 || // 50 moves per side = 100 half moves
1095+
this.isDrawByFiftyMoves() ||
10921096
this.isStalemate() ||
10931097
this.isInsufficientMaterial() ||
10941098
this.isThreefoldRepetition()
10951099
)
10961100
}
10971101

1098-
isGameOver() {
1102+
isGameOver(): boolean {
10991103
return this.isCheckmate() || this.isStalemate() || this.isDraw()
11001104
}
11011105

0 commit comments

Comments
 (0)