-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.ts
149 lines (140 loc) Β· 4.01 KB
/
solution.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
type TicTacToeChipType = "β" | "β";
type TicTacToeEndStateType = "β Won" | "β Won" | "Draw";
type TicTacToeStateType = TicTacToeChipType | TicTacToeEndStateType;
type TicTacToeEmptyCellType = " ";
type TicTacToeCellType = TicTacToeChipType | TicTacToeEmptyCellType;
type TicTacToeYPositionsType = "top" | "middle" | "bottom";
type TicTacToeXPositionsType = "left" | "center" | "right";
type TicTacToePositions =
`${TicTacToeYPositionsType}-${TicTacToeXPositionsType}`;
type TicTactToeBoardLine = TicTacToeCellType[];
type TicTactToeBoardType = TicTactToeBoardLine[];
type TicTacToeGameType = {
board: TicTactToeBoardType;
state: TicTacToeStateType;
};
type EmptyBoardType = [
[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]
];
type NewGame = {
board: EmptyBoardType;
state: "β";
};
type NextMoveMap = {
"β": "β";
"β": "β";
};
type PositionIndexMap = {
top: 0;
center: 1;
bottom: 2;
left: 0;
middle: 1;
right: 2;
};
type WinCombinationsList = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
type CheckDraw<Board extends TicTactToeBoardLine> = Board extends [
infer Current extends TicTacToeCellType,
...infer Rest extends TicTactToeBoardLine
]
? Current extends TicTacToeEmptyCellType
? false
: CheckDraw<Rest>
: true;
type CheckWinCombination<
Board extends TicTactToeBoardLine,
Target extends TicTacToeChipType,
Combination extends number[] = [],
Accumulator extends boolean[] = []
> = Accumulator["length"] extends 3
? true
: Combination extends [
infer CurrentIndex extends number,
...infer Rest extends number[]
]
? Board[CurrentIndex] extends Target
? CheckWinCombination<Board, Target, Rest, [...Accumulator, true]>
: false
: false;
type CheckWin<
Board extends TicTactToeBoardLine,
Target extends TicTacToeChipType,
WinCombinations extends number[][] = []
> = WinCombinations extends [
infer CurrentCombination extends number[],
...infer Rest extends number[][]
]
? CheckWinCombination<Board, Target, CurrentCombination> extends true
? true
: CheckWin<Board, Target, Rest>
: false;
type FlatBoard<Board extends TicTactToeBoardType> = [
...Board[0],
...Board[1],
...Board[2]
];
type GetState<
Board extends TicTactToeBoardLine,
State extends TicTacToeStateType
> = CheckDraw<Board> extends true
? "Draw"
: CheckWin<Board, "β", WinCombinationsList> extends true
? "β Won"
: CheckWin<Board, "β", WinCombinationsList> extends true
? "β Won"
: State extends keyof NextMoveMap
? NextMoveMap[State]
: never;
type ChangeInListByIndex<
List extends TicTactToeBoardType | TicTactToeBoardLine,
Index extends number,
Target extends TicTacToeChipType | TicTactToeBoardLine,
Accumulator extends any[] = []
> = List extends [
infer Current extends TicTacToeCellType | TicTactToeBoardLine,
...infer Rest extends TicTactToeBoardLine | TicTactToeBoardType
]
? Accumulator["length"] extends Index
? ChangeInListByIndex<Rest, Index, Target, [...Accumulator, Target]>
: ChangeInListByIndex<Rest, Index, Target, [...Accumulator, Current]>
: Accumulator;
type GameResult<
Board extends TicTactToeBoardType,
PrevState extends TicTacToeStateType
> = {
board: Board;
state: GetState<FlatBoard<Board>, PrevState>;
};
type TicTacToe<
Game extends TicTacToeGameType,
Position extends TicTacToePositions
> = Game["state"] extends infer PrevState extends TicTacToeChipType
? Position extends `${infer Y extends TicTacToeYPositionsType}-${infer X extends TicTacToeXPositionsType}`
? Game["board"][PositionIndexMap[Y]][PositionIndexMap[X]] extends TicTacToeEmptyCellType
? GameResult<
ChangeInListByIndex<
Game["board"],
PositionIndexMap[Y],
ChangeInListByIndex<
Game["board"][PositionIndexMap[Y]],
PositionIndexMap[X],
PrevState
>
>,
PrevState
>
: Game
: Game
: Game;
export { TicTacToe, NewGame };