projects
/
vchess.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Various fixes, additions...
[vchess.git]
/
public
/
javascripts
/
variants
/
Checkered.js
diff --git
a/public/javascripts/variants/Checkered.js
b/public/javascripts/variants/Checkered.js
index
2d62d4b
..
4a9419a
100644
(file)
--- a/
public/javascripts/variants/Checkered.js
+++ b/
public/javascripts/variants/Checkered.js
@@
-4,6
+4,7
@@
class CheckeredRules extends ChessRules
{
return b[0]=='c' ? "Checkered/"+b : b;
}
{
return b[0]=='c' ? "Checkered/"+b : b;
}
+
static board2fen(b)
{
const checkered_codes = {
static board2fen(b)
{
const checkered_codes = {
@@
-17,29
+18,49
@@
class CheckeredRules extends ChessRules
return checkered_codes[b[1]];
return ChessRules.board2fen(b);
}
return checkered_codes[b[1]];
return ChessRules.board2fen(b);
}
+
static fen2board(f)
{
static fen2board(f)
{
+ // Tolerate upper-case versions of checkered pieces (why not?)
const checkered_pieces = {
's': 'p',
const checkered_pieces = {
's': 'p',
+ 'S': 'p',
't': 'q',
't': 'q',
+ 'T': 'q',
'u': 'r',
'u': 'r',
+ 'U': 'r',
'c': 'b',
'c': 'b',
+ 'C': 'b',
'o': 'n',
'o': 'n',
+ 'O': 'n',
};
if (Object.keys(checkered_pieces).includes(f))
return 'c'+checkered_pieces[f];
return ChessRules.fen2board(f);
}
};
if (Object.keys(checkered_pieces).includes(f))
return 'c'+checkered_pieces[f];
return ChessRules.fen2board(f);
}
- s
etFlags(fen
)
+ s
tatic get PIECES(
)
{
{
- super.setFlags(fen); //castleFlags
+ return ChessRules.PIECES.concat(['s','t','u','c','o']);
+ }
+
+ static IsGoodFlags(flags)
+ {
+ // 4 for castle + 16 for pawns
+ return !!flags.match(/^[01]{20,20}$/);
+ }
+
+ setFlags(fenflags)
+ {
+ super.setFlags(fenflags); //castleFlags
this.pawnFlags =
{
this.pawnFlags =
{
- "w":
new Array(8
), //pawns can move 2 squares?
- "b":
new Array(8
)
+ "w":
_.map(_.range(8), i => true
), //pawns can move 2 squares?
+ "b":
_.map(_.range(8), i => true
)
};
};
- const flags = fen.split(" ")[1].substr(4); //skip first 4 digits, for castle
+ if (!fenflags)
+ return;
+ const flags = fenflags.substr(4); //skip first 4 digits, for castle
for (let c of ['w','b'])
{
for (let i=0; i<8; i++)
for (let c of ['w','b'])
{
for (let i=0; i<8; i++)
@@
-47,13
+68,12
@@
class CheckeredRules extends ChessRules
}
}
}
}
- // Aggregates flags into one object
-
get flags()
{
+ aggregateFlags()
+ {
return [this.castleFlags, this.pawnFlags];
}
return [this.castleFlags, this.pawnFlags];
}
- // Reverse operation
- parseFlags(flags)
+ disaggregateFlags(flags)
{
this.castleFlags = flags[0];
this.pawnFlags = flags[1];
{
this.castleFlags = flags[0];
this.pawnFlags = flags[1];
@@
-108,9
+128,7
@@
class CheckeredRules extends ChessRules
canIplay(side, [x,y])
{
canIplay(side, [x,y])
{
- return ((side=='w' && this.moves.length%2==0)
- || (side=='b' && this.moves.length%2==1))
- && [side,'c'].includes(this.getColor(x,y));
+ return (side == this.turn && [side,'c'].includes(this.getColor(x,y)));
}
// Does m2 un-do m1 ? (to disallow undoing checkered moves)
}
// Does m2 un-do m1 ? (to disallow undoing checkered moves)
@@
-133,7
+151,10
@@
class CheckeredRules extends ChessRules
const L = this.moves.length;
if (L > 0 && this.oppositeMoves(this.moves[L-1], m))
return false;
const L = this.moves.length;
if (L > 0 && this.oppositeMoves(this.moves[L-1], m))
return false;
- return !this.underCheck(m);
+ this.play(m);
+ const res = !this.underCheck(color);
+ this.undo(m);
+ return res;
});
}
});
}
@@
-158,27
+179,21
@@
class CheckeredRules extends ChessRules
return false;
}
return false;
}
- underCheck(
move
)
+ underCheck(
color
)
{
{
- const color = this.turn;
- this.play(move);
- let res = this.isAttacked(this.kingPos[color], [this.getOppCol(color),'c']);
- this.undo(move);
- return res;
+ return this.isAttacked(this.kingPos[color], [this.getOppCol(color),'c']);
}
}
- getCheckSquares(
move
)
+ getCheckSquares(
color
)
{
{
- this.play(move);
- const color = this.turn;
- this.moves.push(move); //artifically change turn, for checkered pawns (TODO)
+ // Artifically change turn, for checkered pawns
+ this.turn = this.getOppCol(color);
const kingAttacked = this.isAttacked(
this.kingPos[color], [this.getOppCol(color),'c']);
let res = kingAttacked
const kingAttacked = this.isAttacked(
this.kingPos[color], [this.getOppCol(color),'c']);
let res = kingAttacked
- ? [ JSON.parse(JSON.stringify(this.kingPos[color])) ] //need to duplicate!
- : [ ];
- this.moves.pop();
- this.undo(move);
+ ? [JSON.parse(JSON.stringify(this.kingPos[color]))] //need to duplicate!
+ : [];
+ this.turn = color;
return res;
}
return res;
}
@@
-194,11
+209,12
@@
class CheckeredRules extends ChessRules
checkGameEnd()
{
const color = this.turn;
checkGameEnd()
{
const color = this.turn;
- this.moves.length++; //artifically change turn, for checkered pawns (TODO)
+ // Artifically change turn, for checkered pawns
+ this.turn = this.getOppCol(this.turn);
const res = this.isAttacked(this.kingPos[color], [this.getOppCol(color),'c'])
? (color == "w" ? "0-1" : "1-0")
: "1/2";
const res = this.isAttacked(this.kingPos[color], [this.getOppCol(color),'c'])
? (color == "w" ? "0-1" : "1-0")
: "1/2";
- this.
moves.length--
;
+ this.
turn = this.getOppCol(this.turn)
;
return res;
}
return res;
}
@@
-223,7
+239,9
@@
class CheckeredRules extends ChessRules
static GenRandInitFen()
{
static GenRandInitFen()
{
- return ChessRules.GenRandInitFen() + "1111111111111111"; //add 16 pawns flags
+ const randFen = ChessRules.GenRandInitFen();
+ // Add 16 pawns flags:
+ return randFen.replace(" w 1111", " w 11111111111111111111");
}
getFlagsFen()
}
getFlagsFen()
@@
-250,10
+268,9
@@
class CheckeredRules extends ChessRules
}
// Translate final square
}
// Translate final square
- let finalSquare =
- String.fromCharCode(97 + move.end.y) + (V.size.x-move.end.x);
+ const finalSquare = V.CoordsToSquare(move.end);
-
le
t piece = this.getPiece(move.start.x, move.start.y);
+
cons
t piece = this.getPiece(move.start.x, move.start.y);
if (piece == V.PAWN)
{
// Pawn move
if (piece == V.PAWN)
{
// Pawn move
@@
-261,7
+278,7
@@
class CheckeredRules extends ChessRules
if (move.vanish.length > 1)
{
// Capture
if (move.vanish.length > 1)
{
// Capture
-
let startColumn = String.fromCharCode(97 +
move.start.y);
+
const startColumn = V.CoordToColumn(
move.start.y);
notation = startColumn + "x" + finalSquare +
"=" + move.appear[0].p.toUpperCase();
}
notation = startColumn + "x" + finalSquare +
"=" + move.appear[0].p.toUpperCase();
}
@@
-282,3
+299,5
@@
class CheckeredRules extends ChessRules
}
}
}
}
}
}
+
+const VariantRules = CheckeredRules;