Skip to content

Commit

Permalink
implement TestAbility
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp committed May 9, 2024
1 parent cb8554c commit 0d6bd4a
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions go/dnd-character/dnd_character.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dndcharacter

import "math"
import (
"math"
"math/rand"
)

type Character struct {
Strength int
Expand All @@ -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
Expand Down

0 comments on commit 0d6bd4a

Please sign in to comment.