Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

cardgen func

package main

import(
	"fmt"
	"math/rand"
	"strconv"
	"time"
)

var cardTypes = []string {
	"ace",
	"king",
	"queen",
	"jack",
	"10",
	"9",
	"8",
	"7",
	"6",
	"5",
	"4",
	"3",
	"2",
}

func main() {
    total := addTotal(3) 
    fmt.Println(total)
}

func reverse(input string) int {
	//do something? return something?
	return 0
}

func skipTurn() {
	//do something? return nothing?
}

func addTotal(cards int) int {
	total := 0
	for i := 0; i < cards; i++ {
		var value int
		kind := RandomString(cardTypes)
		switch(kind) {
			case "4":
				value = reverse(kind)
			case "9":
				skipTurn()
			case "jack":
				value = 10
			case "queen":
				value = 10
			case "king":
				value = -1000
			case "ace":
				value = 1
				//value = 11
				//how do we decide?
			default:
				stringToInt, err := strconv.Atoi(kind)
				if !(err == nil) {
					value = 0
					fmt.Println(err, " - StrConv Failure")
					fmt.Println("Failed Value: ", kind)
				} else {
					value = stringToInt
				}
		}
		total += value
	}
	if total < 0 { return 99 }
	return total
}

func RandomString(options []string) string {
	rand.Seed(time.Now().Unix())
	randNum := rand.Int() % len(options)
	return options[randNum]
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.