-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdealer.go
210 lines (167 loc) · 3.86 KB
/
dealer.go
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
package main
import (
"errors"
"github.com/google/uuid"
)
const (
numStones int = 6
boardSize int = 6
numWorkers int = 100
bufferSize int = 3
)
type Move struct {
pit int
match Match
}
type MancalaBoard [][]int
type Match struct {
Id string
Board MancalaBoard
P1 string
P2 string
Turn string
}
type Dealer interface {
JoinMatch() (*Match, string)
GetMatch(string, string) (*Match, error)
PlayerTurn(Match, string) bool
MakeMove(int, Match, string) (bool, error)
}
type MancalaDealer struct {
repo MatchRepo
moveInCh chan Move
}
func newDealer(r MatchRepo) Dealer {
moveInCh := make(chan Move, boardSize)
moveOutCh := make(chan Move, boardSize)
d := MancalaDealer{repo: r, moveInCh: moveInCh}
for i := 0; i < numWorkers; i++ {
go handleMove(&d, moveOutCh)
go handleMoveCompleted(&d, moveOutCh)
}
return &d
}
func (d *MancalaDealer) JoinMatch() (*Match, string) {
m, err := d.repo.GetWaitingMatch()
if err == nil {
m.P2 = uuid.NewString()
m.Turn = m.P1
d.repo.Save(m)
return m, m.P2
}
newMatch := Match{Id: uuid.NewString(), P1: uuid.NewString(), Board: newBoard()}
d.repo.AddWaitingMatch(&newMatch)
return &newMatch, newMatch.P1
}
func (d *MancalaDealer) GetMatch(matchId string, playerId string) (*Match, error) {
match, err := d.repo.Get(matchId)
if err != nil {
return nil, errors.New("unnable to get match")
}
return match, nil
}
func (d *MancalaDealer) PlayerTurn(match Match, playerId string) bool {
p1IsDone := true
for i := 0; i < boardSize; i++ {
if match.Board[0][i] != 0 {
p1IsDone = false
break
}
}
p2IsDone := true
for i := 0; i < boardSize; i++ {
if match.Board[1][i] != 0 {
p2IsDone = false
break
}
}
return !p1IsDone && !p2IsDone && match.Turn == playerId
}
func (d *MancalaDealer) MakeMove(pit int, match Match, playerId string) (bool, error) {
if err := d.repo.Lock(match.Id); err != nil {
return false, nil
}
if 0 > pit || pit >= boardSize {
return false, errors.New("invalid pit number")
}
if match.Turn != playerId {
return false, nil
}
d.moveInCh <- Move{pit, match}
return true, nil
}
func newBoard() [][]int {
b := make([][]int, 2)
for i := 0; i < boardSize; i++ {
b[0] = append(b[0], numStones)
b[1] = append(b[1], numStones)
}
b[0] = append(b[0], 0)
b[1] = append(b[1], 0)
return b
}
func handleMove(d *MancalaDealer, out chan Move) {
for m := range d.moveInCh {
executeMove(m, d.moveInCh, out)
}
}
func handleMoveCompleted(d *MancalaDealer, ch chan Move) {
for m := range ch {
if m.match.Turn == m.match.P1 {
m.match.Turn = m.match.P2
} else {
m.match.Turn = m.match.P1
}
d.repo.Save(&m.match)
}
}
func executeMove(move Move, moveCh chan Move, completedCh chan Move) {
var playerBoard int
var opponentBoard int
if move.match.Turn == move.match.P1 {
playerBoard = 0
opponentBoard = 1
} else {
playerBoard = 1
opponentBoard = 0
}
linearBoard := append(move.match.Board[playerBoard], move.match.Board[opponentBoard]...)
stones := linearBoard[move.pit]
linearBoard[move.pit] = 0
firstPit := move.pit + 1
lastPit := len(linearBoard)-1
for {
for i := firstPit; i < lastPit; i++ {
linearBoard[i] += 1
stones -= 1
if stones == 0 {
move.match.Board[playerBoard] = linearBoard[:boardSize+1]
move.match.Board[opponentBoard] = linearBoard[boardSize+1:]
if endsOnMySide(i){
if wasEmpty(linearBoard[i]) {
collectStones(i,linearBoard)
completedCh <- move
} else {
moveCh <- Move{pit: i, match: move.match}
}
} else {
completedCh <- move
}
return
}
}
firstPit = 0
}
}
func collectStones(i int, linearBoard []int) {
linearBoard[i] = 0
opponentStone := linearBoard[i+boardSize+1]
linearBoard[i+boardSize+1] = 0
linearBoard[boardSize] += opponentStone + 1
}
func wasEmpty(i int) bool {
return i == 1
}
func endsOnMySide(i int) bool {
return i < boardSize
}