-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloveLetter.go
107 lines (84 loc) · 2.18 KB
/
loveLetter.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
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
package main
import (
"fmt"
"strings"
)
func playersOut(players []*Player) int {
out := 0
for _, p := range players {
if p.out {
out++
}
}
return out
}
func maxCardValue(players []*Player) int {
max := 0
for _, p := range players {
if len(p.hand) > 0 {
value := p.hand[0].value
if !p.out && value > max {
max = value
}
}
}
return max
}
func main() {
deck := NewDeck()
// Simulate burning the top card
deck.Draw()
players := make([]*Player, 0, 4)
for _, name := range []string{"One", "Two", "Three", "Four"} {
players = append(players, NewPlayer(name, deck))
}
for i, p := range players {
p.players = players
p.index = i
p.cpu = true
p.Draw()
}
// players[0].cpu = false
winners := make([]string, 0, 4)
for len(winners) == 0 && !deck.IsEmpty() {
for _, p := range players {
if p.out {
continue
}
if playersOut(players) == len(players) - 1 {
p.points++
winners = append(winners, p.Name())
break
}
p.isImmune = false
fmt.Println()
success := p.Draw()
if !success {
fmt.Println("Deck is empty")
break
}
fmt.Printf("%s: %s\n", p.Name(), p.ShowHand())
p.Play()
}
}
if len(winners) == 0 {
fmt.Println()
winningValue := maxCardValue(players)
for _, p := range players {
if !p.out {
// TODO Sometimes players are not out, but also
// have empty hands, which shouldn't be possible
fmt.Println(p.Name(), "has", p.hand[0].String())
if p.hand[0].value == winningValue {
winners = append(winners, p.Name())
}
}
}
}
fmt.Println()
if len(winners) == 1 {
fmt.Println("Winner is", winners[0])
} else {
fmt.Println("Winners are", strings.Join(winners, " and "))
}
}