summaryrefslogtreecommitdiff
path: root/engine/game.go
blob: 073595810446e76887bcae929dc23d562a07179b (plain)
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
package engine

import (
	"sakura/engine/pool"
)

type Turn struct {
	Player      *Player `json:"-"`
	PlayerSlot  string  `json:"Player"`
	ActionTaken bool
	Actions     []Action
}

type Game struct {
	Shadow   *pool.Pool
	Distance *pool.Pool
	Yin      *Player `json:"PlayerYin"`
	Yang     *Player `json:"PlayerYang"`

	Stack []IsCard

	Actions []Action

	CurrentPlayer     *Player `json:"-"`
	CurrentPlayerSlot string  `json:"CurrentPlayer"`

	History []Turn
}

func (g *Game) propagate() {
	g.Distance.OverflowsTo = g.Shadow
}

func NewGame() *Game {
	g := &Game{
		Shadow:   pool.New(0, pool.Infinite),
		Distance: pool.New(10, 10),
	}
	g.Yin = NewPlayer(g)
	g.Yang = NewPlayer(g)
	g.propagate()
	g.NextTurn()
	return g
}

var Tie = &Player{}

func (g *Game) Winner() *Player {
	if g.Yin.Life.Value <= 0 && g.Yang.Life.Value <= 0 {
		return Tie
	}
	if g.Yin.Life.Value <= 0 {
		return g.Yang
	}
	if g.Yang.Life.Value <= 0 {
		return g.Yin
	}
	return nil
}

func (g *Game) NextTurn() {
	current := g.CurrentPlayer.Opponent(g)
	g.CurrentPlayer = current
	playerRef := "UNKNOWN"
	switch current {
	case g.Yin:
		playerRef = "YIN"
	case g.Yang:
		playerRef = "YANG"
	}
	g.History = append(g.History, Turn{
		Player:      current,
		ActionTaken: false,
		PlayerSlot:  playerRef,
	})
	g.CurrentPlayerSlot = playerRef
	g.Actions = Interactions(ShuffleDeckAction(g), DrawCardAction(g))
}

func (g *Game) CurrentTurn() Turn {
	return g.History[len(g.History)-1]
}

func (g *Game) ActionToHistory(a Action) {
	thisTurn := g.History[len(g.History)-1]
	thisTurn.Actions = append(thisTurn.Actions, a)
}

func (g *Game) Present(p *Player) Game {
	localGame := *g
	switch p {
	case g.Yin:
		localGame.Yang.Hide()
	case g.Yang:
		localGame.Yin.Hide()
	}
	var acs []Action
	for _, a := range g.Actions {
		if a.Actor(g) == p {
			acs = append(acs, a)
		}
	}
	localGame.Actions = acs
	return localGame
}