import { ChessRules } from "@/base_rules";
import { randInt } from "@/utils/alea";
-export class RefusalRules extends ChessRules {
+export class Refusal1Rules extends ChessRules {
+
+ static get HasFlags() {
+ return false;
+ }
static IsGoodFen(fen) {
if (!ChessRules.IsGoodFen(fen)) return false;
static ParseFen(fen) {
return Object.assign(
- { lastMove: fen.split(" ")[5] },
+ { lastMove: fen.split(" ")[4] },
ChessRules.ParseFen(fen)
);
}
// some extra repetitions could be detected... TODO (...)
static GenRandInitFen(randomness) {
- return ChessRules.GenRandInitFen(randomness) + " null";
+ return ChessRules.GenRandInitFen(randomness).slice(0, -6) + "- null";
}
setOtherVariables(fen) {
return super.getPotentialMovesFrom([x, y]);
}
- // NOTE: do not take refusal move into account here (two own moves)
- atLeastTwoMoves() {
- let movesCounter = 0;
- const color = this.turn;
- for (let i = 0; i < V.size.x; i++) {
- for (let j = 0; j < V.size.y; j++) {
- if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) {
- const moves = this.getPotentialMovesFrom([i, j]);
- for (let m of moves) {
- if (m.vanish[0].c == color && this.filterValid([m]).length > 0) {
- movesCounter++;
- if (movesCounter >= 2) return true;
- }
- }
- }
- }
- }
- return false;
- }
-
filterValid(moves) {
if (moves.length == 0) return [];
const color = this.turn;
prePlay(move) {
const L = this.lastMove.length;
const lm = this.lastMove[L-1];
- if (
- // My previous move was already refused?
- (!!lm && this.getColor(lm.end.x, lm.end.y) == this.turn) ||
- // I've only one move available?
- !this.atLeastTwoMoves()
- ) {
- move.noRef = true;
- }
// NOTE: refusal could be recomputed, but, it's easier like this
if (move.vanish[0].c != this.turn) move.refusal = true;
+ move.noRef = (
+ !!move.refusal ||
+ // My previous move was already refused?
+ !!lm && this.getColor(lm.end.x, lm.end.y) == this.turn
+ );
}
getEpSquare(move) {
else {
const L = this.lastMove.length;
const lm = this.lastMove[L-1];
- this.disaggregateFlags(JSON.parse(lm.flags));
+ if (move.appear[0].p == V.KING)
+ this.kingPos[move.appear[0].c] = [move.end.x, move.end.y];
}
// NOTE: explicitely give fields, because some are assigned in BaseGame
let mvInLm = {
end: move.end,
appear: move.appear,
vanish: move.vanish,
- flags: move.flags
};
if (!!move.noRef) mvInLm.noRef = true;
if (!!move.refusal) mvInLm.refusal = true;
postUndo(move) {
if (!move.refusal) super.postUndo(move);
+ else {
+ if (move.appear[0].p == V.KING)
+ this.kingPos[move.appear[0].c] = [move.start.x, move.start.y];
+ }
this.lastMove.pop();
}
const L = this.lastMove.length;
const lm = this.lastMove[L-1];
let potentialMoves = [];
+ if (!!lm && !lm.noRef)
+ // Add refusal move:
+ potentialMoves = this.getPotentialMovesFrom([lm.end.x, lm.end.y]);
for (let i = 0; i < V.size.x; i++) {
for (let j = 0; j < V.size.y; j++) {
- if (
- this.board[i][j] != V.EMPTY &&
- (
- this.getColor(i, j) == color ||
- // Add move refusal:
- (!!lm && lm.end.x == i && lm.end.y == j)
- )
- ) {
+ if (this.board[i][j] != V.EMPTY && this.getColor(i, j) == color) {
Array.prototype.push.apply(
potentialMoves,
this.getPotentialMovesFrom([i, j])
return potentialMoves;
}
+ atLeastOneMove() {
+ const L = this.lastMove.length;
+ const lm = this.lastMove[L-1];
+ if (!!lm && !lm.noRef) return true;
+ return super.atLeastOneMove();
+ }
+
getComputerMove() {
// Just play at random for now... (TODO?)
// Refuse last move with odds 1/3.
const moves = this.getAllValidMoves();
const refusal = moves.find(m => m.vanish[0].c != this.turn);
if (!!refusal) {
- if (Math.random() <= 0.33) return refusal;
+ if (moves.length == 1 || Math.random() <= 0.33) return refusal;
const others = moves.filter(m => m.vanish[0].c == this.turn);
return others[randInt(others.length)];
}