projects
/
vchess.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Prepare some more variants (unfinished)
[vchess.git]
/
public
/
javascripts
/
variants
/
Checkered.js
diff --git
a/public/javascripts/variants/Checkered.js
b/public/javascripts/variants/Checkered.js
index
b9e7223
..
d73cef4
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];
@@
-72,14
+92,19
@@
class CheckeredRules extends ChessRules
{
let standardMoves = super.getPotentialMovesFrom([x,y]);
const lastRank = this.turn == "w" ? 0 : 7;
{
let standardMoves = super.getPotentialMovesFrom([x,y]);
const lastRank = this.turn == "w" ? 0 : 7;
- if (this.getPiece(x,y) == V
ariantRules
.KING)
+ if (this.getPiece(x,y) == V.KING)
return standardMoves; //king has to be treated differently (for castles)
let moves = [];
standardMoves.forEach(m => {
return standardMoves; //king has to be treated differently (for castles)
let moves = [];
standardMoves.forEach(m => {
- if (m.vanish[0].p == VariantRules.PAWN && Math.abs(m.end.x-m.start.x)==2
- && !this.pawnFlags[this.turn][m.start.y])
+ if (m.vanish[0].p == V.PAWN)
{
{
- return; //skip forbidden 2-squares jumps
+ if (Math.abs(m.end.x-m.start.x)==2 && !this.pawnFlags[this.turn][m.start.y])
+ return; //skip forbidden 2-squares jumps
+ if (this.board[m.end.x][m.end.y] == V.EMPTY && m.vanish.length==2
+ && this.getColor(m.start.x,m.start.y) == 'c')
+ {
+ return; //checkered pawns cannot take en-passant
+ }
}
if (m.vanish.length == 1)
moves.push(m); //no capture
}
if (m.vanish.length == 1)
moves.push(m); //no capture
@@
-89,7
+114,7
@@
class CheckeredRules extends ChessRules
m.appear[0].c = "c";
moves.push(m);
if (m.appear[0].p != m.vanish[1].p //avoid promotions (already treated):
m.appear[0].c = "c";
moves.push(m);
if (m.appear[0].p != m.vanish[1].p //avoid promotions (already treated):
- && (m.vanish[0].p != V
ariantRules
.PAWN || m.end.x != lastRank))
+ && (m.vanish[0].p != V.PAWN || m.end.x != lastRank))
{
// Add transformation into captured piece
let m2 = JSON.parse(JSON.stringify(m));
{
// Add transformation into captured piece
let m2 = JSON.parse(JSON.stringify(m));
@@
-103,8
+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)
@@
-141,7
+165,7
@@
class CheckeredRules extends ChessRules
{
for (let i of [-1,1])
{
{
for (let i of [-1,1])
{
- if (y+i>=0 && y+i<8 && this.getPiece(x+pawnShift,y+i)==V
ariantRules
.PAWN
+ if (y+i>=0 && y+i<8 && this.getPiece(x+pawnShift,y+i)==V.PAWN
&& this.getColor(x+pawnShift,y+i)==c)
{
return true;
&& this.getColor(x+pawnShift,y+i)==c)
{
return true;
@@
-165,53
+189,52
@@
class CheckeredRules extends ChessRules
{
this.play(move);
const color = this.turn;
{
this.play(move);
const color = this.turn;
- this.moves.push(move); //artifically change turn, for checkered pawns (TODO)
- const kingAttacked = this.isAttacked(this.kingPos[color], [this.getOppCol(color),'c']);
+ // 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
let res = kingAttacked
- ? [
JSON.parse(JSON.stringify(this.kingPos[color]))
] //need to duplicate!
- : [
];
- this.
moves.pop()
;
+ ? [
JSON.parse(JSON.stringify(this.kingPos[color]))
] //need to duplicate!
+ : [];
+ this.
turn = color
;
this.undo(move);
return res;
}
updateVariables(move)
{
this.undo(move);
return res;
}
updateVariables(move)
{
- const c = this.getColor(move.start.x,move.start.y);
- if (c != 'c') //checkered not concerned by castle flags
- super.updateVariables(move);
-
- // Does it turn off a 2-squares pawn flag?
+ super.updateVariables(move);
+ // Does this move turn off a 2-squares pawn flag?
const secondRank = [1,6];
const secondRank = [1,6];
- if (secondRank.includes(move.start.x) && move.vanish[0].p == V
ariantRules
.PAWN)
+ if (secondRank.includes(move.start.x) && move.vanish[0].p == V.PAWN)
this.pawnFlags[move.start.x==6 ? "w" : "b"][move.start.y] = false;
}
checkGameEnd()
{
const color = this.turn;
this.pawnFlags[move.start.x==6 ? "w" : "b"][move.start.y] = false;
}
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;
}
evalPosition()
{
return res;
}
evalPosition()
{
- const [sizeX,sizeY] = VariantRules.size;
let evaluation = 0;
//Just count material for now, considering checkered neutral (...)
let evaluation = 0;
//Just count material for now, considering checkered neutral (...)
- for (let i=0; i<
sizeX
; i++)
+ for (let i=0; i<
V.size.x
; i++)
{
{
- for (let j=0; j<
sizeY
; j++)
+ for (let j=0; j<
V.size.y
; j++)
{
{
- if (this.board[i][j] != V
ariantRules
.EMPTY)
+ if (this.board[i][j] != V.EMPTY)
{
const sqColor = this.getColor(i,j);
const sign = sqColor == "w" ? 1 : (sqColor=="b" ? -1 : 0);
{
const sqColor = this.getColor(i,j);
const sign = sqColor == "w" ? 1 : (sqColor=="b" ? -1 : 0);
- evaluation += sign * V
ariantRules
.VALUES[this.getPiece(i,j)];
+ evaluation += sign * V.VALUES[this.getPiece(i,j)];
}
}
}
}
}
}
@@
-220,7
+243,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()
@@
-247,19
+272,19
@@
class CheckeredRules extends ChessRules
}
// Translate final square
}
// Translate final square
- let finalSquare =
- String.fromCharCode(97 + move.end.y) + (VariantRules.size[0]-move.end.x);
+ const finalSquare = V.CoordsToSquare(move.end);
-
le
t piece = this.getPiece(move.start.x, move.start.y);
- if (piece == V
ariantRules
.PAWN)
+
cons
t piece = this.getPiece(move.start.x, move.start.y);
+ if (piece == V.PAWN)
{
// Pawn move
let notation = "";
if (move.vanish.length > 1)
{
// Capture
{
// Pawn move
let notation = "";
if (move.vanish.length > 1)
{
// Capture
- let startColumn = String.fromCharCode(97 + move.start.y);
- notation = startColumn + "x" + finalSquare + "=" + move.appear[0].p.toUpperCase();
+ const startColumn = V.GetColumn(move.start.y);
+ notation = startColumn + "x" + finalSquare +
+ "=" + move.appear[0].p.toUpperCase();
}
else //no capture
{
}
else //no capture
{
@@
-278,3
+303,5
@@
class CheckeredRules extends ChessRules
}
}
}
}
}
}
+
+const VariantRules = CheckeredRules;