-
Notifications
You must be signed in to change notification settings - Fork 913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#319: add null move functionality #451
Open
ppeloton
wants to merge
4
commits into
jhlywa:master
Choose a base branch
from
ppeloton:add-null-move-functionality
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6ee8c59
#319: add null move functionality
ppeloton e5df490
#319: add null move functionality: code review fixes
ppeloton 256746c
#319: add null move functionality: prettify code
ppeloton 1b50b1a
#319: add null move functionality: prettify code
ppeloton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Chess } from '../src/chess' | ||
|
||
test('null move', () => { | ||
const fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1' | ||
const next = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1' | ||
const chess = new Chess(fen) | ||
chess.move('--') | ||
expect(chess.fen()).toBe(next) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,7 @@ const FLAGS: Record<string, string> = { | |
PROMOTION: 'p', | ||
KSIDE_CASTLE: 'k', | ||
QSIDE_CASTLE: 'q', | ||
NULL_MOVE: 'nm', | ||
} | ||
|
||
// prettier-ignore | ||
|
@@ -123,6 +124,7 @@ const BITS: Record<string, number> = { | |
PROMOTION: 16, | ||
KSIDE_CASTLE: 32, | ||
QSIDE_CASTLE: 64, | ||
NULL_MOVE: 128, | ||
} | ||
|
||
/* | ||
|
@@ -265,6 +267,8 @@ const SECOND_RANK = { b: RANK_7, w: RANK_2 } | |
|
||
const TERMINATION_MARKERS = ['1-0', '0-1', '1/2-1/2', '*'] | ||
|
||
const SAN_NULLMOVE = '--' | ||
|
||
// Extracts the zero-based rank of an 0x88 square. | ||
function rank(square: number): number { | ||
return square >> 4 | ||
|
@@ -1405,6 +1409,18 @@ export class Chess { | |
const them = swapColor(us) | ||
this._push(move) | ||
|
||
if (move.flags & BITS.NULL_MOVE){ | ||
if (us === BLACK) { | ||
this._moveNumber++ | ||
} | ||
|
||
this._turn = them | ||
|
||
this._epSquare = EMPTY | ||
|
||
return | ||
} | ||
|
||
this._board[move.to] = this._board[move.from] | ||
delete this._board[move.from] | ||
|
||
|
@@ -1524,6 +1540,10 @@ export class Chess { | |
const us = this._turn | ||
const them = swapColor(us) | ||
|
||
if (move.flags & (BITS.NULL_MOVE)) { | ||
return move | ||
} | ||
|
||
this._board[move.from] = this._board[move.to] | ||
this._board[move.from].type = move.piece // to undo any promotions | ||
delete this._board[move.to] | ||
|
@@ -1961,6 +1981,8 @@ export class Chess { | |
output = 'O-O' | ||
} else if (move.flags & BITS.QSIDE_CASTLE) { | ||
output = 'O-O-O' | ||
} else if (move.flags & BITS.NULL_MOVE){ | ||
output = SAN_NULLMOVE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you just return SAN_NULLMOVE here to skip the rest of this function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I fixed this. |
||
} else { | ||
if (move.piece !== PAWN) { | ||
const disambiguator = getDisambiguator(move, moves) | ||
|
@@ -1981,15 +2003,17 @@ export class Chess { | |
} | ||
} | ||
|
||
this._makeMove(move) | ||
if (this.isCheck()) { | ||
if (this.isCheckmate()) { | ||
output += '#' | ||
} else { | ||
output += '+' | ||
if (!(move.flags & BITS.NULL_MOVE)){ | ||
this._makeMove(move) | ||
if (this.isCheck()) { | ||
if (this.isCheckmate()) { | ||
output += '#' | ||
} else { | ||
output += '+' | ||
} | ||
} | ||
this._undoMove() | ||
} | ||
this._undoMove() | ||
|
||
return output | ||
} | ||
|
@@ -1999,6 +2023,18 @@ export class Chess { | |
// strip off any move decorations: e.g Nf3+?! becomes Nf3 | ||
const cleanMove = strippedSan(move) | ||
|
||
//first implementation of null with a dummy move (black king moves from a8 to a8), maybe this can be implemented better | ||
if (cleanMove == SAN_NULLMOVE){ | ||
let res: InternalMove = { | ||
color: this._turn, | ||
from: 0, | ||
to: 0, | ||
piece: "k", | ||
flags: BITS.NULL_MOVE, | ||
}; | ||
return res | ||
} | ||
|
||
let pieceType = inferPieceType(cleanMove) | ||
let moves = this._moves({ legal: true, piece: pieceType }) | ||
|
||
|
@@ -2202,9 +2238,13 @@ export class Chess { | |
} | ||
|
||
// generate the FEN for the 'after' key | ||
this._makeMove(uglyMove) | ||
move.after = this.fen() | ||
this._undoMove() | ||
if (!(uglyMove.flags & BITS.NULL_MOVE)){ | ||
this._makeMove(uglyMove) | ||
move.after = this.fen() | ||
this._undoMove() | ||
} else { | ||
move.after = this.fen() | ||
} | ||
|
||
if (captured) { | ||
move.captured = captured | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The FLAGS.NULL_MOVE value should be a single character. As written, the
n
innm
collides with FLAGS.NORMAL. Maybe use '-'?The whole flag system is an artifact of some C code I wrote years ago, and is kinda kludgy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed this with the suggested value '-'