Pārlūkot izejas kodu

Factor out common utility functions into a package

master
Noëlle Anthony pirms 5 gadiem
vecāks
revīzija
bbce7567f1
4 mainītis faili ar 63 papildinājumiem un 66 dzēšanām
  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 Parādīt failu

@@ -5,6 +5,7 @@ import (
"math/rand"
"sort"
"time"
"util"
)

type Class struct {
@@ -19,31 +20,15 @@ type Player struct {
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 {
rolls := make([]int, 6)
for i := 0; i < 6; i++ {
thisRoll := roll(4, 6)
thisRoll := util.Roll(4, 6)
for k, v := range thisRoll {
// fmt.Println(thisRoll)
for v == 1 {
// fmt.Println("Replacing 1")
newv := roll(1, 6)
newv := util.Roll(1, 6)
thisRoll[k] = newv[0]
v = newv[0]
}
@@ -51,7 +36,7 @@ func stats() []int {
sort.Ints(thisRoll)
top3 := make([]int, 3)
copy(top3, thisRoll[1:])
rolls[i] = sum(top3)
rolls[i] = util.Sum(top3)
}
return rolls
}
@@ -64,7 +49,7 @@ func main() {
rolls := stats()
fmt.Println("Rolls: ", 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 {
ply.Stats[v] = rolls[k]
}

+ 3
- 46
main.go Parādīt failu

@@ -4,8 +4,9 @@ import (
"fmt"
"math/rand"
// "sort"
"strings"
// "strings"
"time"
"util"
)

type Character struct {
@@ -29,55 +30,11 @@ type Character struct {
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() {
rand.Seed(time.Now().UnixNano()) // Keep this first in main()

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

+ 34
- 0
src/util/namegen.go Parādīt failu

@@ -0,0 +1,34 @@
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 Parādīt failu

@@ -0,0 +1,21 @@
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
}

Notiek ielāde…
Atcelt
Saglabāt