aboutsummaryrefslogtreecommitdiff
path: root/go/game/stack.go
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2022-12-27 14:29:39 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2025-08-20 15:46:38 +0200
commitd6b5eeaa5e639c5ab89b5e1c93075a1b765794dc (patch)
treef87422bb91c2197f88aa8d29092592858764049d /go/game/stack.go
parent3c2ee4ee48480a28dc606878503f314ec8862d45 (diff)
downloadmuhqs-game-d6b5eeaa5e639c5ab89b5e1c93075a1b765794dc.tar.gz
muhqs-game-d6b5eeaa5e639c5ab89b5e1c93075a1b765794dc.zip
intermediate commit
Diffstat (limited to 'go/game/stack.go')
-rw-r--r--go/game/stack.go46
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()
+ }
+}