1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class HamiltonRules
extends ChessRules
{
6 static get HasFlags() {
10 static get HasEnpassant() {
23 if (b
[0] == 'x') return 'x';
24 return ChessRules
.board2fen(b
);
28 if (f
== 'x') return V
.HOLE
;
29 return ChessRules
.fen2board(f
);
33 if (b
[0] == 'x') return "Hamilton/hole";
38 return [ChessRules
.KNIGHT
];
41 static IsGoodPosition(position
) {
42 if (position
.length
== 0) return false;
43 const rows
= position
.split("/");
44 if (rows
.length
!= V
.size
.x
) return false;
45 for (let row
of rows
) {
47 for (let i
= 0; i
< row
.length
; i
++) {
48 if (['x'].concat(V
.PIECES
).includes(row
[i
].toLowerCase())) sumElts
++;
50 const num
= parseInt(row
[i
], 10);
51 if (isNaN(num
)) return false;
55 if (sumElts
!= V
.size
.y
) return false;
60 static GenRandInitFen() {
61 return "8/8/8/8/8/8/8/8 w 0";
64 canIplay(side
, [x
, y
]) {
65 return side
== this.turn
;
69 return this.movesCount
== 0;
72 // Initiate the game by choosing a square for the knight:
74 if (this.movesCount
> 0) return null;
77 new PiPo({ x: square
[0], y: square
[1], c: 'w', p: V
.KNIGHT
})
80 start: { x: -1, y: -1 }
84 getAllPotentialMoves() {
85 if (this.movesCount
== 0) {
86 return [...Array(64).keys()].map(k
=> {
88 const j
= (k
- i
) / 8;
89 return this.doClick([i
, j
]);
92 for (let i
=0; i
<8; i
++) {
93 for (let j
=0; j
<8; j
++) {
94 if (!([V
.EMPTY
, V
.HOLE
].includes(this.board
[i
][j
])))
95 return this.getPotentialKnightMoves([i
, j
]);
101 getPotentialKnightMoves([x
, y
]) {
103 V
.steps
[V
.KNIGHT
].filter(
105 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
106 return (V
.OnBoard(i
, j
) && this.board
[i
][j
] != V
.HOLE
);
109 return this.getBasicMove([x
, y
], [x
+ s
[0], y
+ s
[1]]);
115 if (this.movesCount
== 0) return true;
116 for (let i
=0; i
<8; i
++) {
117 for (let j
=0; j
<8; j
++) {
118 if (!([V
.EMPTY
, V
.HOLE
].includes(this.board
[i
][j
])))
119 return this.getPotentialKnightMoves([i
, j
]).length
> 0;
129 static PlayOnBoard(board
, move) {
130 if (move.vanish
.length
> 0)
131 board
[move.vanish
[0].x
][move.vanish
[0].y
] = V
.HOLE
;
132 for (let psq
of move.appear
) board
[psq
.x
][psq
.y
] = psq
.c
+ psq
.p
;
143 if (this.atLeastOneMove()) return "*";
144 // No valid move: I lose
145 return this.turn
== "w" ? "0-1" : "1-0";
149 const moves
= this.getAllValidMoves();
150 // Just a random mover for now...
151 return moves
[randInt(moves
.length
)];
155 if (move.vanish
.length
> 0) return super.getNotation(move);
157 return "N@" + V
.CoordsToSquare(move.end
);