1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class HamiltonRules
extends ChessRules
{
5 static get HasFlags() {
9 static get HasEnpassant() {
17 hoverHighlight(x
, y
) {
18 return this.movesCount
== 0;
22 if (b
[0] == 'x') return 'x';
23 return ChessRules
.board2fen(b
);
27 if (f
== 'x') return V
.HOLE
;
28 return ChessRules
.fen2board(f
);
32 if (b
[0] == 'x') return "Hamilton/hole";
37 return [ChessRules
.KNIGHT
];
40 static IsGoodPosition(position
) {
41 if (position
.length
== 0) return false;
42 const rows
= position
.split("/");
43 if (rows
.length
!= V
.size
.x
) return false;
44 for (let row
of rows
) {
46 for (let i
= 0; i
< row
.length
; i
++) {
47 if (['x'].concat(V
.PIECES
).includes(row
[i
].toLowerCase())) sumElts
++;
49 const num
= parseInt(row
[i
]);
50 if (isNaN(num
)) return false;
54 if (sumElts
!= V
.size
.y
) return false;
59 static GenRandInitFen() {
60 return "8/8/8/8/8/8/8/8 w 0";
63 canIplay(side
, [x
, y
]) {
64 return side
== this.turn
;
67 // Initiate the game by choosing a square for the knight:
69 if (this.movesCount
> 0) return null;
72 new PiPo({ x: square
[0], y: square
[1], c: 'w', p: V
.KNIGHT
})
75 start: { x: -1, y: -1 }
79 getAllPotentialMoves() {
80 if (this.movesCount
== 0) {
81 return [...Array(64).keys()].map(k
=> {
83 const j
= (k
- i
) / 8;
86 new PiPo({ x: i
, y: j
, c: 'w', p: V
.KNIGHT
})
89 start: { x: -1, y: -1 }
93 for (let i
=0; i
<8; i
++) {
94 for (let j
=0; j
<8; j
++) {
95 if (!([V
.EMPTY
, V
.HOLE
].includes(this.board
[i
][j
])))
96 return this.getPotentialKnightMoves([i
, j
]);
102 getPotentialKnightMoves([x
, y
]) {
104 V
.steps
[V
.KNIGHT
].filter(
106 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
107 return (V
.OnBoard(i
, j
) && this.board
[i
][j
] != V
.HOLE
);
110 return this.getBasicMove([x
, y
], [x
+ s
[0], y
+ s
[1]]);
116 if (this.movesCount
== 0) return true;
117 for (let i
=0; i
<8; i
++) {
118 for (let j
=0; j
<8; j
++) {
119 if (!([V
.EMPTY
, V
.HOLE
].includes(this.board
[i
][j
])))
120 return this.getPotentialKnightMoves([i
, j
]).length
> 0;
130 static PlayOnBoard(board
, move) {
131 if (move.vanish
.length
> 0)
132 board
[move.vanish
[0].x
][move.vanish
[0].y
] = V
.HOLE
;
133 for (let psq
of move.appear
) board
[psq
.x
][psq
.y
] = psq
.c
+ psq
.p
;
141 if (this.atLeastOneMove()) return "*";
142 // No valid move: I lose
143 return this.turn
== "w" ? "0-1" : "1-0";
147 const moves
= this.getAllValidMoves();
148 // Just a random mover for now...
149 return moves
[randInt(moves
.length
)];
153 if (move.vanish
.length
> 0) return super.getNotation(move);
155 return "N@" + V
.CoordsToSquare(move.end
);