-
Notifications
You must be signed in to change notification settings - Fork 10
/
turn.go
290 lines (238 loc) · 8.02 KB
/
turn.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package game
import (
"errors"
"fmt"
"strconv"
"github.com/domino14/word-golib/tilemapping"
"github.com/rs/zerolog/log"
"github.com/domino14/macondo/board"
pb "github.com/domino14/macondo/gen/api/proto/macondo"
"github.com/domino14/macondo/move"
)
func (g *Game) curPlayer() *playerState {
return g.players[g.onturn]
}
func (g *Game) oppPlayer() *playerState {
return g.players[(g.onturn+1)%2]
}
func (g *Game) EventFromMove(m *move.Move) *pb.GameEvent {
curPlayer := g.curPlayer()
evt := &pb.GameEvent{
PlayerIndex: uint32(g.onturn),
Cumulative: int32(curPlayer.points),
Rack: m.FullRack(),
}
switch m.Action() {
case move.MoveTypePlay:
evt.Position = m.BoardCoords()
evt.PlayedTiles = m.Tiles().UserVisiblePlayedTiles(m.Alphabet())
evt.Score = int32(m.Score())
evt.Type = pb.GameEvent_TILE_PLACEMENT_MOVE
evt.IsBingo = m.TilesPlayed() == RackTileLimit
evt.NumTilesFromRack = uint32(m.TilesPlayed())
CalculateCoordsFromStringPosition(evt)
case move.MoveTypePass:
evt.Type = pb.GameEvent_PASS
case move.MoveTypeChallenge:
evt.Type = pb.GameEvent_CHALLENGE
case move.MoveTypeUnsuccessfulChallengePass:
evt.Type = pb.GameEvent_UNSUCCESSFUL_CHALLENGE_TURN_LOSS
case move.MoveTypeExchange:
evt.Exchanged = m.Tiles().UserVisible(m.Alphabet())
evt.Type = pb.GameEvent_EXCHANGE
evt.NumTilesFromRack = uint32(m.TilesPlayed())
}
log.Debug().
Interface("curplayer", curPlayer).
Interface("move", m.String()).
Interface("evt", evt).
Msg("EventFromMove")
return evt
}
func (g *Game) endRackEvt(pidx int, bonusPts int) *pb.GameEvent {
curPlayer := g.players[pidx]
otherPlayer := g.players[otherPlayer(pidx)]
evt := &pb.GameEvent{
PlayerIndex: uint32(pidx),
Cumulative: int32(curPlayer.points),
Rack: otherPlayer.rack.String(),
EndRackPoints: int32(bonusPts),
Type: pb.GameEvent_END_RACK_PTS,
}
return evt
}
func (g *Game) endRackPenaltyEvt(penalty int) *pb.GameEvent {
curPlayer := g.curPlayer()
evt := &pb.GameEvent{
PlayerIndex: uint32(g.onturn),
Cumulative: int32(curPlayer.points),
Rack: curPlayer.rack.String(),
LostScore: int32(penalty),
Type: pb.GameEvent_END_RACK_PENALTY,
}
return evt
}
func modifyForPlaythrough(tiles tilemapping.MachineWord, board *board.GameBoard,
vertical bool, row int, col int) error {
// modify the tiles array to account for situations in which a letter
// being played through is not specified as the playthrough marker
currow := row
curcol := col
for idx := range tiles {
if vertical {
currow = row + idx
} else {
curcol = col + idx
}
if currow > board.Dim()-1 || curcol > board.Dim()-1 {
log.Error().Int("currow", currow).Int("curcol", curcol).Int("dim", board.Dim()).Msg("err-out-of-bounds")
return errors.New("play out of bounds of board")
}
if tiles[idx] != 0 {
// log.Debug().Int("ml", int(tiles[idx])).Msg("not playthru")
// This is either a tile we are placing or a tile on the board.
if board.HasLetter(currow, curcol) {
// We specified a tile on the board already. Make sure
// that it's the same tile we specified.
onboard := board.GetLetter(currow, curcol)
if onboard != tiles[idx] && onboard.Unblank() != tiles[idx].Unblank() {
return fmt.Errorf("the play-through tile is incorrect (board %v, specified %v)",
int(onboard), int(tiles[idx]))
}
// Overwrite to be playthroughmarker
log.Debug().Int("idx", idx).Int("ml", int(tiles[idx])).Msg("Overwriting tile at idx")
tiles[idx] = 0
}
// Otherwise it's a tile we are placing. Do nothing.
}
}
return nil
}
// MoveFromEvent generates a move from an event
func MoveFromEvent(evt *pb.GameEvent, alph *tilemapping.TileMapping, board *board.GameBoard) (*move.Move, error) {
var m *move.Move
rack, err := tilemapping.ToMachineWord(evt.Rack, alph)
if err != nil {
log.Error().Err(err).Msg("")
return nil, err
}
log.Debug().Int("evt-type", int(evt.Type)).Msg("creating-move-from-event")
switch evt.Type {
case pb.GameEvent_TILE_PLACEMENT_MOVE:
// Calculate tiles, leave, tilesPlayed
tiles, err := tilemapping.ToMachineWord(evt.PlayedTiles, alph)
if err != nil {
log.Error().Err(err).Msg("")
return nil, err
}
err = modifyForPlaythrough(tiles, board, evt.Direction == pb.GameEvent_VERTICAL,
int(evt.Row), int(evt.Column))
if err != nil {
log.Error().Err(err).Msg("")
return nil, err
}
leaveMW, err := tilemapping.Leave(rack, tiles, false)
if err != nil {
log.Error().Err(err).Msg("")
return nil, err
}
// log.Debug().Msgf("calculated leave %v from rack %v, tiles %v",
// leaveMW.UserVisible(alph), rack.UserVisible(alph),
// tiles.UserVisible(alph))
m = move.NewScoringMove(int(evt.Score), tiles, leaveMW,
evt.Direction == pb.GameEvent_VERTICAL,
len(rack)-len(leaveMW), alph, int(evt.Row), int(evt.Column))
case pb.GameEvent_EXCHANGE:
ct, err := strconv.Atoi(evt.Exchanged)
var tiles tilemapping.MachineWord
if err == nil {
// The event contains a number of exchanged tiles, instead
// of the actual tiles.
// Set the exchanged tiles to just the first N tiles that
// are provided.
if len(rack) < ct {
ct = len(rack)
}
tiles = rack[:ct]
} else {
tiles, err = tilemapping.ToMachineWord(evt.Exchanged, alph)
if err != nil {
log.Error().Err(err).Msg("")
return nil, err
}
}
leaveMW, err := tilemapping.Leave(rack, tiles, true)
if err != nil {
log.Error().Err(err).Msg("")
return nil, err
}
m = move.NewExchangeMove(tiles, leaveMW, alph)
case pb.GameEvent_PASS:
m = move.NewPassMove(rack, alph)
case pb.GameEvent_CHALLENGE:
m = move.NewChallengeMove(rack, alph)
case pb.GameEvent_CHALLENGE_BONUS:
m = move.NewBonusScoreMove(move.MoveTypeChallengeBonus,
rack, int(evt.Bonus))
case pb.GameEvent_END_RACK_PTS:
m = move.NewBonusScoreMove(move.MoveTypeEndgameTiles,
rack, int(evt.EndRackPoints))
// point loss events:
// This either happens for:
// - game over after 6 passes
// - phony came off the board
// - international rules at the end of a game
// - time penalty
case pb.GameEvent_PHONY_TILES_RETURNED,
pb.GameEvent_TIME_PENALTY,
pb.GameEvent_END_RACK_PENALTY:
var mt move.MoveType
if evt.Type == pb.GameEvent_PHONY_TILES_RETURNED {
mt = move.MoveTypePhonyTilesReturned
} else if evt.Type == pb.GameEvent_END_RACK_PENALTY {
mt = move.MoveTypeLostTileScore
} else if evt.Type == pb.GameEvent_TIME_PENALTY {
mt = move.MoveTypeLostScoreOnTime
}
log.Debug().Int("mt", int(mt)).Msg("lost-score-move")
m = move.NewLostScoreMove(mt, rack, int(evt.LostScore))
case pb.GameEvent_UNSUCCESSFUL_CHALLENGE_TURN_LOSS:
m = move.NewUnsuccessfulChallengePassMove(rack, alph)
default:
log.Error().Msgf("Unhandled event %v", evt)
}
return m, nil
}
func summary(players []*pb.PlayerInfo, evt *pb.GameEvent) string {
summary := ""
who := players[evt.PlayerIndex].Nickname
switch evt.Type {
case pb.GameEvent_TILE_PLACEMENT_MOVE:
summary = fmt.Sprintf("%s played %s %s for %d pts from a rack of %s",
who, evt.Position, evt.PlayedTiles, evt.Score, evt.Rack)
case pb.GameEvent_PASS:
summary = fmt.Sprintf("%s passed, holding a rack of %s",
who, evt.Rack)
case pb.GameEvent_CHALLENGE:
summary = fmt.Sprintf("%s challenged, holding a rack of %s",
who, evt.Rack)
case pb.GameEvent_UNSUCCESSFUL_CHALLENGE_TURN_LOSS:
summary = fmt.Sprintf("%s challenged unsuccessfully, holding a rack of %s",
who, evt.Rack)
case pb.GameEvent_EXCHANGE:
summary = fmt.Sprintf("%s exchanged %s from a rack of %s",
who, evt.Exchanged, evt.Rack)
case pb.GameEvent_CHALLENGE_BONUS:
summary = fmt.Sprintf(" (+%d)", evt.Bonus)
case pb.GameEvent_END_RACK_PTS:
summary = fmt.Sprintf(" (+%d from opponent rack)", evt.EndRackPoints)
case pb.GameEvent_PHONY_TILES_RETURNED:
summary = fmt.Sprintf("(%s challenged off)", evt.PlayedTiles)
case pb.GameEvent_TIME_PENALTY:
summary = fmt.Sprintf("%s lost %d on time", who, evt.LostScore)
case pb.GameEvent_END_RACK_PENALTY:
summary = fmt.Sprintf("%s lost %d from their rack", who,
evt.LostScore)
}
return summary
}