c05b4d92304691ca0bffb80ced70511341a78f33
1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class Avalam2Rules
extends ChessRules
{
10 static get HasFlags() {
14 static get HasEnpassant() {
18 static get Monochrome() {
31 // Towers of 1, 2, 3, 4 and 5
32 return ['b', 'c', 'd', 'e', 'f'];
35 static IsGoodPosition(position
) {
36 if (position
.length
== 0) return false;
37 const rows
= position
.split("/");
38 if (rows
.length
!= V
.size
.x
) return false;
39 for (let row
of rows
) {
41 for (let i
= 0; i
< row
.length
; i
++) {
42 if (['x'].concat(V
.PIECES
).includes(row
[i
].toLowerCase())) sumElts
++;
44 const num
= parseInt(row
[i
], 10);
45 if (isNaN(num
)) return false;
49 if (sumElts
!= V
.size
.y
) return false;
54 static GenRandInitFen() {
56 "BbBbBbBb/bBbBbBbB/BbBbBbBb/bBbBbBbB/" +
57 "BbBbBbBb/bBbBbBbB/BbBbBbBb/bBbBbBbB w 0"
62 return this.turn
== side
;
66 return this.turn
; //:-)
69 getBasicMove([x1
, y1
], [x2
, y2
]) {
70 const cp1
= this.board
[x1
][y1
],
71 cp2
= this.board
[x2
][y2
];
73 String
.fromCharCode(cp1
.charCodeAt(1) + cp2
.charCodeAt(1) - 97);
77 new PiPo({ x: x1
, y: y1
, c: cp1
[0], p: cp1
[1] }),
78 new PiPo({ x: x2
, y: y2
, c: cp2
[0], p: cp2
[1] })
81 new PiPo({ x: x2
, y: y2
, c: cp1
[0], p: newPiece
})
87 getPotentialMovesFrom([x
, y
]) {
88 const height
= this.board
[x
][y
].charCodeAt(1) - 97;
89 if (height
== 5) return [];
91 for (let s
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
92 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
95 this.board
[i
][j
] != V
.EMPTY
&&
96 (height
+ this.board
[i
][j
].charCodeAt(1) - 97 <= 5)
98 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
113 let towersCount
= { w: 0, b: 0 };
114 for (let i
= 0; i
< V
.size
.x
; i
++) {
115 for (let j
= 0; j
< V
.size
.y
; j
++) {
116 if (this.board
[i
][j
] != V
.EMPTY
) {
117 if (this.getPotentialMovesFrom([i
, j
]).length
> 0) return '*';
118 towersCount
[ this.board
[i
][j
][0] ]++;
122 if (towersCount
['w'] > towersCount
['b']) return "1-0";
123 if (towersCount
['b'] > towersCount
['w']) return "0-1";
128 // Random mover (TODO)
129 const moves
= super.getAllValidMoves();
130 if (moves
.length
== 0) return null;
131 return moves
[randInt(moves
.length
)];