8738b2897bc73bd591a7f3ddca8f555cec5a40e7
1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class HamiltonRules
extends ChessRules
{
10 static get HasFlags() {
14 static get HasEnpassant() {
27 if (b
[0] == 'x') return 'x';
28 return ChessRules
.board2fen(b
);
32 if (f
== 'x') return V
.HOLE
;
33 return ChessRules
.fen2board(f
);
37 if (b
[0] == 'x') return "Hamilton/hole";
42 return [ChessRules
.KNIGHT
];
45 static IsGoodPosition(position
) {
46 if (position
.length
== 0) return false;
47 const rows
= position
.split("/");
48 if (rows
.length
!= V
.size
.x
) return false;
49 for (let row
of rows
) {
51 for (let i
= 0; i
< row
.length
; i
++) {
52 if (['x'].concat(V
.PIECES
).includes(row
[i
].toLowerCase())) sumElts
++;
54 const num
= parseInt(row
[i
], 10);
55 if (isNaN(num
)) return false;
59 if (sumElts
!= V
.size
.y
) return false;
64 static GenRandInitFen() {
65 return "8/8/8/8/8/8/8/8 w 0";
68 canIplay(side
, [x
, y
]) {
69 return side
== this.turn
;
73 return this.movesCount
== 0;
76 // Initiate the game by choosing a square for the knight:
78 if (this.movesCount
> 0) return null;
81 new PiPo({ x: square
[0], y: square
[1], c: 'w', p: V
.KNIGHT
})
84 start: { x: -1, y: -1 }
88 getAllPotentialMoves() {
89 if (this.movesCount
== 0) {
90 return [...Array(64).keys()].map(k
=> {
92 const j
= (k
- i
) / 8;
93 return this.doClick([i
, j
]);
96 for (let i
=0; i
<8; i
++) {
97 for (let j
=0; j
<8; j
++) {
98 if (!([V
.EMPTY
, V
.HOLE
].includes(this.board
[i
][j
])))
99 return this.getPotentialKnightMoves([i
, j
]);
105 getPotentialKnightMoves([x
, y
]) {
107 V
.steps
[V
.KNIGHT
].filter(
109 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
110 return (V
.OnBoard(i
, j
) && this.board
[i
][j
] != V
.HOLE
);
113 return this.getBasicMove([x
, y
], [x
+ s
[0], y
+ s
[1]]);
119 if (this.movesCount
== 0) return true;
120 for (let i
=0; i
<8; i
++) {
121 for (let j
=0; j
<8; j
++) {
122 if (!([V
.EMPTY
, V
.HOLE
].includes(this.board
[i
][j
])))
123 return this.getPotentialKnightMoves([i
, j
]).length
> 0;
133 static PlayOnBoard(board
, move) {
134 if (move.vanish
.length
> 0)
135 board
[move.vanish
[0].x
][move.vanish
[0].y
] = V
.HOLE
;
136 for (let psq
of move.appear
) board
[psq
.x
][psq
.y
] = psq
.c
+ psq
.p
;
147 if (this.atLeastOneMove()) return "*";
148 // No valid move: I lose
149 return this.turn
== "w" ? "0-1" : "1-0";
153 const moves
= this.getAllValidMoves();
154 // Just a random mover for now...
155 return moves
[randInt(moves
.length
)];
159 if (move.vanish
.length
> 0) return super.getNotation(move);
161 return "N@" + V
.CoordsToSquare(move.end
);