-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.go
76 lines (66 loc) · 1.11 KB
/
match.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
package dominocount
type match struct {
Score1, Score2 int
Team1, Team2 string
Id int64
}
type matchOption func(*match) error
func NewMatch(opts ...matchOption) match {
m := match{
Team1: string(Team1),
Team2: string(Team2),
Score1: 0,
Score2: 0,
}
for _, opt := range opts {
//ignoring errors as current name option generates no errors
opt(&m)
}
return m
}
func MatchWithTeam1Name(name string) matchOption {
return func(m *match) error {
if name != "" {
m.Team1 = name
}
return nil
}
}
func MatchWithTeam2Name(name string) matchOption {
return func(m *match) error {
if name != "" {
m.Team2 = name
}
return nil
}
}
func (m *match) AddPoints(t team, points int) {
if m.GameOver() {
return
}
if points < 0 {
return
}
if t == Team1 {
m.Score1 += points
return
}
m.Score2 += points
}
func (m match) Score(t team) int {
if t == Team1 {
return m.Score1
}
return m.Score2
}
func (m match) GameOver() bool {
if m.Score1 >= 200 || m.Score2 >= 200 {
return true
}
return false
}
type team string
const (
Team1 team = "Team1"
Team2 team = "Team2"
)