diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2025-08-19 14:33:48 +0200 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2025-08-20 15:57:39 +0200 |
| commit | 9674fc8abd20287663a0312694319f98827dd50a (patch) | |
| tree | b117ddb716a84ca665e0cc2d2dfde0e389c0058b | |
| parent | 9884eca784e853e48ff82ecd904890d28b2fe0fa (diff) | |
| download | muhqs-game-9674fc8abd20287663a0312694319f98827dd50a.tar.gz muhqs-game-9674fc8abd20287663a0312694319f98827dd50a.zip | |
fix king game wincondition and add draw special case
| -rw-r--r-- | go/game/winCondition.go | 6 | ||||
| -rw-r--r-- | go/game/winCondition_test.go | 40 |
2 files changed, 45 insertions, 1 deletions
diff --git a/go/game/winCondition.go b/go/game/winCondition.go index ded9b0ca..ec6a76e4 100644 --- a/go/game/winCondition.go +++ b/go/game/winCondition.go @@ -27,7 +27,7 @@ func KingGame(s *LocalState) []*Player { return []*Player{} } - loosers := make([]*Player, 0, len(s.Players())) + loosers := make([]*Player, 2, len(s.Players())) copy(loosers, s.Players()) for p := range foundKings { i := slices.Index(loosers, p) @@ -35,6 +35,10 @@ func KingGame(s *LocalState) []*Player { 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) { diff --git a/go/game/winCondition_test.go b/go/game/winCondition_test.go new file mode 100644 index 00000000..4ecd472e --- /dev/null +++ b/go/game/winCondition_test.go @@ -0,0 +1,40 @@ +package game + +import ( + "testing" + + "golang.org/x/exp/slices" +) + +func TestKingGame(t *testing.T) { + s := NewLocalState() + s.SetMap(newEmpty2x2Map()) + s._map.WinCondition = KingGame + p := s.AddNewPlayer("p", NewDeck()) + k1 := s.addNewUnit(NewCard("misc/king"), Position{0, 0}, p) + o := s.AddNewPlayer("o", NewDeck()) + k2 := s.addNewUnit(NewCard("misc/king"), Position{1, 1}, o) + + w := s._map.WinCondition(s) + if len(w) != 0 { + t.Fatal("winners declared with two kings") + } + + s.destroyPermanent(k2) + w = s._map.WinCondition(s) + if len(w) != 1 { + t.Fatal("winner not declared", w) + } + if w[0] != p { + t.Fatal("p is not the winner", w) + } + + s.destroyPermanent(k1) + w = s._map.WinCondition(s) + if len(w) != 2 { + t.Fatal("winners not declared", w) + } + if !slices.Contains(w, p) || !slices.Contains(w, o) { + t.Fatal("p or o is not a winner", w) + } +} |
