aboutsummaryrefslogtreecommitdiff
path: root/go/game/winCondition.go
blob: ab73dcb52b78d4ad64d936ae25597bba445f5194 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package game

import (
	"fmt"
	"slices"
)

// A WinCondition determines if there are winners using the current game state.
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.
var DummyWinCondition = &winCondition{
	condition: func(*LocalState) []*Player {
		return []*Player{}
	},
	desc: "dummy wincondition",
}

func BossGame(name string) *winCondition {
	return &winCondition{
		condition: func(s *LocalState) []*Player {
			bossFound := false
			s.FilterUnits(func(u *Unit) bool {
				if u.Card().Name == "The "+name {
					bossFound = true
				}
				return false
			})

			var winners []*Player
			if bossFound {
				if winners = explicitWinners(s); len(winners) > 0 {
					return winners
				}

				return singleConcessionWinner(s)
			}

			// No Boss was found
			for _, p := range s.players {
				if p.Name != name && !p.Conceded && !slices.Contains(winners, p) {
					winners = append(winners, p)
				}
			}

			return winners
		},
		desc: fmt.Sprintf("destroy the %s", name),
	}
}

// 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{}{}
		}

		var winners []*Player
		if len(foundKings) == len(s.Players()) {
			if winners = explicitWinners(s); len(winners) > 0 {
				return winners
			}

			return singleConcessionWinner(s)
		}

		nPlayers := len(s.Players())
		loosers := make([]*Player, nPlayers)
		copy(loosers, s.Players())
		for p := range foundKings {
			// Considere players who have conceded as loosers
			if p.Conceded {
				continue
			}
			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()
		}

		for _, p := range s.Players() {
			if !slices.Contains(loosers, p) && !slices.Contains(winners, p) {
				winners = append(winners, p)
			}
		}
		return winners
	},
	desc: "Destroy the enemy King",
}

// DeathMatch returns the players without enemy units.
var DeathMatch = &winCondition{
	condition: func(s *LocalState) []*Player {
		winners := explicitWinners(s)
		for _, p := range s.players {
			if len(s.EnemyUnits(p)) == 0 && !slices.Contains(winners, p) {
				winners = append(winners, p)
			}
		}
		if len(winners) > 0 {
			return winners
		}
		return singleConcessionWinner(s)
	},
	desc: "Destroy all enemy units",
}

// singleConcessionWinner returns the player left over after the rest conceded.
func singleConcessionWinner(s *LocalState) []*Player {
	var winner *Player
	for _, p := range s.players {
		if p.Conceded {
			continue
		}
		if winner == nil {
			winner = p
			// Two players have not conceded yet
		} else {
			return nil
		}
	}
	return []*Player{winner}
}

// explicitWinners returns the players that explicitly won the game.
// Winning the game is sometimes possible without fulfilling a maps win condition,
// but by achieving some external condition (like Approach Supremacy!).
func explicitWinners(s *LocalState) []*Player {
	winners := []*Player{}
	for _, p := range s.players {
		if p.Won {
			winners = append(winners, p)
		}
	}
	return winners
}