dice-roller/Coin/Coin.go

47 lines
785 B
Go
Raw Normal View History

2022-08-19 10:15:31 +02:00
package Coin
import (
"fmt"
"math/rand"
2022-08-22 17:44:16 +02:00
"time"
2022-08-19 10:15:31 +02:00
2022-08-22 17:44:16 +02:00
"github.com/fatih/color"
2022-08-19 10:15:31 +02:00
)
2022-08-22 17:44:16 +02:00
var initRandom bool
func InitRandom(initialised bool) {
if initialised {
return
}
rand.Seed(time.Now().Unix())
initRandom = true
}
2022-08-19 10:15:31 +02:00
func Toss(castAmount int) {
2022-08-22 17:44:16 +02:00
InitRandom(initRandom)
yellow := color.New(color.FgYellow).SprintFunc()
blue := color.New(color.FgBlue).SprintFunc()
2022-08-19 10:15:31 +02:00
var (
coins []string
coin string
coinState int
)
2022-08-22 17:44:16 +02:00
2022-08-19 10:15:31 +02:00
for i := 0; i < castAmount; i++ {
coinState = rand.Intn(2)
if coinState == 0 {
2022-08-22 17:44:16 +02:00
coin = yellow("heads")
2022-08-19 10:15:31 +02:00
} else {
2022-08-22 17:44:16 +02:00
coin = blue("tails")
2022-08-19 10:15:31 +02:00
}
coins = append(coins, coin)
}
if castAmount > 1 {
2022-08-22 17:44:16 +02:00
color.Yellow("Tossing %d coins...\n", castAmount)
2022-08-19 10:15:31 +02:00
} else {
2022-08-22 17:44:16 +02:00
color.Yellow("Tossing coin...\n", castAmount)
2022-08-19 10:15:31 +02:00
}
2022-08-22 17:44:16 +02:00
fmt.Println(fmt.Sprint(coins))
2022-08-19 10:15:31 +02:00
}