47af232aafc80b86b673b147d71fcce6822b379e
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 return this.movesCount
== 0;
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
;
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;
91 new PiPo({ x: i
, y: j
, c: 'w', p: V
.KNIGHT
})
94 start: { x: -1, y: -1 }
98 for (let i
=0; i
<8; i
++) {
99 for (let j
=0; j
<8; j
++) {
100 if (!([V
.EMPTY
, V
.HOLE
].includes(this.board
[i
][j
])))
101 return this.getPotentialKnightMoves([i
, j
]);
107 getPotentialKnightMoves([x
, y
]) {
109 V
.steps
[V
.KNIGHT
].filter(
111 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
112 return (V
.OnBoard(i
, j
) && this.board
[i
][j
] != V
.HOLE
);
115 return this.getBasicMove([x
, y
], [x
+ s
[0], y
+ s
[1]]);
121 if (this.movesCount
== 0) return true;
122 for (let i
=0; i
<8; i
++) {
123 for (let j
=0; j
<8; j
++) {
124 if (!([V
.EMPTY
, V
.HOLE
].includes(this.board
[i
][j
])))
125 return this.getPotentialKnightMoves([i
, j
]).length
> 0;
135 static PlayOnBoard(board
, move) {
136 if (move.vanish
.length
> 0)
137 board
[move.vanish
[0].x
][move.vanish
[0].y
] = V
.HOLE
;
138 for (let psq
of move.appear
) board
[psq
.x
][psq
.y
] = psq
.c
+ psq
.p
;
146 if (this.atLeastOneMove()) return "*";
147 // No valid move: I lose
148 return this.turn
== "w" ? "0-1" : "1-0";
152 const moves
= this.getAllValidMoves();
153 // Just a random mover for now...
154 return moves
[randInt(moves
.length
)];
158 if (move.vanish
.length
> 0) return super.getNotation(move);
160 return "N@" + V
.CoordsToSquare(move.end
);