projects
/
vchess.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Simplified underCheck / getCheckSquares logic. Debugging Berolina
[vchess.git]
/
public
/
javascripts
/
base_rules.js
diff --git
a/public/javascripts/base_rules.js
b/public/javascripts/base_rules.js
index
d85ff86
..
0bf5114
100644
(file)
--- a/
public/javascripts/base_rules.js
+++ b/
public/javascripts/base_rules.js
@@
-195,16
+195,12
@@
class ChessRules
return (this.turn == side && this.getColor(x,y) == side);
}
return (this.turn == side && this.getColor(x,y) == side);
}
- // On which squares is
opponent under check after our move
? (for interface)
- getCheckSquares(
move
)
+ // On which squares is
color under check
? (for interface)
+ getCheckSquares(
color
)
{
{
- this.play(move);
- const color = this.turn; //opponent
- let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)])
+ return this.isAttacked(this.kingPos[color], [this.getOppCol(color)])
? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate!
: [];
? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate!
: [];
- this.undo(move);
- return res;
}
/////////////
}
/////////////
@@
-766,9
+762,9
@@
class ChessRules
////////////////////
// MOVES VALIDATION
////////////////////
// MOVES VALIDATION
+ // For the interface: possible moves for the current turn from square sq
getPossibleMovesFrom(sq)
{
getPossibleMovesFrom(sq)
{
- // Assuming color is right (already checked)
return this.filterValid( this.getPotentialMovesFrom(sq) );
}
return this.filterValid( this.getPotentialMovesFrom(sq) );
}
@@
-777,7
+773,13
@@
class ChessRules
{
if (moves.length == 0)
return [];
{
if (moves.length == 0)
return [];
- return moves.filter(m => { return !this.underCheck(m); });
+ const color = this.turn;
+ return moves.filter(m => {
+ this.play(m);
+ const res = !this.underCheck(color);
+ this.undo(m);
+ return res;
+ });
}
// Search for all valid moves considering current turn (for engine and game end)
}
// Search for all valid moves considering current turn (for engine and game end)
@@
-912,14
+914,10
@@
class ChessRules
return false;
}
return false;
}
- // Is c
urrent playe
r under check after his move ?
- underCheck(
move
)
+ // Is c
olo
r under check after his move ?
+ underCheck(
color
)
{
{
- const color = this.turn;
- this.play(move);
- let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color)]);
- this.undo(move);
- return res;
+ return this.isAttacked(this.kingPos[color], [this.getOppCol(color)]);
}
/////////////////
}
/////////////////
@@
-945,8
+943,8
@@
class ChessRules
// Before move is played, update variables + flags
updateVariables(move)
{
// Before move is played, update variables + flags
updateVariables(move)
{
- const piece =
this.getPiece(move.start.x,move.start.y)
;
- const c =
this.turn
;
+ const piece =
move.vanish[0].p
;
+ const c =
move.vanish[0].c
;
const firstRank = (c == "w" ? V.size.x-1 : 0);
// Update king position + flags
const firstRank = (c == "w" ? V.size.x-1 : 0);
// Update king position + flags
@@
-986,6
+984,7
@@
class ChessRules
play(move, ingame)
{
// DEBUG:
play(move, ingame)
{
// DEBUG:
+// console.log("DO");
// if (!this.states) this.states = [];
// if (!ingame) this.states.push(this.getFen());
// if (!this.states) this.states = [];
// if (!ingame) this.states.push(this.getFen());
@@
-994,12
+993,12
@@
class ChessRules
if (V.HasFlags)
move.flags = JSON.stringify(this.aggregateFlags()); //save flags (for undo)
if (V.HasFlags)
move.flags = JSON.stringify(this.aggregateFlags()); //save flags (for undo)
- this.updateVariables(move);
- this.moves.push(move);
if (V.HasEnpassant)
this.epSquares.push( this.getEpSquare(move) );
if (V.HasEnpassant)
this.epSquares.push( this.getEpSquare(move) );
- this.turn = this.getOppCol(this.turn);
V.PlayOnBoard(this.board, move);
V.PlayOnBoard(this.board, move);
+ this.turn = this.getOppCol(this.turn);
+ this.moves.push(move);
+ this.updateVariables(move);
if (!!ingame)
{
if (!!ingame)
{
@@
-1010,16
+1009,17
@@
class ChessRules
undo(move)
{
undo(move)
{
- V.UndoOnBoard(this.board, move);
- this.turn = this.getOppCol(this.turn);
if (V.HasEnpassant)
this.epSquares.pop();
if (V.HasEnpassant)
this.epSquares.pop();
- this.moves.pop();
- this.unupdateVariables(move);
if (V.HasFlags)
this.disaggregateFlags(JSON.parse(move.flags));
if (V.HasFlags)
this.disaggregateFlags(JSON.parse(move.flags));
+ V.UndoOnBoard(this.board, move);
+ this.turn = this.getOppCol(this.turn);
+ this.moves.pop();
+ this.unupdateVariables(move);
// DEBUG:
// DEBUG:
+// console.log("UNDO "+this.getNotation(move));
// if (this.getFen() != this.states[this.states.length-1])
// debugger;
// this.states.pop();
// if (this.getFen() != this.states[this.states.length-1])
// debugger;
// this.states.pop();