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
|
// Copyright (c) 2016 Florian Fischer. All rights reserved.
// Use of this source code is governed by a MIT license found in the LICENSE file.
package uis
import (
"bufio"
"fmt"
"os"
. "muhq.space/go/muhq/goffel/logic"
)
type Interactive struct {
players []Player
}
var dices Dice
var reader = bufio.NewReader(os.Stdin)
func (i *Interactive) Run() {
i.init()
for r := 1; r < 14; r++ {
i.round(r)
}
i.broadcastWinner()
}
func (i *Interactive) init() {
name := ""
count := 0
fmt.Println("Please enter your player names. An empty line ends the player registration.")
for {
fmt.Printf("%d: ", count+1)
_, err := fmt.Scanln(&name)
if err != nil {
break
}
i.players = append(i.players, NewPlayer(name))
count++
}
}
func (ui *Interactive) round(r int) {
fmt.Printf("Round %d\n", r)
for i := range ui.players {
turn(&ui.players[i])
}
}
func turn(p *Player) {
dices.Roll(nil)
fmt.Printf("%s's turn:\n", p.Name)
fmt.Println(dices)
var cmd Cmd
rerolls := 0
for {
fmt.Print("Please enter a command(h for help): ")
input, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
cmd, err = ParseCmd(input[0 : len(input)-1])
if err != nil {
fmt.Printf("%v. Try \"h\".\n", err)
continue
}
switch cmd.Cmd {
case "h":
fmt.Println()
for _, helpLine := range CmdHelp(cmd.Argv[0]) {
fmt.Println(helpLine)
}
case "p":
fmt.Println(p.Score)
case "d":
fmt.Println(dices)
case "q":
os.Exit(0)
case "i":
points, err := p.Score.Insert(dices, cmd.Argv[0])
if err != nil {
fmt.Println("Insert failed:", err)
continue
}
fmt.Printf("You inserted %d into %v.\n", points, ScoreNames[cmd.Argv[0]])
return
case "c":
err = p.Score.Cancel(cmd.Argv[0])
if err != nil {
fmt.Println(err)
continue
}
fmt.Println("You cancelled", ScoreNames[cmd.Argv[0]])
return
case "r":
if rerolls < 2 {
err := dices.Roll(cmd.Argv)
if err != nil {
fmt.Println("Reroll failed:", err)
continue
}
rerolls++
fmt.Println(dices)
continue
}
fmt.Println("You are not allowed to reroll more than twice.")
default:
}
}
}
func (ui *Interactive) broadcastWinner() {
fmt.Println("Fin!")
winners := []Player{}
max := 0
for _, player := range ui.players {
t := player.Score.Score()
if t > max || len(winners) == 0 {
for _, winner := range winners {
fmt.Printf("\t%v scored %v points\n", winner.Name, max)
}
max = t
winners = []Player{player}
} else if t == max {
winners = append(winners, player)
} else {
fmt.Printf("\t%v scored %v points\n", player.Name, t)
}
}
if len(winners) == 1 {
fmt.Println("The Winner is:")
} else {
fmt.Println("The Winners are:")
}
for _, winner := range winners {
fmt.Printf("\t%v with %v points\n", winner.Name, max)
}
}
|