-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.ts
248 lines (220 loc) · 7.52 KB
/
21.ts
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
import { Equal, Expect } from "type-testing";
type TicTacToeChip = "❌" | "⭕";
type TicTacToeEndState = "❌ Won" | "⭕ Won" | "Draw";
type TicTacToeState = TicTacToeChip | TicTacToeEndState;
type TicTacToeEmptyCell = " ";
type TicTacToeCell = TicTacToeChip | TicTacToeEmptyCell;
type TicTacToeYPositions = "top" | "middle" | "bottom";
type TicTacToeXPositions = "left" | "center" | "right";
type TicTacToePositions = `${TicTacToeYPositions}-${TicTacToeXPositions}`;
type TicTacToeBoard = TicTacToeCell[][];
type TicTacToeGame = {
board: TicTacToeBoard;
state: TicTacToeState;
};
type EmptyBoard = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]];
type NewGame = {
board: EmptyBoard;
state: "❌";
};
type PositionToYIndex<Position extends TicTacToeYPositions> = {
top: 0;
middle: 1;
bottom: 2;
}[Position];
type PositionToXIndex<Position extends TicTacToeXPositions> = {
left: 0;
center: 1;
right: 2;
}[Position];
type GetXY<Move extends TicTacToePositions> = Move extends `${infer Y extends
TicTacToeYPositions}-${infer X extends TicTacToeXPositions}`
? [PositionToXIndex<X>, PositionToYIndex<Y>]
: never;
type Index = [2, 1, 0];
type ResolveIndex<RestLength extends number> = RestLength extends Index[number]
? Index[RestLength]
: never;
type PlaceRow<
Row extends TicTacToeCell[],
X extends Index[number],
Chip extends TicTacToeChip,
> = Row extends [infer Cell, ...infer RestCells extends TicTacToeCell[]]
? ResolveIndex<RestCells["length"]> extends X
? [
Cell extends TicTacToeEmptyCell ? Chip : Cell,
...PlaceRow<RestCells, X, Chip>,
]
: [Cell, ...PlaceRow<RestCells, X, Chip>]
: [];
type PlaceInBoard<
Board extends TicTacToeBoard,
XY extends [X: Index[number], Y: Index[number]],
Chip extends TicTacToeChip,
> = Board extends [
infer Row extends TicTacToeCell[],
...infer RestRows extends TicTacToeBoard,
]
? XY[1] extends ResolveIndex<RestRows["length"]>
? [PlaceRow<Row, XY[0], Chip>, ...PlaceInBoard<RestRows, XY, Chip>]
: [Row, ...PlaceInBoard<RestRows, XY, Chip>]
: [];
type a = PlaceInBoard<EmptyBoard, GetXY<"bottom-left">, "⭕">;
// ^?
type DidChipWin<
Chip extends TicTacToeChip,
Board extends TicTacToeBoard,
> = Board extends [[Chip, any, any], [Chip, any, any], [Chip, any, any]]
? true
: Board extends [[any, Chip, any], [any, Chip, any], [any, Chip, any]]
? true
: Board extends [[any, any, Chip], [any, any, Chip], [any, any, Chip]]
? true
: Board extends [[Chip, Chip, Chip], [any, any, any], [any, any, any]]
? true
: Board extends [[any, any, any], [Chip, Chip, Chip], [any, any, any]]
? true
: Board extends [[any, any, any], [any, any, any], [Chip, Chip, Chip]]
? true
: Board extends [
[Chip, any, any],
[any, Chip, any],
[any, any, Chip],
]
? true
: Board extends [
[any, any, Chip],
[any, Chip, any],
[Chip, any, any],
]
? true
: false;
type b = DidChipWin<
"⭕",
[[" ", "❌", "❌"], ["⭕", "⭕", "❌"], ["❌", "❌", "❌"]]
>;
// ^?
type AreThereEmpties<Row extends TicTacToeCell[], Test = false> = Row extends [
infer Cell,
...infer RestCells extends TicTacToeCell[],
]
? AreThereEmpties<
RestCells,
Test extends false
? Cell extends TicTacToeEmptyCell
? true
: false
: false
>
: Test;
type RecAreThereEmpties<Board extends TicTacToeBoard> = Board extends [
infer Row extends TicTacToeCell[],
...infer RestRows extends TicTacToeBoard,
]
? [AreThereEmpties<Row>, ...RecAreThereEmpties<RestRows>]
: [];
type IsEveryCellFilled<Board extends TicTacToeBoard> =
RecAreThereEmpties<Board> extends [false, false, false] ? true : false;
type blu = IsEveryCellFilled<
[["❌", "❌", "❌"], ["❌", "❌", "❌"], ["❌", "❌", "❌"]]
>;
// ^?
type DidAnyoneWin<Board extends TicTacToeBoard> = DidChipWin<
"⭕",
Board
> extends true
? "⭕ Won"
: DidChipWin<"❌", Board> extends true
? "❌ Won"
: IsEveryCellFilled<Board> extends true
? "Draw"
: "no";
type IsInvalid<
Board extends TicTacToeBoard,
XY extends [Index[number], Index[number]],
> = Board[XY[1]][XY[0]] extends TicTacToeEmptyCell ? false : true;
type TicTacToe<
Game extends TicTacToeGame,
Move extends TicTacToePositions,
XY extends [Index[number], Index[number]] = GetXY<Move>,
> = Game["state"] extends TicTacToeChip
? {
board: PlaceInBoard<Game["board"], XY, Game["state"]>;
state: DidAnyoneWin<
PlaceInBoard<Game["board"], XY, Game["state"]>
> extends "no"
? IsInvalid<Game["board"], XY> extends true
? Game["state"]
: Exclude<TicTacToeChip, Game["state"]>
: DidAnyoneWin<PlaceInBoard<Game["board"], XY, Game["state"]>>;
}
: Game;
type test_move1_actual = TicTacToe<NewGame, "top-center">;
// ^?
type test_move1_expected = {
board: [[" ", "❌", " "], [" ", " ", " "], [" ", " ", " "]];
state: "⭕";
};
type test_move1 = Expect<Equal<test_move1_actual, test_move1_expected>>;
type test_move2_actual = TicTacToe<test_move1_actual, "top-left">;
// ^?
type test_move2_expected = {
board: [["⭕", "❌", " "], [" ", " ", " "], [" ", " ", " "]];
state: "❌";
};
type test_move2 = Expect<Equal<test_move2_actual, test_move2_expected>>;
type test_move3_actual = TicTacToe<test_move2_actual, "middle-center">;
// ^?
type test_move3_expected = {
board: [["⭕", "❌", " "], [" ", "❌", " "], [" ", " ", " "]];
state: "⭕";
};
type test_move3 = Expect<Equal<test_move3_actual, test_move3_expected>>;
type test_move4_actual = TicTacToe<test_move3_actual, "bottom-left">;
// ^?
type test_move4_expected = {
board: [["⭕", "❌", " "], [" ", "❌", " "], ["⭕", " ", " "]];
state: "❌";
};
type test_move4 = Expect<Equal<test_move4_actual, test_move4_expected>>;
type test_x_win_actual = TicTacToe<test_move4_actual, "bottom-center">;
// ^?
type test_x_win_expected = {
board: [["⭕", "❌", " "], [" ", "❌", " "], ["⭕", "❌", " "]];
state: "❌ Won";
};
type test_x_wina = DidAnyoneWin<test_x_win_actual["board"]>;
// ^?
type test_x_win = Expect<Equal<test_x_win_actual, test_x_win_expected>>;
type type_move5_actual = TicTacToe<test_move4_actual, "bottom-right">;
// ^?
type type_move5_expected = {
board: [["⭕", "❌", " "], [" ", "❌", " "], ["⭕", " ", "❌"]];
state: "⭕";
};
type test_move5 = Expect<Equal<type_move5_actual, type_move5_expected>>;
type test_o_win_actual = TicTacToe<type_move5_actual, "middle-left">;
// ^?
type test_o_win_expected = {
board: [["⭕", "❌", " "], ["⭕", "❌", " "], ["⭕", " ", "❌"]];
state: "⭕ Won";
};
// invalid move don't change the board and state
type test_invalid_actual = TicTacToe<test_move1_actual, "top-center">;
// ^?
type test_invalid_expected = {
board: [[" ", "❌", " "], [" ", " ", " "], [" ", " ", " "]];
state: "⭕";
};
type test_invalid = Expect<Equal<test_invalid_actual, test_invalid_expected>>;
type test_before_draw = {
board: [["⭕", "❌", "⭕"], ["⭕", "❌", "❌"], ["❌", "⭕", " "]];
state: "⭕";
};
type test_draw_actual = TicTacToe<test_before_draw, "bottom-right">;
// ^?
type test_draw_expected = {
board: [["⭕", "❌", "⭕"], ["⭕", "❌", "❌"], ["❌", "⭕", "⭕"]];
state: "Draw";
};
type test_draw = Expect<Equal<test_draw_actual, test_draw_expected>>;