80a3cce3e13472707d5db80ce2d9e744c8763ddb
1 import ChessRules
from "/base_rules.js";
2 import PiPo
from "/utils/PiPo.js";
3 import Move
from "/utils/Move.js";
5 export default class BarioRules
extends ChessRules
{
11 "atomic", "cannibal", "capture", "cylinder",
12 "dark", "madrasi", "rifle", "teleport"
17 // Does not really seem necessary (although the author mention it)
18 // Instead, first move = pick a square for the king.
34 super.pieces(color
, x
, y
)
39 return this.movesCount
<= 1;
42 // Initiate the game by choosing a square for the king:
44 const color
= this.turn
;
46 this.movesCount
<= 1 &&
48 (color
== 'w' && coords
.x
== this.size
.x
- 1) ||
49 (color
== 'b' && coords
.x
== 0)
53 appear: [ new PiPo({x: coords
.x
, y: coords
.y
, c: color
, p: 'k' }) ],
54 vanish: [ new PiPo({x: coords
.x
, y: coords
.y
, c: color
, p: 'u' }) ]
60 genRandInitBaseFen() {
62 fen: "uuuuuuuu/pppppppp/8/8/8/8/PPPPPPPP/UUUUUUUU",
70 captureUndef: (o
.init
|| !this.captureUndef
)
72 : C
.CoordsToSquare(this.captureUndef
)
82 ["w","b"].map(c
=> Object
.values(this.reserve
[c
]).join("")).join("")
86 initReserves(reserveStr
) {
87 super.initReserves(reserveStr
, ['r', 'n', 'b', 'q']);
90 setOtherVariables(fenParsed
) {
91 super.setOtherVariables(fenParsed
);
92 this.captureUndef
= fenParsed
.captureUndef
== '-'
94 C
.SquareToCoords(fenParsed
.captureUndef
);
95 this.definition
= null;
98 canDrop([c
, p
], [i
, j
]) {
99 switch (this.subTurn
) {
101 return i
== this.captureUndef
.x
&& j
== this.captureUndef
.y
;
103 return this.getPiece(i
, j
) == 'u' && c
== this.getColor(i
, j
);
105 return false; //never reached
108 getPotentialMovesFrom([x
, y
]) {
109 if (this.movesCount
<= 1)
112 switch (this.subTurn
) {
114 if (typeof x
== "string")
115 moves
= this.getDropMovesFrom([x
, y
]);
118 // Both normal move (from defined piece) and definition allowed
119 if (typeof x
== "string")
120 moves
= this.getDropMovesFrom([x
, y
]);
121 else if (this.getPiece(x
, y
) != 'u')
122 moves
= super.getPotentialMovesFrom([x
, y
]);
125 // We can only move the just-defined piece
126 if (x
== this.definition
.x
&& y
== this.definition
.y
)
127 moves
= super.getPotentialMovesFrom([x
, y
]);
134 if (this.movesCount
<= 1 || this.subTurn
== 0)
136 if (this.subTurn
== 1) {
137 // Remove defining moves with un-movable def piece
138 moves
= moves
.filter(m
=> {
139 if (m
.vanish
.length
>= 2 || m
.vanish
[0].p
!= 'u')
142 const canMove
= super.filterValid(
143 super.getPotentialMovesFrom([m
.end
.x
, m
.end
.y
])).length
>= 1;
148 return super.filterValid(moves
);
151 atLeastOneMove(color
) {
152 if (this.subTurn
!= 1)
154 return super.atLeastOneMove(color
);
157 // TODO: this method fails to detect undefined checks
158 underCheck(square_s
, oppCol
) {
159 if (super.underCheck(square_s
, oppCol
))
161 // Check potential specializations of undefined using reserve:
162 const allAttacks
= Array
.prototype.concat
.apply(
163 ['r', 'n', 'b', 'q'].map(p
=> this.pieces()[p
].moves
[0]));
164 const [x
, y
] = [square_s
[0], square_s
[1]];
165 for (let i
=0; i
<this.size
.x
; i
++) {
166 for (let j
=0; j
<this.size
.y
; j
++) {
168 this.board
[i
][j
] != "" &&
169 this.getColor(i
, j
) == oppCol
&&
170 this.getPiece(i
, j
) == 'u'
172 for (let stepDef
of allAttacks
) {
173 for (let s
of stepDef
.steps
) {
174 if (!super.compatibleStep([i
, j
], [x
, y
], s
, stepDef
.range
))
177 super.findDestSquares(
180 captureTarget: [x
, y
],
181 captureSteps: [{steps: [s
], range: stepDef
.range
}],
198 // TODO: missing "undefined reset" check (is everything defined? If yes, reset if enough variety)
200 const color
= this.turn
;
201 const toNextPlayer
= () => {
202 this.turn
= C
.GetOppCol(color
);
205 if (this.movesCount
<= 1) {
209 const captureUndef
= (
210 move.vanish
.length
== 2 &&
211 move.vanish
[1].c
!= color
&&
212 move.vanish
[1].p
== 'u'
214 if (typeof move.start
.x
== "number" && !captureUndef
)
215 // Normal move (including Teleport)
216 super.postPlay(move);
217 else if (typeof move.start
.x
== "string") {
218 this.reserve
[color
][move.appear
[0].p
]--;
219 if (move.vanish
.length
== 1 && move.vanish
[0].p
== 'u')
220 this.definition
= move.end
;
225 this.captureUndef
= move.end
;
231 return true; //called only on normal moves (not Teleport)
234 getCurrentScore(move_s
) {
235 return (this.movesCount
<= 2 ? "*" : super.getCurrentScore(move_s
));