From 0d6bd4a9d4d81673b44a7a445b0c5d0873f66b2f Mon Sep 17 00:00:00 2001 From: Rootul Patel Date: Thu, 9 May 2024 13:52:38 -0400 Subject: [PATCH] implement TestAbility --- go/dnd-character/dnd_character.go | 34 +++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/go/dnd-character/dnd_character.go b/go/dnd-character/dnd_character.go index 7a861481..43272f16 100644 --- a/go/dnd-character/dnd_character.go +++ b/go/dnd-character/dnd_character.go @@ -1,6 +1,9 @@ package dndcharacter -import "math" +import ( + "math" + "math/rand" +) type Character struct { Strength int @@ -19,7 +22,34 @@ func Modifier(score int) int { // Ability uses randomness to generate the score for an ability func Ability() int { - panic("Please implement the Ability() function") + a := rollDice() + b := rollDice() + c := rollDice() + d := rollDice() + + return getSum(a, b, c, d) - getMin(a, b, c, d) +} + +// rollDice returns a random number between 1 and 6. +func rollDice() int { + return rand.Intn(5) + 1 +} + +func getSum(slice ...int) (sum int) { + for _, x := range slice { + sum += x + } + return sum +} + +func getMin(slice ...int) (minimum int) { + minimum = slice[0] + for _, x := range slice { + if x < minimum { + minimum = x + } + } + return minimum } // GenerateCharacter creates a new Character with random scores for abilities