Sha256: b02583f694b3b82d72b3edca23b080171bee5911ed9faa21627490b3790783d6

Contents?: true

Size: 1.96 KB

Versions: 63

Compression:

Stored size: 1.96 KB

Contents

//Package yacht is a sample solution to the yacht exercise for go
package yacht

import (
	"sort"
)

//scoreN calculates the score of the lower categories
//It returns the total of those dice that equal n
func scoreN(dice []int, n int) int {
	score := 0
	for _, d := range dice {
		if d == n {
			score += n
		}
	}
	return score
}

//scoreAll adds up and returns the total of the five dice
func scoreAll(dice []int) int {
	return dice[0] + dice[1] + dice[2] + dice[3] + dice[4]
}

var diceValueMap = map[string]int{
	"ones": 1, "twos": 2, "threes": 3, "fours": 4, "fives": 5, "sixes": 6,
}

//Score returns the total score of a hand of Yacht dice in a given category.
func Score(dice []int, category string) int {
	sort.Ints(dice)
	switch category {
	case "ones", "twos", "threes", "fours", "fives", "sixes":
		return scoreN(dice, diceValueMap[category])
	case "full house":
		//Note, a yacht scores zero as a full house
		if dice[0] == dice[1] && dice[2] == dice[3] && dice[3] == dice[4] && dice[1] != dice[2] {
			return scoreAll(dice)
		}
		if dice[0] == dice[1] && dice[1] == dice[2] && dice[3] == dice[4] && dice[2] != dice[3] {
			return scoreAll(dice)
		}
		return 0
	case "four of a kind":
		//Note, a yacht can score as four of a kind, but only 4 dice are counted
		if dice[0] == dice[1] && dice[1] == dice[2] && dice[2] == dice[3] {
			return dice[0] * 4
		}
		if dice[1] == dice[2] && dice[2] == dice[3] && dice[3] == dice[4] {
			return dice[1] * 4
		}
		return 0
	case "little straight":
		if dice[0] == 1 && dice[1] == 2 && dice[2] == 3 && dice[3] == 4 && dice[4] == 5 {
			return 30
		}
		return 0
	case "big straight":
		if dice[0] == 2 && dice[1] == 3 && dice[2] == 4 && dice[3] == 5 && dice[4] == 6 {
			return 30
		}
		return 0
	case "choice":
		return scoreAll(dice)
	case "yacht":
		if dice[0] == dice[1] && dice[1] == dice[2] && dice[2] == dice[3] && dice[3] == dice[4] {
			return 50
		}
		return 0
	default:
		//This can't happen, so panic is appropriate
		panic("Unknown category")
	}
}

Version data entries

63 entries across 63 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/go/exercises/yacht/example.go
trackler-2.2.1.179 tracks/go/exercises/yacht/example.go
trackler-2.2.1.178 tracks/go/exercises/yacht/example.go
trackler-2.2.1.177 tracks/go/exercises/yacht/example.go
trackler-2.2.1.176 tracks/go/exercises/yacht/example.go
trackler-2.2.1.175 tracks/go/exercises/yacht/example.go
trackler-2.2.1.174 tracks/go/exercises/yacht/example.go
trackler-2.2.1.173 tracks/go/exercises/yacht/example.go
trackler-2.2.1.172 tracks/go/exercises/yacht/example.go
trackler-2.2.1.171 tracks/go/exercises/yacht/example.go
trackler-2.2.1.170 tracks/go/exercises/yacht/example.go
trackler-2.2.1.169 tracks/go/exercises/yacht/example.go
trackler-2.2.1.167 tracks/go/exercises/yacht/example.go
trackler-2.2.1.166 tracks/go/exercises/yacht/example.go
trackler-2.2.1.165 tracks/go/exercises/yacht/example.go
trackler-2.2.1.164 tracks/go/exercises/yacht/example.go
trackler-2.2.1.163 tracks/go/exercises/yacht/example.go
trackler-2.2.1.162 tracks/go/exercises/yacht/example.go
trackler-2.2.1.161 tracks/go/exercises/yacht/example.go
trackler-2.2.1.160 tracks/go/exercises/yacht/example.go