-
Notifications
You must be signed in to change notification settings - Fork 2
/
dominoes.ts
174 lines (139 loc) · 3.76 KB
/
dominoes.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
// An engine that automatically runs dominoes games. #game
function shuffle<T>(arr: T[]): T[] {
return arr.sort(() => (Math.random() < 0.5 ? -1 : 0))
}
class Domino {
constructor(public a: number, public b: number) {}
against(n: number) {
if (this.a === n) return this.b
if (this.b === n) return this.a
}
static index: Domino[][] = []
static dominoes: Domino[] = []
static of(a: number, b: number) {
if (a < b) [a, b] = [b, a]
return this.index[a]![b]!
}
}
Domino.index = Array<Domino[]>(7)
.fill(0 as any)
.map((_, i) =>
Array<Domino>(i + 1)
.fill(0 as any)
.map((_, j) => new Domino(i, j)),
)
Domino.dominoes = Domino.index.flat()
class Player {
dominoes: Domino[] = []
}
interface GameData {
startingPlayer?: number
startingNumber?: number
didStart: boolean
winningPlayer?: number
tilesLeftByPlayers?: number
tilesLeftInDeck?: number
didFinish: boolean
turns: number
}
class Game {
static many(count: number, players: number) {
return Array(count)
.fill(0)
.map(() => new Game(players).play().data)
}
players: Player[] = []
dominoes: Domino[] = shuffle(Domino.index.flat().slice())
turn!: number
a!: number
b!: number
data: GameData = { didStart: false, didFinish: false, turns: 0 }
constructor(players: number) {
this.players = Array<Player>(players)
.fill(0 as any)
.map(() => {
const player = new Player()
player.dominoes = this.dominoes.splice(0, 7)
return player
})
for (let i = 6; i >= 0; i--) {
if (this.startWithDouble(i)) {
this.data.startingNumber = i
this.data.didStart = true
this.data.turns = 1
break
}
}
}
startWithDouble(n: number) {
const domino = Domino.of(n, n)
for (let i = 0; i < this.players.length; i++) {
const player = this.players[i]!
const index = player.dominoes.indexOf(domino)
if (index !== -1) {
this.data.startingPlayer = this.turn = i
this.a = this.b = n
player.dominoes.splice(index, 1)
return true
}
}
return false
}
play() {
if (!this.data.didStart) return this
for (let final = 0; final < 112 /* 28 x 4 */; final++) {
this.turn = (this.turn + 1) % this.players.length
const player = this.players[this.turn]!
this.data.turns++
let didPlay = false
for (let i = 0; i < player.dominoes.length; i++) {
const domino = player.dominoes[i]!
const a = domino.against(this.a)
if (a !== undefined) {
this.a = a
player.dominoes.splice(i, 1)
didPlay = true
break
}
const b = domino.against(this.b)
if (b !== undefined) {
this.b = b
player.dominoes.splice(i, 1)
didPlay = true
break
}
}
if (!didPlay) {
if (this.dominoes.length) {
const domino = this.dominoes.pop()!
player.dominoes.unshift(domino)
const a = domino.against(this.a)
if (a !== undefined) {
this.a = a
player.dominoes.splice(0, 1)
didPlay = true
} else {
const b = domino.against(this.b)
if (b !== undefined) {
this.b = b
player.dominoes.splice(0, 1)
didPlay = true
}
}
}
}
if (player.dominoes.length === 0) {
this.data.didFinish = true
this.data.winningPlayer = this.turn
this.data.tilesLeftByPlayers = this.players.reduce(
(count, player) => count + player.dominoes.length,
0,
)
this.data.tilesLeftInDeck = this.dominoes.length
return this
}
}
return this
}
}
export {}