1 import { ChessRules
} from "@/base_rules";
3 export class IceageRules
extends ChessRules
{
5 static get IgnoreRepetition() {
14 if (b
[0] == 'c') return 'c';
15 return ChessRules
.board2fen(b
);
19 if (f
== 'c') return V
.ICECUBE
;
20 return ChessRules
.fen2board(f
);
24 if (b
[0] == 'c') return "Iceage/icecube";
28 static IsGoodPosition(position
) {
29 if (position
.length
== 0) return false;
30 const rows
= position
.split("/");
31 if (rows
.length
!= V
.size
.x
) return false;
32 let kings
= { "k": 0, "K": 0 };
33 for (let row
of rows
) {
35 for (let i
= 0; i
< row
.length
; i
++) {
36 if (['K','k'].includes(row
[i
])) kings
[row
[i
]]++;
37 if (['c'].concat(V
.PIECES
).includes(row
[i
].toLowerCase())) sumElts
++;
39 const num
= parseInt(row
[i
], 10);
40 if (isNaN(num
)) return false;
44 if (sumElts
!= V
.size
.y
) return false;
46 if (Object
.values(kings
).some(v
=> v
!= 1)) return false;
50 static GenRandInitFen(options
) {
51 return ChessRules
.GenRandInitFen(options
).replace(/8/g
, "cccccccc");
55 const iceAgeAfterMove
= (this.movesCount
% 40 == 39);
57 // Next ice age after this move:
58 move.state
= JSON
.stringify(this.board
);
60 if (iceAgeAfterMove
) {
61 for (let i
=0; i
<8; i
++) {
62 for (let j
=0; j
<8; j
++) {
63 if (this.board
[i
][j
] == V
.EMPTY
) {
64 const surrounded
= V
.steps
[V
.ROOK
].every(s
=> {
65 const [ii
, jj
] = [i
+ s
[0], j
+ s
[1]];
68 ![V
.EMPTY
, V
.ICECUBE
].includes(this.board
[ii
][jj
])
71 if (!surrounded
) this.board
[i
][j
] = V
.ICECUBE
;
73 else if (this.board
[i
][j
] != V
.ICECUBE
) {
74 const steps
= V
.steps
[V
.ROOK
].concat(V
.steps
[V
.BISHOP
]);
75 const connected
= steps
.some(s
=> {
76 const [ii
, jj
] = [i
+ s
[0], j
+ s
[1]];
79 ![V
.EMPTY
, V
.ICECUBE
].includes(this.board
[ii
][jj
])
82 if (!connected
) this.board
[i
][j
] = V
.ICECUBE
;
86 // Update king position (no need to update flags: game over)
87 const kp
= this.kingPos
;
88 if (this.getPiece(kp
['w'][0], kp
['w'][1]) != V
.KING
)
89 this.kingPos
['w'] = [-1, -1];
90 if (this.getPiece(kp
['b'][0], kp
['b'][1]) != V
.KING
)
91 this.kingPos
['b'] = [-1, -1];
98 this.board
= JSON
.parse(move.state
);
99 if (this.kingPos
['w'][0] < 0 || this.kingPos
['b'][0] < 0) {
100 for (let i
=0; i
<8; i
++) {
101 for (let j
=0; j
<8; j
++) {
102 if (this.board
[i
][j
] != V
.EMPTY
&& this.getPiece(i
, j
) == V
.KING
)
103 this.kingPos
[this.getColor(i
, j
)] = [i
, j
];
111 if (this.kingPos
[this.turn
][0] < 0) return [];
112 return super.getCheckSquares();
116 const kingDisappear
= {
117 w: this.kingPos
['w'][0] < 0,
118 b: this.kingPos
['b'][0] < 0
120 if (kingDisappear
['w'] && kingDisappear
['b']) return "1/2";
121 if (kingDisappear
['w']) return "0-1";
122 if (kingDisappear
['b']) return "1-0";
123 return super.getCurrentScore();
126 static get SEARCH_DEPTH() {
132 for (let i
= 0; i
< V
.size
.x
; i
++) {
133 for (let j
= 0; j
< V
.size
.y
; j
++) {
134 if (![V
.EMPTY
,V
.ICECUBE
].includes(this.board
[i
][j
])) {
135 const sign
= this.getColor(i
, j
) == "w" ? 1 : -1;
136 evaluation
+= sign
* V
.VALUES
[this.getPiece(i
, j
)];