1 import { ChessRules
, Move
, PiPo
} from "@/base_rules";
2 import { randInt
} from "@/utils/alea";
4 export class Avalam2Rules
extends ChessRules
{
6 static get HasFlags() {
10 static get HasEnpassant() {
14 static get Monochrome() {
27 // Towers of 1, 2, 3, 4 and 5
28 return ['b', 'c', 'd', 'e', 'f'];
31 static IsGoodPosition(position
) {
32 if (position
.length
== 0) return false;
33 const rows
= position
.split("/");
34 if (rows
.length
!= V
.size
.x
) return false;
35 for (let row
of rows
) {
37 for (let i
= 0; i
< row
.length
; i
++) {
38 if (['x'].concat(V
.PIECES
).includes(row
[i
].toLowerCase())) sumElts
++;
40 const num
= parseInt(row
[i
], 10);
41 if (isNaN(num
)) return false;
45 if (sumElts
!= V
.size
.y
) return false;
50 static GenRandInitFen() {
52 "BbBbBbBb/bBbBbBbB/BbBbBbBb/bBbBbBbB/" +
53 "BbBbBbBb/bBbBbBbB/BbBbBbBb/bBbBbBbB w 0"
58 return this.turn
== side
;
62 return this.turn
; //:-)
65 getBasicMove([x1
, y1
], [x2
, y2
]) {
66 const cp1
= this.board
[x1
][y1
],
67 cp2
= this.board
[x2
][y2
];
69 String
.fromCharCode(cp1
.charCodeAt(1) + cp2
.charCodeAt(1) - 97);
73 new PiPo({ x: x1
, y: y1
, c: cp1
[0], p: cp1
[1] }),
74 new PiPo({ x: x2
, y: y2
, c: cp2
[0], p: cp2
[1] })
77 new PiPo({ x: x2
, y: y2
, c: cp1
[0], p: newPiece
})
83 getPotentialMovesFrom([x
, y
]) {
84 const height
= this.board
[x
][y
].charCodeAt(1) - 97;
85 if (height
== 5) return [];
87 for (let s
of V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
])) {
88 const [i
, j
] = [x
+ s
[0], y
+ s
[1]];
91 this.board
[i
][j
] != V
.EMPTY
&&
92 (height
+ this.board
[i
][j
].charCodeAt(1) - 97 <= 5)
94 moves
.push(this.getBasicMove([x
, y
], [i
, j
]));
109 let towersCount
= { w: 0, b: 0 };
110 for (let i
= 0; i
< V
.size
.x
; i
++) {
111 for (let j
= 0; j
< V
.size
.y
; j
++) {
112 if (this.board
[i
][j
] != V
.EMPTY
) {
113 if (this.getPotentialMovesFrom([i
, j
]).length
> 0) return '*';
114 towersCount
[ this.board
[i
][j
][0] ]++;
118 if (towersCount
['w'] > towersCount
['b']) return "1-0";
119 if (towersCount
['b'] > towersCount
['w']) return "0-1";
124 // Random mover (TODO)
125 const moves
= super.getAllValidMoves();
126 if (moves
.length
== 0) return null;
127 return moves
[randInt(moves
.length
)];