aboutsummaryrefslogtreecommitdiff
path: root/example/interactive.go
blob: 91b2390a3705d91ac99dfd06f1757be25612ff25 (plain)
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
// 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 main

import (
	"bufio"
	"fmt"
	"os"

	"muhq.space/go/muhq/goffel/logic"
)

var players []logic.Player
var dice logic.Dice
var reader = bufio.NewReader(os.Stdin)

func main() {
	getPlayers()
	for r := 1; r < 14; r++ {
		round(r)
	}
	broadcastWinner()
}

func getPlayers() {
	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
		}
		players = append(players, logic.NewPlayer(name))
		count++
	}
}

func round(r int) {
	fmt.Printf("Round %d\n", r)
	for i := range players {
		turn(&players[i])
	}
}

func turn(p *logic.Player) {
	dice.Roll(nil)

	fmt.Printf("%s's turn:\n", p.Name)
	fmt.Println(dice)

	var cmd logic.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 = logic.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 logic.CmdHelp(cmd.Argv[0]) {
				fmt.Println(helpLine)
			}
		case "p":
			fmt.Println(p.Score)
		case "d":
			fmt.Println(dice)
		case "q":
			os.Exit(0)
		case "i":
			points, err := p.Score.Insert(dice, cmd.Argv[0])
			if err != nil {
				fmt.Println("Insert failed:", err)
				continue
			}
			fmt.Printf("You inserted %d into %v.\n", points, logic.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", logic.ScoreNames[cmd.Argv[0]])
			return
		case "r":
			if rerolls < 2 {
				err := dice.Roll(cmd.Argv)
				if err != nil {
					fmt.Println("Reroll failed:", err)
					continue
				}
				rerolls++
				fmt.Println(dice)
				continue
			}
			fmt.Println("You are not allowed to reroll more than twice.")
		default:
		}
	}
}

func broadcastWinner() {
	fmt.Println("Fin!")
	winners, max := logic.FindBest(players)

	for _,p := range players {
		fmt.Printf("%v scored %d\n", p, p.Score.Score())
	}

	if len(winners) == 1 {
		fmt.Println("The Winner is:")
	} else {
		fmt.Println("The Winners are:")
	}
	for _, winner := range winners {
		fmt.Printf("%v with %v points\n", winner.Name, max)
	}
}