dice-roller/Coin/Coin.go

47 lines
785 B
Go

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