Browse Source

Factor out common utility functions into a package

master
Noëlle Anthony 5 years ago
parent
commit
bbce7567f1
4 changed files with 63 additions and 66 deletions
  1. 5
    20
      ddchar.go
  2. 3
    46
      main.go
  3. 34
    0
      src/util/namegen.go
  4. 21
    0
      src/util/rolls.go

+ 5
- 20
ddchar.go View File

"math/rand" "math/rand"
"sort" "sort"
"time" "time"
"util"
) )


type Class struct { type Class struct {
Stats map[string]int Stats map[string]int
} }


func roll(x, y int) []int {
rolls := make([]int, x)
for i := 0; i < x; i++ {
rolls[i] = (rand.Intn(y) + 1)
}
return rolls
}

func sum(ary []int) int {
s := 0
for _, v := range ary {
s += v
}
return s
}

func stats() []int { func stats() []int {
rolls := make([]int, 6) rolls := make([]int, 6)
for i := 0; i < 6; i++ { for i := 0; i < 6; i++ {
thisRoll := roll(4, 6)
thisRoll := util.Roll(4, 6)
for k, v := range thisRoll { for k, v := range thisRoll {
// fmt.Println(thisRoll) // fmt.Println(thisRoll)
for v == 1 { for v == 1 {
// fmt.Println("Replacing 1") // fmt.Println("Replacing 1")
newv := roll(1, 6)
newv := util.Roll(1, 6)
thisRoll[k] = newv[0] thisRoll[k] = newv[0]
v = newv[0] v = newv[0]
} }
sort.Ints(thisRoll) sort.Ints(thisRoll)
top3 := make([]int, 3) top3 := make([]int, 3)
copy(top3, thisRoll[1:]) copy(top3, thisRoll[1:])
rolls[i] = sum(top3)
rolls[i] = util.Sum(top3)
} }
return rolls return rolls
} }
rolls := stats() rolls := stats()
fmt.Println("Rolls: ", rolls) fmt.Println("Rolls: ", rolls)
sort.Ints(rolls) sort.Ints(rolls)
ply := Player{"Bob", barb, make(map[string]int)}
ply := Player{util.Namegen(), barb, make(map[string]int)}
for k, v := range barb.StatPriority { for k, v := range barb.StatPriority {
ply.Stats[v] = rolls[k] ply.Stats[v] = rolls[k]
} }

+ 3
- 46
main.go View File

"fmt" "fmt"
"math/rand" "math/rand"
// "sort" // "sort"
"strings"
// "strings"
"time" "time"
"util"
) )


type Character struct { type Character struct {
Roles map[string][]int Roles map[string][]int
} }


func roll(x, y int) []int {
rolls := make([]int, x)
for i := 0; i < x; i++ {
rolls[i] = (rand.Intn(y) + 1)
}
return rolls
}

func sum(ary []int) int {
s := 0
for _, v := range ary {
s += v
}
return s
}

func namegen() string {
vowels := make([]string, 6)
copy(vowels, strings.Split("aeiouy", ""))
consonants := make([]string, 20)
copy(consonants, strings.Split("bcdfghjklmnpqrstvwxz", ""))

ln := rand.Intn(5) + 5
vorc := rand.Intn(2)
i := 0
nm := make([]string, ln)
for i < ln {
sel, c := 0, ""
if i%2 == vorc {
sel = rand.Intn(6)
c = vowels[sel]
} else {
sel = rand.Intn(20)
c = consonants[sel]
}
if i == 0 {
c = strings.ToUpper(c)
}
nm = append(nm, c)
i++
}
return strings.Join(nm, "")
}

func main() { func main() {
rand.Seed(time.Now().UnixNano()) // Keep this first in main() rand.Seed(time.Now().UnixNano()) // Keep this first in main()


ply := Character{} ply := Character{}
ply.Name = namegen()
ply.Name = util.Namegen()
ply.TotalForces = 9 ply.TotalForces = 9
ply.CorporealForces = 1 ply.CorporealForces = 1
ply.EtherealForces = 1 ply.EtherealForces = 1

+ 34
- 0
src/util/namegen.go View File

package util

import (
"math/rand"
"strings"
)

func Namegen() string {
vowels := make([]string, 6)
copy(vowels, strings.Split("aeiouy", ""))
consonants := make([]string, 20)
copy(consonants, strings.Split("bcdfghjklmnpqrstvwxz", ""))

ln := rand.Intn(5) + 5
vorc := rand.Intn(2)
i := 0
nm := make([]string, ln)
for i < ln {
sel, c := 0, ""
if i%2 == vorc {
sel = rand.Intn(6)
c = vowels[sel]
} else {
sel = rand.Intn(20)
c = consonants[sel]
}
if i == 0 {
c = strings.ToUpper(c)
}
nm = append(nm, c)
i++
}
return strings.Join(nm, "")
}

+ 21
- 0
src/util/rolls.go View File

package util

import (
"math/rand"
)

func Roll(x, y int) []int {
rolls := make([]int, x)
for i := 0; i < x; i++ {
rolls[i] = (rand.Intn(y) + 1)
}
return rolls
}

func Sum(ary []int) int {
s := 0
for _, v := range ary {
s += v
}
return s
}

Loading…
Cancel
Save