-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
chess.dart
281 lines (251 loc) · 7.08 KB
/
chess.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import 'package:dartchess/dartchess.dart';
import 'package:deep_pick/deep_pick.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/widgets.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:lichess_mobile/src/styles/lichess_icons.dart';
part 'chess.freezed.dart';
part 'chess.g.dart';
/// Move represented with UCI notation
typedef UCIMove = String;
/// Represents a [Move] with its associated SAN.
@Freezed(fromJson: true, toJson: true)
class SanMove with _$SanMove {
const SanMove._();
const factory SanMove(
String san,
@MoveConverter() Move move,
) = _SanMove;
factory SanMove.fromJson(Map<String, dynamic> json) =>
_$SanMoveFromJson(json);
bool get isCheck => san.contains('+');
bool get isCapture => san.contains('x');
}
class MoveConverter implements JsonConverter<Move, String> {
const MoveConverter();
// assume we are serializing only valid uci strings
@override
Move fromJson(String json) => Move.fromUci(json)!;
@override
String toJson(Move object) => object.uci;
}
/// Alternative castling uci notations.
const altCastles = {
'e1a1': 'e1c1',
'e1h1': 'e1g1',
'e8a8': 'e8c8',
'e8h8': 'e8g8',
};
const ISet<Variant> supportedVariants = ISetConst({
Variant.standard,
Variant.chess960,
Variant.fromPosition,
Variant.antichess,
Variant.kingOfTheHill,
Variant.threeCheck,
Variant.racingKings,
Variant.horde,
});
enum Variant {
standard('Standard', LichessIcons.crown),
chess960('Chess960', LichessIcons.die_six),
fromPosition('From Position', LichessIcons.feather),
antichess('Antichess', LichessIcons.antichess),
kingOfTheHill('King of the Hill', LichessIcons.flag),
threeCheck('Three Check', LichessIcons.three_check),
atomic('Atomic', LichessIcons.atom),
horde('Horde', LichessIcons.horde),
racingKings('Racing Kings', LichessIcons.racing_kings),
crazyhouse('Crazyhouse', LichessIcons.h_square);
const Variant(this.label, this.icon);
final String label;
final IconData icon;
bool get isSupported => supportedVariants.contains(this);
static final IMap<String, Variant> nameMap = IMap(values.asNameMap());
static Variant fromRule(Rule rule) {
switch (rule) {
case Rule.chess:
return Variant.standard;
case Rule.antichess:
return Variant.antichess;
case Rule.kingofthehill:
return Variant.kingOfTheHill;
case Rule.threecheck:
return Variant.threeCheck;
case Rule.atomic:
return Variant.atomic;
case Rule.horde:
return Variant.horde;
case Rule.racingKings:
return Variant.racingKings;
case Rule.crazyhouse:
return Variant.crazyhouse;
}
}
/// Returns the initial position for this [Variant].
///
/// Will throw an [ArgumentError] if called on [Variant.chess960] or [Variant.fromPosition].
Position get initialPosition {
switch (this) {
case Variant.standard:
return Chess.initial;
case Variant.chess960:
case Variant.fromPosition:
throw ArgumentError('This variant has no defined initial position!');
case Variant.antichess:
return Antichess.initial;
case Variant.kingOfTheHill:
return KingOfTheHill.initial;
case Variant.threeCheck:
return ThreeCheck.initial;
case Variant.atomic:
return Atomic.initial;
case Variant.crazyhouse:
return Crazyhouse.initial;
case Variant.horde:
return Horde.initial;
case Variant.racingKings:
return RacingKings.initial;
}
}
Rule get rule {
switch (this) {
case Variant.standard:
case Variant.chess960:
case Variant.fromPosition:
return Rule.chess;
case Variant.antichess:
return Rule.antichess;
case Variant.kingOfTheHill:
return Rule.kingofthehill;
case Variant.threeCheck:
return Rule.threecheck;
case Variant.atomic:
return Rule.atomic;
case Variant.horde:
return Rule.horde;
case Variant.racingKings:
return Rule.racingKings;
case Variant.crazyhouse:
return Rule.crazyhouse;
}
}
}
/// Represents a chess opening.
sealed class Opening {
String get eco;
String get name;
}
@Freezed(fromJson: true, toJson: true)
class LightOpening with _$LightOpening implements Opening {
const LightOpening._();
const factory LightOpening({
required String eco,
required String name,
}) = _LightOpening;
factory LightOpening.fromJson(Map<String, dynamic> json) =>
_$LightOpeningFromJson(json);
}
@Freezed(fromJson: true, toJson: true)
class Division with _$Division {
const factory Division({
double? middlegame,
double? endgame,
}) = _Division;
factory Division.fromJson(Map<String, dynamic> json) =>
_$DivisionFromJson(json);
}
@freezed
class FullOpening with _$FullOpening implements Opening {
const FullOpening._();
const factory FullOpening({
required String eco,
required String name,
required String fen,
required String pgnMoves,
required String uciMoves,
}) = _FullOpening;
}
extension ChessExtension on Pick {
Move asUciMoveOrThrow() {
final value = this.required().value;
if (value is Move) {
return value;
}
if (value is String) {
final move = Move.fromUci(value);
if (move != null) {
return move;
} else {
throw PickException(
"value $value at $debugParsingExit can't be casted to Move: invalid UCI string.",
);
}
}
throw PickException(
"value $value at $debugParsingExit can't be casted to Move",
);
}
Move? asUciMoveOrNull() {
if (value == null) return null;
try {
return asUciMoveOrThrow();
} catch (_) {
return null;
}
}
Side asSideOrThrow() {
final value = this.required().value;
if (value is Side) {
return value;
}
if (value is String) {
return value == 'white'
? Side.white
: value == 'black'
? Side.black
: throw PickException(
"value $value at $debugParsingExit can't be casted to Side: invalid string.",
);
}
throw PickException(
"value $value at $debugParsingExit can't be casted to Side",
);
}
Side? asSideOrNull() {
if (value == null) return null;
try {
return asSideOrThrow();
} catch (_) {
return null;
}
}
Variant asVariantOrThrow() {
final value = this.required().value;
if (value is Variant) {
return value;
}
if (value is String) {
final variant = Variant.nameMap[value];
if (variant != null) {
return variant;
}
} else if (value is Map<String, dynamic>) {
final variant = Variant.nameMap[value['key'] as String];
if (variant != null) {
return variant;
}
}
throw PickException(
"value $value at $debugParsingExit can't be casted to Variant",
);
}
Variant? asVariantOrNull() {
if (value == null) return null;
try {
return asVariantOrThrow();
} catch (_) {
return null;
}
}
}