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() {
21 hoverHighlight(x
, y
) {
22 return this.movesCount
== 0;
26 if (b
[0] == 'x') return 'x';
27 return ChessRules
.board2fen(b
);
31 if (f
== 'x') return V
.HOLE
;
32 return ChessRules
.fen2board(f
);
36 if (b
[0] == 'x') return "Hamilton/hole";
41 return [ChessRules
.KNIGHT
];
44 static IsGoodPosition(position
) {
45 if (position
.length
== 0) return false;
46 const rows
= position
.split("/");
47 if (rows
.length
!= V
.size
.x
) return false;
48 for (let row
of rows
) {
50 for (let i
= 0; i
< row
.length
; i
++) {
51 if (['x'].concat(V
.PIECES
).includes(row
[i
].toLowerCase())) sumElts
++;
53 const num
= parseInt(row
[i
], 10);
54 if (isNaN(num
)) return false;
58 if (sumElts
!= V
.size
.y
) return false;
63 static GenRandInitFen() {
64 return "8/8/8/8/8/8/8/8 w 0";
67 canIplay(side
, [x
, y
]) {
68 return side
== this.turn
;
71 // Initiate the game by choosing a square for the knight:
73 if (this.movesCount
> 0) return null;
76 new PiPo({ x: square
[0], y: square
[1], c: 'w', p: V
.KNIGHT
})
79 start: { x: -1, y: -1 }
83 getAllPotentialMoves() {
84 if (this.movesCount
== 0) {
85 return [...Array(64).keys()].map(k
=> {
87 const j
= (k
- i
) / 8;
90 new PiPo({ x: i
, y: j
, c: 'w', p: V
.KNIGHT
})
93 start: { x: -1, y: -1 }
97 for (let i
=0; i
<8; i
++) {
98 for (let j
=0; j
<8; j
++) {
99 if (!([V
.EMPTY
, V
.HOLE
].includes(this.board
[i
][j
])))
100 return this.getPotentialKnightMoves([i
, j
]);
106 getPotentialKnightMoves([x
, y
]) {
108 V
.steps
[V
.KNIGHT
].filter(
110 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
111 return (V
.OnBoard(i
, j
) && this.board
[i
][j
] != V
.HOLE
);
114 return this.getBasicMove([x
, y
], [x
+ s
[0], y
+ s
[1]]);
120 if (this.movesCount
== 0) return true;
121 for (let i
=0; i
<8; i
++) {
122 for (let j
=0; j
<8; j
++) {
123 if (!([V
.EMPTY
, V
.HOLE
].includes(this.board
[i
][j
])))
124 return this.getPotentialKnightMoves([i
, j
]).length
> 0;
134 static PlayOnBoard(board
, move) {
135 if (move.vanish
.length
> 0)
136 board
[move.vanish
[0].x
][move.vanish
[0].y
] = V
.HOLE
;
137 for (let psq
of move.appear
) board
[psq
.x
][psq
.y
] = psq
.c
+ psq
.p
;
145 if (this.atLeastOneMove()) return "*";
146 // No valid move: I lose
147 return this.turn
== "w" ? "0-1" : "1-0";
151 const moves
= this.getAllValidMoves();
152 // Just a random mover for now...
153 return moves
[randInt(moves
.length
)];
157 if (move.vanish
.length
> 0) return super.getNotation(move);
159 return "N@" + V
.CoordsToSquare(move.end
);