-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathttt.ts
140 lines (112 loc) · 3.33 KB
/
ttt.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
import * as m from './math';
export type Config = {
dimension: Dimension,
rot_speed: number,
zoom_speed: number,
camera_dist: number,
project_dist: number,
point_size: number,
fov: number,
code: string,
};
export type Winner = {
indexes: number[],
piece: Piece
};
export enum Piece {
X, O
}
export enum Dimension {
TWO, THREE, FOUR
}
export class Board {
public dimension: number;
public pieces: Piece[];
public cur_piece: Piece;
public select: number;
public winners: Winner[];
constructor(dimension: Dimension) {
this.reset(dimension);
}
reset(dimension: Dimension = this.dimension ?? Dimension.FOUR) {
this.dimension = dimension;
this.pieces = [];
this.select = 0;
this.winners = [];
if(this.cur_piece == null)
this.cur_piece = Piece.X;
}
swap_piece() {
if(this.cur_piece == Piece.X)
this.cur_piece = Piece.O;
else
this.cur_piece = Piece.X;
}
put_piece() {
if(this.pieces[this.select] == null) {
this.pieces[this.select] = this.cur_piece;
this.swap_piece();
}
}
move(dir: m.Vec4) {
this.select = m.pos_to_index(
m.addv4(dir, m.index_to_pos(this.select))
);
if(this.dimension == Dimension.TWO)
this.select %= 9;
else if(this.dimension == Dimension.THREE)
this.select %= 27;
}
check_winners() {
this.winners = [];
const line_directions = function(pos: m.Vec4): m.Vec4[] {
const forms_line = [0, 1, 2, 3].filter(i => pos[i] != 1);
const rst = [];
for(let comb = 1; comb < (1 << forms_line.length); comb++) {
const dir: m.Vec4 = [0, 0, 0, 0];
forms_line.filter((_, i) => ((1<<i) & comb) != 0).forEach(i => {
if(pos[i] == 0)
dir[i] = 1
else
dir[i] = -1
});
rst.push(dir);
}
return rst;
};
const dir_to_indexes = function(pos: m.Vec4, dir: m.Vec4): number[] {
const indexes = [];
while(Math.max(...pos.map(Math.abs)) < 3) {
indexes.push(m.pos_to_index(pos));
pos = m.addv4(pos, dir);
}
return indexes;
};
for(let i = 0; i < 81; i++) {
const pos = m.index_to_pos(i);
for(let dir of line_directions(pos)) {
const inds = dir_to_indexes(pos, dir);
const first_piece = this.pieces[inds[0]];
if(first_piece != null && inds.every(i => this.pieces[i] == first_piece)) {
this.winners.push({
indexes: inds,
piece: this.pieces[inds[0]]
});
}
}
}
}
serialize(): string {
return JSON.stringify({
dimension: this.dimension,
pieces: this.pieces,
cur_piece: this.cur_piece
});
}
deserialize(json: string) {
const data = JSON.parse(json);
this.dimension = data.dimension;
this.pieces = data.pieces;
this.cur_piece = data.cur_piece;
}
}