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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
package uis
import (
"fmt"
"strconv"
"strings"
. "muhq.space/go/muhq/goffel/logic"
)
type intPlayer struct {
name string
score Score
}
type Interactive struct {
players []intPlayer
}
var dices Dices
func (i *Interactive) Init() error {
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, intPlayer{name, NewScore()})
count++
}
return nil
}
func (ui *Interactive) Round(r int) error {
fmt.Printf("Round %d\n", r)
for i := range ui.players {
turn(&ui.players[i])
}
return nil
}
func (ui *Interactive) BroadcastWinner() {
w := []intPlayer{}
s := 0
for _, p := range ui.players {
t := p.score.Score()
if t > s || len(w) == 0 {
s = t
w = []intPlayer{p}
} else if t == s {
w = append(w, p)
}
}
if len(w) == 1 {
fmt.Println("The Winner is:")
} else {
fmt.Println("The Winners are:")
}
for _, p := range w {
fmt.Println(p.name, "with", p.score.Score(), "points")
}
}
func turn(p *intPlayer) {
dices.Roll(nil)
fmt.Printf("%s's turn:\n", p.name)
fmt.Println(dices)
var cmd string
var args [5]string
rerolls := 0
outer:
for {
cmd = " "
args[0], args[1], args[2], args[3], args[4] = "", "", "", "", ""
fmt.Print("Please enter a command(h for help): ")
fmt.Scanln(&cmd, &args[0], &args[1], &args[2], &args[3], &args[4])
switch cmd[0] {
case 'h':
cmdHelp(args[0])
case 'i':
pos, err := strconv.Atoi(args[0])
if err != nil {
fmt.Println("[pos] musst be a number.")
continue
}
points, err := p.score.Insert(dices, pos-1)
if err != nil {
fmt.Println("Insert failed:", err)
continue
}
fmt.Printf("You inserted %d into %d.\n", points, pos)
break outer
case 'p':
fmt.Println(p.score)
case 'd':
fmt.Println(dices)
case 'c':
pos, err := strconv.Atoi(args[0])
if err != nil {
fmt.Println("[pos] must be a number")
continue
}
err = p.score.Cancel(pos)
if err != nil {
fmt.Println(err)
continue
}
fmt.Println("You cancelled", pos)
break outer
case 'r':
if rerolls < 2 {
var idx []int
for _, c := range args {
if c == "" {
break
}
if n, err := strconv.Atoi(c); err == nil {
idx = append(idx, n)
} else {
fmt.Println(err)
fmt.Println("cant parse", c)
break
}
}
err := dices.Roll(idx)
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:
fmt.Println("Not a valid command. Try \"h\".")
}
}
}
func cmdHelp(cmd string) {
cmd = strings.TrimLeft(cmd, " ")
switch cmd {
case "d":
fmt.Println("d - print dices")
fmt.Println("Print out the dices")
case "p":
fmt.Println("p - print score")
fmt.Println("Print out your score<M-Escape>")
case "i":
fmt.Println("i [pos] - insert into score")
fmt.Println("Insert your current dices into your score.")
fmt.Println("See \"h pos\" for a explenation of the score entries")
case "r":
fmt.Println("r [dices] - reroll some dices")
fmt.Println("Reroll the specified dices.")
fmt.Println("The dices to reroll are represented by a list of their indices in the dice set.")
fmt.Println("Example:")
fmt.Println("\t\"r 1 4 5\" rerolls dice number 1, 4 and 5")
fmt.Println("\t\"r\" rerolls all dices")
case "h":
fmt.Println("h [cmd] - print help")
fmt.Println("Print the help for a specific command or gernal help.")
case "c":
fmt.Println("c [pos] - cancel a entry")
fmt.Println("Write 0 into your score.")
fmt.Println("See \"h pos\" for a explenation of the score entries")
case "pos":
fmt.Println("Positions:")
fmt.Println("\t1: Aces")
fmt.Println("\t2: Twos")
fmt.Println("\t3: Threes")
fmt.Println("\t4: Fours")
fmt.Println("\t5: Fives")
fmt.Println("\t6: Sixes")
fmt.Println("\t7: ThreeOfAKind")
fmt.Println("\t8: FourOfAKind")
fmt.Println("\t9: FullHouse")
fmt.Println("\t10: SmallStraight")
fmt.Println("\t11: LargeStraight")
fmt.Println("\t12: Yathzze")
fmt.Println("\t13: Chance")
default:
fmt.Println("h [cmd] - print help")
fmt.Println("p - print score")
fmt.Println("d - print dices")
fmt.Println("i [pos] - insert into score")
fmt.Println("r [dices] - reroll some dices")
fmt.Println("c [pos] - cancel a entry")
}
}
|