/
game.go
520 lines (459 loc) · 13.3 KB
/
game.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// Package acquire manages the flow and status of an acquire game. It acts
// like a finite state machine (FSM), in which received inputs modify
// machine state
package acquire
import (
"container/ring"
"errors"
"fmt"
"sort"
"time"
"math/rand"
"github.com/svera/acquire/board"
"github.com/svera/acquire/corporation"
"github.com/svera/acquire/fsm"
"github.com/svera/acquire/interfaces"
"github.com/svera/acquire/tileset"
)
const (
totalCorporations = 7
endGameCorporationSize = 41
)
// Game stores state of game elements and provides methods to control game flow
type Game struct {
board interfaces.Board
stateMachine interfaces.StateMachine
players *ring.Ring
corporations [7]interfaces.Corporation
tileset interfaces.Tileset
initialPlayerNumber int
newCorpTiles []interfaces.Tile
mergeCorps map[string][]interfaces.Corporation
sellTradePlayers []interfaces.Player
lastPlayedTile interfaces.Tile
round int
isLastRound bool
// When in sell_trade state, the current player is stored here temporary as the turn
// is passed to all defunct corporations stockholders
frozenPlayer interfaces.Player
}
// New initialises a new Acquire game
func New(players map[int]interfaces.Player, optional Optional) (*Game, error) {
var err error
if len(players) < 3 || len(players) > 6 {
return nil, errors.New(WrongNumberPlayers)
}
if optional, err = initOptionalParameters(optional); err == nil {
gm := Game{
board: optional.Board,
corporations: optional.Corporations,
tileset: optional.Tileset,
round: 1,
stateMachine: optional.StateMachine,
isLastRound: false,
}
gm.initPlayersRing(players)
for i := range gm.corporations {
gm.corporations[i].SetPricesChart(gm.setPricesChart(i))
}
gm.pickRandomPlayer()
return &gm, nil
}
return nil, err
}
// initRing puts all players into a ring struct, so they can be looped in an
// order, circular fashion.
func (g *Game) initPlayersRing(players map[int]interfaces.Player) {
var playerNumbers []int
g.players = ring.New(len(players))
for k := range players {
playerNumbers = append(playerNumbers, k)
}
sort.Ints(playerNumbers)
for _, n := range playerNumbers {
g.giveInitialHand(players[n])
g.players = g.players.Next()
g.players.Value = players[n]
}
}
func initOptionalParameters(optional Optional) (Optional, error) {
if areCorporationsEmpty(optional.Corporations) {
optional.Corporations = defaultCorporations()
}
if optional.Board == nil {
optional.Board = board.New()
}
if optional.Tileset == nil {
optional.Tileset = tileset.New()
}
if optional.StateMachine == nil {
optional.StateMachine = fsm.New()
}
return optional, nil
}
func areCorporationsEmpty(corporations [7]interfaces.Corporation) bool {
for i := range corporations {
if corporations[i] == nil {
return true
}
}
return false
}
// Corporations returns an array with all seven corporations
func (g *Game) Corporations() [7]interfaces.Corporation {
return g.corporations
}
// Initialises player hand of tiles
func (g *Game) giveInitialHand(plyr interfaces.Player) {
for i := 0; i < 6; i++ {
tile, _ := g.tileset.Draw()
plyr.PickTile(tile)
}
}
// AreEndConditionsReached checks if game end conditions are reached
func (g *Game) AreEndConditionsReached() bool {
active := g.activeCorporations()
safe := 0
if len(active) == 0 {
return false
}
for _, corp := range active {
if corp.Size() >= endGameCorporationSize {
return true
}
if corp.IsSafe() {
safe++
}
}
if safe == len(active) {
return true
}
return false
}
// ActiveCorporations returns all corporations on the board
func (g *Game) activeCorporations() []interfaces.Corporation {
return g.findCorporationsByActiveState(true)
}
func (g *Game) findCorporationsByActiveState(value bool) []interfaces.Corporation {
result := []interfaces.Corporation{}
for _, corp := range g.corporations {
if corp.IsActive() == value {
result = append(result, corp)
}
}
return result
}
// IsCorporationDefunct return true if the passed corporation is in a merge process
// and will disappear from the board after that merge is complete, false otherwise
func (g *Game) IsCorporationDefunct(corp interfaces.Corporation) bool {
for _, defunct := range g.mergeCorps["defunct"] {
if corp == defunct {
return true
}
}
return false
}
// IsTilePlayable returns false if the passed tile is either
// temporary or permanently unplayable, true otherwise
func (g *Game) IsTilePlayable(tl interfaces.Tile) bool {
if g.isTileTemporarilyUnplayable(tl) || g.isTilePermanentlyUnplayable(tl) {
return false
}
return true
}
// Returns true if a tile is permanently unplayable, that is,
// that putting it on the board would merge two or more safe corporations
func (g *Game) isTilePermanentlyUnplayable(tl interfaces.Tile) bool {
adjacents := g.board.AdjacentCorporations(tl.Number(), tl.Letter())
safeNeighbours := 0
for _, adjacent := range adjacents {
if adjacent.IsSafe() {
safeNeighbours++
}
if safeNeighbours == 2 {
return true
}
}
return false
}
// Returns true if a tile is temporarily unplayable, that is,
// that putting it on the board would create an 8th corporation
func (g *Game) isTileTemporarilyUnplayable(tl interfaces.Tile) bool {
if len(g.activeCorporations()) < totalCorporations {
return false
}
adjacents := g.board.AdjacentCells(tl.Number(), tl.Letter())
emptyAdjacentCells := 0
for _, adjacent := range adjacents {
if adjacent.Type() == interfaces.CorporationOwner {
return false
}
if adjacent.Type() == interfaces.EmptyOwner {
emptyAdjacentCells++
}
}
if emptyAdjacentCells == len(adjacents) {
return false
}
return true
}
// Player returns player with passed number
func (g *Game) Player(playerNumber int) interfaces.Player {
cp := g.players.Next()
for i := 0; i < cp.Len(); i++ {
if cp.Value.(interfaces.Player).Number() == playerNumber {
return cp.Value.(interfaces.Player)
}
cp = cp.Next()
}
return nil
}
// CurrentPlayer returns player currently in play
func (g *Game) CurrentPlayer() interfaces.Player {
return g.players.Value.(interfaces.Player)
}
// PlayTile puts the given tile on board and triggers related actions
func (g *Game) PlayTile(tl interfaces.Tile) error {
if err := g.checkTile(tl); err != nil {
return err
}
g.CurrentPlayer().DiscardTile(tl)
g.lastPlayedTile = tl
if merge, mergeCorps := g.board.TileMergeCorporations(tl); merge {
g.startMerge(tl, mergeCorps)
} else if corpFounded, tiles := g.board.TileFoundCorporation(tl); corpFounded {
g.board.PutTile(tl)
g.stateMachine.ToFoundCorp()
g.newCorpTiles = tiles
} else if grow, tiles, corp := g.board.TileGrowCorporation(tl); grow {
g.growCorporation(corp, tiles)
g.stateMachine.ToBuyStock()
} else {
return g.putUnincorporatedTile(tl)
}
return nil
}
func (g *Game) checkTile(tl interfaces.Tile) error {
if g.stateMachine.CurrentStateName() != interfaces.PlayTileStateName {
return fmt.Errorf(ActionNotAllowed, "play_tile", g.stateMachine.CurrentStateName())
}
if g.isTileTemporarilyUnplayable(tl) {
return errors.New(TileTemporarilyUnplayable)
}
if !g.CurrentPlayer().HasTile(tl) {
return errors.New(TileNotOnHand)
}
return nil
}
func (g *Game) putUnincorporatedTile(tl interfaces.Tile) error {
g.board.PutTile(tl)
if g.existActiveCorporations() {
g.stateMachine.ToBuyStock()
return nil
}
return g.endTurn()
}
func (g *Game) existActiveCorporations() bool {
for _, corp := range g.corporations {
if corp.IsActive() {
return true
}
}
return false
}
// Sets player currently in play
func (g *Game) setCurrentPlayer(pl interfaces.Player) *Game {
for i := 0; i < g.players.Len(); i++ {
if g.players.Value.(interfaces.Player) != pl {
g.players = g.players.Next()
}
}
return g
}
// FoundCorporation founds a new corporation
func (g *Game) FoundCorporation(corp interfaces.Corporation) error {
if g.stateMachine.CurrentStateName() != interfaces.FoundCorpStateName {
return fmt.Errorf(ActionNotAllowed, "found_corp", g.stateMachine.CurrentStateName())
}
if corp.IsActive() {
return errors.New(CorporationAlreadyOnBoard)
}
g.board.SetOwner(corp, g.newCorpTiles)
corp.Grow(len(g.newCorpTiles))
g.newCorpTiles = []interfaces.Tile{}
g.getFounderStockShare(g.CurrentPlayer(), corp)
g.stateMachine.ToBuyStock()
return nil
}
// Receive a free stock share from a recently founded corporation, if it has
// remaining shares available
// TODO this should trigger an event warning that no founder stock share will be given
// of the founded corporation has no stock shares left
func (g *Game) getFounderStockShare(pl interfaces.Player, corp interfaces.Corporation) {
if corp.Stock() > 0 {
corp.RemoveStock(1)
pl.AddShares(corp, 1)
}
}
// Makes a corporation grow with the passed tiles
func (g *Game) growCorporation(corp interfaces.Corporation, tiles []interfaces.Tile) {
g.board.SetOwner(corp, tiles)
corp.Grow(len(tiles))
}
// RemovePlayer gets the received player and removes him/her from the game
// because the player has left the game. All player's
// assets are returned to its respective origin sets. If the removed player was in
// his/her turn, turn passes to the next player.
func (g *Game) RemovePlayer(pl interfaces.Player) {
pl.RemoveCash(pl.Cash())
g.tileset.Add(pl.Tiles())
for _, corp := range g.Corporations() {
if pl.Shares(corp) > 0 {
corp.AddStock(pl.Shares(corp))
pl.RemoveShares(corp, pl.Shares(corp))
}
}
// Is current player the one to remove? Then end his/her turn before removing
if g.players.Value.(interfaces.Player) == pl {
g.players = g.players.Prev()
g.players.Unlink(1)
g.nextPlayer()
} else {
search := g.players
for i := 0; i < g.players.Len(); i++ {
if search.Next().Value.(interfaces.Player) == pl {
search.Unlink(1)
break
}
search = search.Next()
}
}
if g.players.Len() < 3 {
g.stateMachine.ToInsufficientPlayers()
return
}
}
// Round returns the current round number
func (g *Game) Round() int {
return g.round
}
// ClaimEndGame allows the current player to claim end game
// This can be done at any time. After announcing that the game is over,
// the player may finish his/her turn.
func (g *Game) ClaimEndGame() *Game {
if g.AreEndConditionsReached() {
g.isLastRound = true
}
return g
}
// Board returns game's board instance
func (g *Game) Board() interfaces.Board {
return g.board
}
// GameStateName returns game's current state
func (g *Game) GameStateName() string {
return g.stateMachine.CurrentStateName()
}
func (g *Game) endTurn() error {
var err error
if g.isLastRound {
g.stateMachine.ToEndGame()
return g.finish()
}
if err = g.drawTile(); err == nil || err.Error() == tileset.NoTilesAvailable {
g.nextPlayer()
}
return err
}
// Passes the turn to the next player
func (g *Game) nextPlayer() {
if g.stateMachine.CurrentStateName() != interfaces.PlayTileStateName {
g.stateMachine.ToPlayTile()
}
if g.players.Value.(interfaces.Player).Number() > g.players.Next().Value.(interfaces.Player).Number() {
g.round++
}
g.players = g.players.Next()
if len(g.CurrentPlayer().Tiles()) == 0 {
g.stateMachine.ToEndGame()
g.finish()
}
if g.isHandUnplayable() {
g.replaceWholeHand()
}
}
// IsLastRound returns if the current round will be the last one or not
func (g *Game) IsLastRound() bool {
return g.isLastRound
}
// A player takes a tile from the facedown cluster to replace
// the one he/she played. This is not done until the end of
// the turn.
func (g *Game) drawTile() error {
var tile interfaces.Tile
var err error
if tile, err = g.tileset.Draw(); err != nil {
return err
}
g.CurrentPlayer().PickTile(tile)
if err = g.replaceUnplayableTiles(); err != nil {
return err
}
return nil
}
// If a player has any permanently
// unplayable tiles that player discard the unplayable tiles
// and draws an equal number of replacement tiles. This can
// only be done once per turn.
func (g *Game) replaceUnplayableTiles() error {
for _, tile := range g.CurrentPlayer().Tiles() {
if g.isTilePermanentlyUnplayable(tile) {
g.CurrentPlayer().DiscardTile(tile)
if newTile, err := g.tileset.Draw(); err == nil {
g.CurrentPlayer().PickTile(newTile)
} else {
return err
}
}
}
return nil
}
// This checks the (highly improbable, but possible) case in which all tiles in a
// hand are not playable and thus, the player cannot place a tile as it is forced
// to
func (g *Game) isHandUnplayable() bool {
for _, tile := range g.CurrentPlayer().Tiles() {
if g.IsTilePlayable(tile) {
return false
}
}
return true
}
// This must be only used if a player hand is COMPLETELY unplayable
func (g *Game) replaceWholeHand() error {
for _, tile := range g.CurrentPlayer().Tiles() {
g.CurrentPlayer().DiscardTile(tile)
if newTile, err := g.tileset.Draw(); err == nil {
g.CurrentPlayer().PickTile(newTile)
} else {
return err
}
}
return nil
}
func defaultCorporations() [7]interfaces.Corporation {
var corporations [7]interfaces.Corporation
for i := 0; i < 7; i++ {
corporations[i] = corporation.New()
}
return corporations
}
// Choose a random player who'll be the one to start playing
func (g *Game) pickRandomPlayer() {
source := rand.NewSource(time.Now().UnixNano())
rn := rand.New(source)
numberPlayers := g.players.Len()
move := rn.Intn(numberPlayers - 1)
g.players = g.players.Move(move)
g.initialPlayerNumber = g.players.Value.(interfaces.Player).Number()
}