// 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 intPlayer struct { name string score Score } type Interactive struct { players []intPlayer } var dices Dices var reader = bufio.NewReader(os.Stdin) 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() { fmt.Println("Fin!") winners := []intPlayer{} 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 = []intPlayer{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) } } func turn(p *intPlayer) { 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: } } }