/
determineWinners.go
55 lines (51 loc) · 1.43 KB
/
determineWinners.go
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
package game
import (
"slices"
"github.com/Chopikashvili/shell-poker/card"
)
// Compares every player's best hand and determines the winner(s).
func DetermineWinners(hands []card.HandStrength) []int {
slices.SortStableFunc(hands, card.CompareHands)
slices.Reverse(hands)
winners := []int{}
//at least one hand must be the winning hand
winners = append(winners, hands[0].PlayerId)
for i := 1; i < len(hands); i++ {
if card.CompareHands(hands[0], hands[i]) == 0 {
winners = append(winners, hands[i].PlayerId)
} else {
break
}
}
//standardizes the output
slices.Sort(winners)
return winners
}
// Compares every player's best hand and determines the winner(s).
func DetermineWinnersSide(hands []card.HandStrength) ([]int, []int) {
slices.SortStableFunc(hands, card.CompareHands)
slices.Reverse(hands)
winners := []int{}
sideWinners := []int{}
//at least one hand must be the winning hand
winners = append(winners, hands[0].PlayerId)
for i := 1; i < len(hands); i++ {
if card.CompareHands(hands[0], hands[i]) == 0 {
winners = append(winners, hands[i].PlayerId)
} else {
sideWinners = append(sideWinners, hands[i].PlayerId)
for j := i + 1; j < len(hands); j++ {
if card.CompareHands(hands[i], hands[j]) == 0 {
winners = append(winners, hands[j].PlayerId)
} else {
break
}
}
break
}
}
//standardizes the output
slices.Sort(winners)
slices.Sort(sideWinners)
return winners, sideWinners
}