projects
/
vchess.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
f3802fc
)
Get rid of redundant variable variantRules.movesCount
author
Benjamin Auder
<benjamin.auder@somewhere>
Mon, 19 Nov 2018 00:57:47 +0000
(
01:57
+0100)
committer
Benjamin Auder
<benjamin.auder@somewhere>
Mon, 19 Nov 2018 00:57:47 +0000
(
01:57
+0100)
public/javascripts/base_rules.js
patch
|
blob
|
blame
|
history
public/javascripts/variants/Atomic.js
patch
|
blob
|
blame
|
history
public/javascripts/variants/Checkered.js
patch
|
blob
|
blame
|
history
diff --git
a/public/javascripts/base_rules.js
b/public/javascripts/base_rules.js
index
3a0f1c6
..
f08c272
100644
(file)
--- a/
public/javascripts/base_rules.js
+++ b/
public/javascripts/base_rules.js
@@
-46,7
+46,7
@@
class ChessRules
/////////////////
// INITIALIZATION
/////////////////
// INITIALIZATION
- // fen =
"position flags epSquare movesCount
"
+ // fen =
= "position flags
"
constructor(fen, moves)
{
this.moves = moves;
constructor(fen, moves)
{
this.moves = moves;
@@
-152,7
+152,7
@@
class ChessRules
return L>0 ? this.moves[L-1] : null;
}
get turn() {
return L>0 ? this.moves[L-1] : null;
}
get turn() {
- return this.moves
Count
%2==0 ? 'w' : 'b';
+ return this.moves
.length
%2==0 ? 'w' : 'b';
}
// Pieces codes
}
// Pieces codes
@@
-470,8
+470,8
@@
class ChessRules
canIplay(color, sq)
{
canIplay(color, sq)
{
- return ((color=='w' && this.moves
Count
%2==0)
- || (color=='b' && this.moves
Count
%2==1))
+ return ((color=='w' && this.moves
.length
%2==0)
+ || (color=='b' && this.moves
.length
%2==1))
&& this.getColor(sq[0], sq[1]) == color;
}
&& this.getColor(sq[0], sq[1]) == color;
}
@@
-660,7
+660,7
@@
class ChessRules
board[psq.x][psq.y] = psq.c + psq.p;
}
board[psq.x][psq.y] = psq.c + psq.p;
}
- // Before move is played
:
+ // Before move is played
, update variables + flags
updateVariables(move)
{
const piece = this.getPiece(move.start.x,move.start.y);
updateVariables(move)
{
const piece = this.getPiece(move.start.x,move.start.y);
@@
-691,37
+691,33
@@
class ChessRules
}
}
}
}
-
play(move, ingam
e)
+
unupdateVariables(mov
e)
{
{
- // Save flags (for undo)
- move.flags = JSON.stringify(this.flags); //TODO: less costly
- this.updateVariables(move);
+ // (Potentially) Reset king position
+ const c = this.getColor(move.start.x,move.start.y);
+ if (this.getPiece(move.start.x,move.start.y) == VariantRules.KING)
+ this.kingPos[c] = [move.start.x, move.start.y];
+ }
+ play(move, ingame)
+ {
if (!!ingame)
if (!!ingame)
- {
move.notation = this.getNotation(move);
move.notation = this.getNotation(move);
- this.moves.push(move);
- }
+ // Save flags (for undo)
+ move.flags = JSON.stringify(this.flags); //TODO: less costly?
+ this.updateVariables(move);
+ this.moves.push(move);
this.epSquares.push( this.getEpSquare(move) );
VariantRules.PlayOnBoard(this.board, move);
this.epSquares.push( this.getEpSquare(move) );
VariantRules.PlayOnBoard(this.board, move);
- this.movesCount++;
}
undo(move, ingame)
{
VariantRules.UndoOnBoard(this.board, move);
this.epSquares.pop();
}
undo(move, ingame)
{
VariantRules.UndoOnBoard(this.board, move);
this.epSquares.pop();
- this.movesCount--;
-
- if (!!ingame)
- this.moves.pop();
-
- // Update king position, and reset stored/computed flags
- const c = this.getColor(move.start.x,move.start.y);
- if (this.getPiece(move.start.x,move.start.y) == VariantRules.KING)
- this.kingPos[c] = [move.start.x, move.start.y];
-
+ this.moves.pop();
+ this.unupdateVariables(move);
this.flags = JSON.parse(move.flags);
}
this.flags = JSON.parse(move.flags);
}
diff --git
a/public/javascripts/variants/Atomic.js
b/public/javascripts/variants/Atomic.js
index
bf860df
..
0fd7647
100644
(file)
--- a/
public/javascripts/variants/Atomic.js
+++ b/
public/javascripts/variants/Atomic.js
@@
-96,10
+96,9
@@
class AtomicRules extends ChessRules
}
}
}
}
- un
do
(move)
+ un
updateVariables
(move)
{
{
- super.undo(move);
-
+ super.unupdateVariables(move);
const c = this.getColor(move.start.x,move.start.y);
const oppCol = this.getOppCol(c);
if ([this.kingPos[c][0],this.kingPos[oppCol][0]].some(e => { return e < 0; }))
const c = this.getColor(move.start.x,move.start.y);
const oppCol = this.getOppCol(c);
if ([this.kingPos[c][0],this.kingPos[oppCol][0]].some(e => { return e < 0; }))
diff --git
a/public/javascripts/variants/Checkered.js
b/public/javascripts/variants/Checkered.js
index
7c00cca
..
3626822
100644
(file)
--- a/
public/javascripts/variants/Checkered.js
+++ b/
public/javascripts/variants/Checkered.js
@@
-276,8
+276,8
@@
class CheckeredRules extends ChessRules
canIplay(color, sq)
{
canIplay(color, sq)
{
- return ((color=='w' && this.moves
Count
%2==0) || color=='c'
- || (color=='b' && this.moves
Count
%2==1))
+ return ((color=='w' && this.moves
.length
%2==0) || color=='c'
+ || (color=='b' && this.moves
.length
%2==1))
&& [color,'c'].includes(this.getColor(sq[0], sq[1]));
}
&& [color,'c'].includes(this.getColor(sq[0], sq[1]));
}
@@
-385,19
+385,6
@@
class CheckeredRules extends ChessRules
this.flags[1][move.start.x==6 ? "w" : "b"][move.start.y] = false;
}
this.flags[1][move.start.x==6 ? "w" : "b"][move.start.y] = false;
}
- play(move, ingame)
- {
- super.play(move, ingame);
- if (!ingame)
- this.moves.push(move); //needed for turn indication for checkered pieces
- }
-
- undo(move)
- {
- super.undo(move);
- this.moves.pop();
- }
-
checkGameEnd(color)
{
if (!this.isAttacked(this.kingPos[color], this.getOppCol(color))
checkGameEnd(color)
{
if (!this.isAttacked(this.kingPos[color], this.getOppCol(color))