package main 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") 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") } }