diff options
Diffstat (limited to 'go/game/stack.go')
| -rw-r--r-- | go/game/stack.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/go/game/stack.go b/go/game/stack.go new file mode 100644 index 00000000..9132ae13 --- /dev/null +++ b/go/game/stack.go @@ -0,0 +1,46 @@ +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] + + err := s.gameState.ValidateAction(a) + if err == nil { + a.Resolve(s.gameState) + } + log.Println("Resolved", a, err) + s.gameState.broadcastNotification(NewResolvedActionNotification(a, err)) +} + +func (s *Stack) Resolve() { + for !s.IsEmpty() { + s.gameState.AllPassing(false) + s.Pop() + } +} |
