Draft of Ultima chess rules; almost OK HalfChess
[vchess.git] / public / javascripts / variants / Half.js
CommitLineData
32cfcea4
BA
1class HalfRules extends ChessRules
2{
3 // Standard rules on a 4x8 board with no pawns
4
5 initVariables(fen) { } //nothing to do
6
7 setFlags(fen)
8 {
9 // No castling, hence no flags; but flags defined for compatibility
10 this.castleFlags = { "w":[false,false], "b":[false,false] };
11 }
12
13 static get size() { return [8,4]; }
14
15 getPotentialKingMoves(sq)
16 {
17 const V = VariantRules;
18 // No castling
19 return this.getSlideNJumpMoves(sq,
20 V.steps[V.ROOK].concat(V.steps[V.BISHOP]), "oneStep");
21 }
22
23 isAttacked(sq, colors)
24 {
25 return (this.isAttackedByRook(sq, colors)
26 || this.isAttackedByKnight(sq, colors)
27 || this.isAttackedByBishop(sq, colors)
28 || this.isAttackedByQueen(sq, colors)
29 || this.isAttackedByKing(sq, colors));
30 }
31
32 // Unused:
33 updateVariables(move) { }
34 unupdateVariables(move) { }
35
36 static get SEARCH_DEPTH() { return 4; }
37
38 static GenRandInitFen()
39 {
40 let minorPieces = { "w": new Array(4), "b": new Array(4) };
41 let majorPieces = { "w": new Array(4), "b": new Array(4) };
42 for (let c of ["w","b"])
43 {
44 // Minor pieces first (on 2nd rank)
45 let positions = _.range(4);
46
47 // Get random squares for bishops
48 let randIndex = 2 * _.random(1);
49 let bishop1Pos = positions[randIndex];
50 let randIndex_tmp = 2 * _.random(1) + 1;
51 let bishop2Pos = positions[randIndex_tmp];
52 positions.splice(Math.max(randIndex,randIndex_tmp), 1);
53 positions.splice(Math.min(randIndex,randIndex_tmp), 1);
54
55 // Get random squares for knights
56 randIndex = _.random(1);
57 let knight1Pos = positions[randIndex];
58 positions.splice(randIndex, 1);
59 let knight2Pos = positions[0];
60
61 minorPieces[c][bishop1Pos] = 'b';
62 minorPieces[c][bishop2Pos] = 'b';
63 minorPieces[c][knight1Pos] = 'n';
64 minorPieces[c][knight2Pos] = 'n';
65
66 // Major pieces then (on 1st rank)
67 positions = _.range(4);
68
69 // Get random square for queen
70 randIndex = _.random(3);
71 let queenPos = positions[randIndex];
72 positions.splice(randIndex, 1);
73
74 // Rooks and king positions:
75 let rook1Pos = positions[0];
76 let kingPos = positions[1];
77 let rook2Pos = positions[2];
78
79 majorPieces[c][rook1Pos] = 'r';
80 majorPieces[c][rook2Pos] = 'r';
81 majorPieces[c][kingPos] = 'k';
82 majorPieces[c][queenPos] = 'q';
83 }
84 return majorPieces["b"].join("") + "/" + minorPieces["b"].join("") + "/4/4/4/4/" +
85 minorPieces["w"].join("").toUpperCase() + "/" +
86 majorPieces["w"].join("").toUpperCase() + " 0000"; //TODO: flags?!
87 }
88}