From d6b5eeaa5e639c5ab89b5e1c93075a1b765794dc Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Tue, 27 Dec 2022 14:29:39 +0100 Subject: intermediate commit --- go/game/stack.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 go/game/stack.go (limited to 'go/game/stack.go') 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() + } +} -- cgit v1.2.3