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() {
18 if (b
[0] == 'x') return 'x';
19 return ChessRules
.board2fen(b
);
23 if (f
== 'x') return V
.HOLE
;
24 return ChessRules
.fen2board(f
);
28 if (b
[0] == 'x') return "Hamilton/hole";
33 return [ChessRules
.KNIGHT
];
36 static IsGoodPosition(position
) {
37 if (position
.length
== 0) return false;
38 const rows
= position
.split("/");
39 if (rows
.length
!= V
.size
.x
) return false;
40 for (let row
of rows
) {
42 for (let i
= 0; i
< row
.length
; i
++) {
43 if (['x'].concat(V
.PIECES
).includes(row
[i
].toLowerCase())) sumElts
++;
45 const num
= parseInt(row
[i
]);
46 if (isNaN(num
)) return false;
50 if (sumElts
!= V
.size
.y
) return false;
55 static GenRandInitFen() {
56 return "8/8/8/8/8/8/8/8 w 0";
59 canIplay(side
, [x
, y
]) {
60 return side
== this.turn
;
63 // Initiate the game by choosing a square for the knight:
65 if (this.movesCount
> 0) return null;
68 new PiPo({ x: square
[0], y: square
[1], c: 'w', p: V
.KNIGHT
})
71 start: { x: -1, y: -1 }
75 getAllPotentialMoves() {
76 if (this.movesCount
== 0) {
77 return [...Array(64).keys()].map(k
=> {
79 const j
= (k
- i
) / 8;
82 new PiPo({ x: i
, y: j
, c: 'w', p: V
.KNIGHT
})
85 start: { x: -1, y: -1 }
89 for (let i
=0; i
<8; i
++) {
90 for (let j
=0; j
<8; j
++) {
91 if (!([V
.EMPTY
, V
.HOLE
].includes(this.board
[i
][j
])))
92 return this.getPotentialKnightMoves([i
, j
]);
98 getPotentialKnightMoves([x
, y
]) {
100 V
.steps
[V
.KNIGHT
].filter(
102 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
103 return (V
.OnBoard(i
, j
) && this.board
[i
][j
] != V
.HOLE
);
106 return this.getBasicMove([x
, y
], [x
+ s
[0], y
+ s
[1]]);
112 if (this.movesCount
== 0) return true;
113 for (let i
=0; i
<8; i
++) {
114 for (let j
=0; j
<8; j
++) {
115 if (!([V
.EMPTY
, V
.HOLE
].includes(this.board
[i
][j
])))
116 return this.getPotentialKnightMoves([i
, j
]).length
> 0;
126 static PlayOnBoard(board
, move) {
127 if (move.vanish
.length
> 0)
128 board
[move.vanish
[0].x
][move.vanish
[0].y
] = V
.HOLE
;
129 for (let psq
of move.appear
) board
[psq
.x
][psq
.y
] = psq
.c
+ psq
.p
;
137 if (this.atLeastOneMove()) return "*";
138 // No valid move: I lose
139 return this.turn
== "w" ? "0-1" : "1-0";
143 const moves
= this.getAllValidMoves();
144 // Just a random mover for now...
145 return moves
[randInt(moves
.length
)];
149 if (move.vanish
.length
> 0) return super.getNotation(move);
151 return "N@" + V
.CoordsToSquare(move.end
);