diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2025-08-20 14:44:51 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-08-20 14:50:08 +0200 |
| commit | 20b7135e023c71b84719f8e58df532f2dba0a311 (patch) | |
| tree | 5ff31782d6dde0d3eabd2f79b4e4b34ec696b6dd /go | |
| parent | b1f0bfebc5c585e50eb432db8127125bd392a4cd (diff) | |
| download | muhqs-game-go.tar.gz muhqs-game-go.zip | |
add description to wincondition to show the usergo
Diffstat (limited to 'go')
| -rw-r--r-- | go/game/kraken.go | 2 | ||||
| -rw-r--r-- | go/game/state.go | 2 | ||||
| -rw-r--r-- | go/game/winCondition.go | 107 | ||||
| -rw-r--r-- | go/game/winCondition_test.go | 6 |
4 files changed, 71 insertions, 46 deletions
diff --git a/go/game/kraken.go b/go/game/kraken.go index 8e4ee6f7..25a3f19b 100644 --- a/go/game/kraken.go +++ b/go/game/kraken.go @@ -72,7 +72,7 @@ func addKrakenControl(kraken *Player) { ctrl.syncGameState.Add(1) go func() { s := kraken.gameState - for len(s.Map().WinCondition(s)) == 0 { + for len(s.Map().WinCondition.check(s)) == 0 { ctrl.krakenTurn() } }() diff --git a/go/game/state.go b/go/game/state.go index e7d77f4c..f53f46ae 100644 --- a/go/game/state.go +++ b/go/game/state.go @@ -230,7 +230,7 @@ func (s *LocalState) stateBasedActions() []*Player { s.handleTriggers() - w := s._map.WinCondition(s) + w := s._map.WinCondition.check(s) return w } diff --git a/go/game/winCondition.go b/go/game/winCondition.go index ec6a76e4..a0728e60 100644 --- a/go/game/winCondition.go +++ b/go/game/winCondition.go @@ -5,56 +5,81 @@ import ( ) // A WinCondition determines if there are winners using the current game state. -type WinCondition func(*LocalState) []*Player +type WinCondition interface { + check(*LocalState) []*Player + String() string +} + +type winCondition struct { + condition func(*LocalState) []*Player + desc string +} + +func (w *winCondition) check(s *LocalState) []*Player { + return w.condition(s) +} + +func (w *winCondition) String() string { + return w.desc +} // DummyWinCondition always return an empty winner slice. -func DummyWinCondition(*LocalState) []*Player { - return []*Player{} +var DummyWinCondition = &winCondition{ + condition: func(*LocalState) []*Player { + return []*Player{} + }, + desc: "dummy wincondition", } -// KingGame reports the winners of a game using kings. -func KingGame(s *LocalState) []*Player { - foundKings := map[*Player]struct{}{} - for _, u := range s.Units() { - if u.card.Name != "King" { - continue +// KingGame reports all players without an enemy king. +var KingGame = &winCondition{ + condition: func(s *LocalState) []*Player { + foundKings := map[*Player]struct{}{} + for _, u := range s.Units() { + if u.card.Name != "King" { + continue + } + + foundKings[u.owner] = struct{}{} } - foundKings[u.owner] = struct{}{} - } + if len(foundKings) == len(s.Players()) { + return []*Player{} + } - if len(foundKings) == len(s.Players()) { - return []*Player{} - } - - loosers := make([]*Player, 2, len(s.Players())) - copy(loosers, s.Players()) - for p := range foundKings { - i := slices.Index(loosers, p) - loosers[i] = loosers[len(loosers)-1] - loosers = loosers[:len(loosers)-1] - } - - if len(loosers) == len(s.Players()) { - return s.Players() - } - - winners := []*Player{} - for _, p := range s.Players() { - if !slices.Contains(loosers, p) { - winners = append(winners, p) + loosers := make([]*Player, 2, len(s.Players())) + copy(loosers, s.Players()) + for p := range foundKings { + i := slices.Index(loosers, p) + loosers[i] = loosers[len(loosers)-1] + loosers = loosers[:len(loosers)-1] + } + + if len(loosers) == len(s.Players()) { + return s.Players() + } + + winners := []*Player{} + for _, p := range s.Players() { + if !slices.Contains(loosers, p) { + winners = append(winners, p) + } } - } - return winners + return winners + }, + desc: "Destroy the enemy King", } -// DeathMatch returns the players without enemy &nits. -func DeathMatch(s *LocalState) []*Player { - winners := []*Player{} - for _, p := range s.players { - if len(s.EnemyUnits(p)) == 0 { - winners = append(winners, p) +// DeathMatch returns the players without enemy units. +var DeathMatch = &winCondition{ + condition: func(s *LocalState) []*Player { + winners := []*Player{} + for _, p := range s.players { + if len(s.EnemyUnits(p)) == 0 { + winners = append(winners, p) + } } - } - return winners + return winners + }, + desc: "Destroy all enemy units", } diff --git a/go/game/winCondition_test.go b/go/game/winCondition_test.go index 4ecd472e..c50e9692 100644 --- a/go/game/winCondition_test.go +++ b/go/game/winCondition_test.go @@ -15,13 +15,13 @@ func TestKingGame(t *testing.T) { o := s.AddNewPlayer("o", NewDeck()) k2 := s.addNewUnit(NewCard("misc/king"), Position{1, 1}, o) - w := s._map.WinCondition(s) + w := s._map.WinCondition.check(s) if len(w) != 0 { t.Fatal("winners declared with two kings") } s.destroyPermanent(k2) - w = s._map.WinCondition(s) + w = s._map.WinCondition.check(s) if len(w) != 1 { t.Fatal("winner not declared", w) } @@ -30,7 +30,7 @@ func TestKingGame(t *testing.T) { } s.destroyPermanent(k1) - w = s._map.WinCondition(s) + w = s._map.WinCondition.check(s) if len(w) != 2 { t.Fatal("winners not declared", w) } |
