Add unique-mode for dice
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
8d438c6c01
commit
f8a616e284
1 changed files with 16 additions and 1 deletions
17
dice/main.go
17
dice/main.go
|
@ -24,6 +24,7 @@ var (
|
||||||
Sides int64 `flag:"sides,s" default:"6" description:"How many sides does the dice have?"`
|
Sides int64 `flag:"sides,s" default:"6" description:"How many sides does the dice have?"`
|
||||||
Template string `flag:"template" vardefault:"tpl" description:"Template to format the result with"`
|
Template string `flag:"template" vardefault:"tpl" description:"Template to format the result with"`
|
||||||
Type string `flag:"type,t" default:"dice" description:"What to throw (coin, dice)"`
|
Type string `flag:"type,t" default:"dice" description:"What to throw (coin, dice)"`
|
||||||
|
Unique bool `flag:"unique,u" default:"false" description:"Cannot roll the same number twice"`
|
||||||
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"`
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
|
@ -115,14 +116,28 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func containsNum(s []int64, n int64) bool {
|
||||||
|
for _, sn := range s {
|
||||||
|
if sn == n {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func getRandomNumber(sides int64) int64 {
|
func getRandomNumber(sides int64) int64 {
|
||||||
n, _ := rand.Int(rand.Reader, big.NewInt(sides))
|
n, _ := rand.Int(rand.Reader, big.NewInt(sides))
|
||||||
return n.Int64()
|
return n.Int64()
|
||||||
}
|
}
|
||||||
|
|
||||||
func getResults(sides, count int64) (res []int64, sum int64) {
|
func getResults(sides, count int64) (res []int64, sum int64) {
|
||||||
for i := int64(0); i < count; i++ {
|
for len(res) < int(count) {
|
||||||
v := getRandomNumber(sides) + 1
|
v := getRandomNumber(sides) + 1
|
||||||
|
|
||||||
|
if cfg.Unique && containsNum(res, v) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
res = append(res, v)
|
res = append(res, v)
|
||||||
sum += v
|
sum += v
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue