Skip to content

Commit 4017772

Browse files
committed
Checkmate detection
1 parent 5b1e4d2 commit 4017772

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
# X3139 Chess
22

33
15 sliding puzzle, but chess! Inspired by [xkcd 3139](https://xkcd.com/3139). Currently supports most chess features, including:
4+
45
- Castling
56
- En passant
67
- Showing available moves
78
- Requiring you to get out of check if you are in check
9+
- Checkmate detection (working on)
810

911
Stuff that will be implemented Soon :tm::
10-
- Checkmate detection (working on)
12+
13+
- nothing, make an issue if you have a suggestion
1114

1215
Stuff that will probably never be implemented:
16+
1317
- Pawn promotion - pawns are still useful after they reach the back rank because of the tiles, and because of the tiles it's easier to get them to the back rank
1418

1519
If you like this, please star on github!

index.ts

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -376,24 +376,26 @@ function get_allowed_moves(piece: [Piece, Color], from: Square, ignore_check: bo
376376
allowed_movements.push([1, 0], [0, 1], [-1, 0], [0, -1]);
377377
allowed_movements.push([1, 1], [-1, -1], [1, -1], [-1, 1]);
378378

379-
switch (piece[1]) {
380-
case Color.White:
381-
if (!moved_pieces[7][7]) {
382-
movement_dirs[MoveDirection.Right].push([2, 0]);
383-
}
384-
if (!moved_pieces[7][0]) {
385-
movement_dirs[MoveDirection.Left].push([-2, 0]);
386-
}
387-
break;
379+
if (!moved_pieces[from[1]][from[0]]) {
380+
switch (piece[1]) {
381+
case Color.White:
382+
if (!moved_pieces[7][7]) {
383+
movement_dirs[MoveDirection.Right].push([2, 0]);
384+
}
385+
if (!moved_pieces[7][0]) {
386+
movement_dirs[MoveDirection.Left].push([-2, 0]);
387+
}
388+
break;
388389

389-
case Color.Black:
390-
if (!moved_pieces[0][7]) {
391-
movement_dirs[MoveDirection.Right].push([2, 0]);
392-
}
393-
if (!moved_pieces[0][0]) {
394-
movement_dirs[MoveDirection.Left].push([-2, 0]);
395-
}
396-
break;
390+
case Color.Black:
391+
if (!moved_pieces[0][7]) {
392+
movement_dirs[MoveDirection.Right].push([2, 0]);
393+
}
394+
if (!moved_pieces[0][0]) {
395+
movement_dirs[MoveDirection.Left].push([-2, 0]);
396+
}
397+
break;
398+
}
397399
}
398400
break;
399401
}
@@ -461,6 +463,15 @@ function in_check(color: Color, after_move: [Piece, Square, Square] | null = nul
461463
return moves.length > 0;
462464
}
463465

466+
function in_checkmate(color: Color): boolean {
467+
const moves = find_pieces((piece) => piece[0] !== null && piece[0][1] === color)
468+
.map((v) => {
469+
return get_allowed_moves(v![0], v![1], false, true);
470+
});
471+
return (in_check(color) && moves
472+
.reduce<Square[]>((accumulator, value) => accumulator.concat(value), []).length == 0) || moves.length == 0;
473+
}
474+
464475
function get_tile_squares(tile: Tile): FixedLengthArray<Square, 4> {
465476
const base: Square = [tile[0] * 2, tile[1] * 2];
466477
return [
@@ -483,6 +494,7 @@ function offset_to_square(pos: CanvasPosition): Tile {
483494

484495
var squareToMove: Square | null = null;
485496
var active = false;
497+
var checkmate = false;
486498
var can_move_all = false;
487499
var move_piece_and_tile = false;
488500

@@ -555,7 +567,9 @@ function draw_turn() {
555567
ctx.strokeRect(635, 275, 50, 50);
556568

557569
ctx.fillStyle = ctx.strokeStyle;
558-
if (!active) {
570+
if (checkmate) {
571+
ctx.fillText("Checkmate", 660, 350, 100);
572+
} else if (!active) {
559573
ctx.fillText("Start", 660, 350, 100);
560574
ctx.fillText("the game", 660, 365, 100);
561575
} else if (turn === Color.Black) {
@@ -624,6 +638,11 @@ function switch_turn() {
624638
has_moved_tile = false;
625639
indicators = [];
626640
show_tile_indicator = false;
641+
642+
if (in_checkmate(turn)) {
643+
checkmate = true;
644+
turn = Color.opposite(turn);
645+
}
627646
}
628647

629648
tick(0);

0 commit comments

Comments
 (0)