aboutsummaryrefslogtreecommitdiff
path: root/go/game/stack.go
blob: 22c20fa3a91c5ad66e5a6c4a88475007d7959f7f (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
package game

import (
	"log"
)

type Stack struct {
	gameState *State
	Actions   []Action
}

func NewStack(s *State) *Stack {
	return &Stack{s, []Action{}}
}

func (s *Stack) IsEmpty() bool {
	return len(s.Actions) == 0
}

func (s *Stack) Push(a Action) {
	s.Actions = append(s.Actions, a)
}

func (s *Stack) Pop() {
	l := len(s.Actions)
	if l == 0 {
		log.Fatalf("Can not pop from empty stack")
	}
	a := s.Actions[l-1]
	s.Actions = s.Actions[:l-1]
	a.Resolve(s.gameState)
}