-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (108 loc) · 2.37 KB
/
main.go
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
package main
import (
"fmt"
"log"
"os"
"runtime/pprof"
"github.com/segmentio/go-prompt"
"github.com/kmecnin/dice-stats/charts"
"github.com/kmecnin/dice-stats/input"
"github.com/kmecnin/dice-stats/stats"
)
func main() {
f, err := os.Create("/tmp/prof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
isPrompt := false
userInput, err := input.ParseCommand()
if nil != err {
if err.Error() == input.CommandHasNoArgs {
userInput, err = input.ParsePrompt()
isPrompt = true
}
if nil != err {
displayError(err)
}
}
if userInput.Versus == "" {
scoreDistribution(userInput)
} else {
winDistribution(userInput)
}
if isPrompt {
prompt.String("Press ENTER to exit")
}
}
func winDistribution(userInput input.Input) {
throw1, err := input.GetThrow(userInput.Query)
if nil != err {
displayError(err)
}
throw2, err := input.GetThrow(userInput.Versus)
if nil != err {
displayError(err)
}
displayMsgWin(throw1, throw2)
proba := stats.DistributionOfWin(throw1, throw2, userInput.Iterations)
charts.DrawProbabilitiesHistogramWin(proba)
}
func scoreDistribution(userInput input.Input) {
throw, err := input.GetThrow(userInput.Query)
if nil != err {
displayError(err)
}
displayMsgScore(throw)
proba := stats.DistributionOfScore(throw, userInput.Iterations)
charts.DrawProbabilitiesHistogramScore(proba)
}
func displayError(err error) {
fmt.Printf("Error: %v\n", err)
os.Exit(1)
}
func displayMsgScore(throw input.Throw) {
keepMessage := ""
if throw.KeepNumber > 0 {
keepMessage = fmt.Sprintf(
" and keeping %v best dice",
throw.KeepNumber,
)
}
fmt.Printf(
"Generating probabilities distributions using %v dice with %v faces%v\n",
throw.DiceNumber,
throw.DiceFaces,
keepMessage,
)
}
func displayMsgWin(throw1, throw2 input.Throw) {
fmt.Printf("Generating probabilities distributions:\n")
keepMessage1 := ""
if throw1.KeepNumber > 0 {
keepMessage1 = fmt.Sprintf(
" and keeping %v best dice",
throw1.KeepNumber,
)
}
fmt.Printf(
"\tPlayer1: %v dice with %v faces%v\n",
throw1.DiceNumber,
throw1.DiceFaces,
keepMessage1,
)
keepMessage2 := ""
if throw2.KeepNumber > 0 {
keepMessage2 = fmt.Sprintf(
" and keeping %v best dice",
throw1.KeepNumber,
)
}
fmt.Printf(
"vs\tPlayer2: %v dice with %v faces%v\n",
throw2.DiceNumber,
throw2.DiceFaces,
keepMessage2,
)
}