瀏覽代碼

Make the D&D generator actually generate

master
Noëlle Anthony 5 年之前
父節點
當前提交
a1f87e0fd4
共有 3 個文件被更改,包括 96 次插入8 次删除
  1. 90
    5
      ddchar.go
  2. 1
    2
      src/util/arrays.go
  3. 5
    1
      src/util/namegen.go

+ 90
- 5
ddchar.go 查看文件

@@ -14,12 +14,34 @@ type Class struct {
StatPriority []string
}

type Race struct {
Name string
Str int
Dex int
Con int
Int int
Wis int
Cha int
Features []string
}

type Player struct {
Name string
Class Class
Class *Class
Race *Race
Stats map[string]int
}

func choiceClass(ary []Class) Class {
r := rand.Intn(len(ary))
return ary[r]
}

func choiceRace(ary []Race) Race {
r := rand.Intn(len(ary))
return ary[r]
}

func stats() []int {
rolls := make([]int, 6)
for i := 0; i < 6; i++ {
@@ -41,19 +63,82 @@ func stats() []int {
return rolls
}

func pickClass() *Class {
barbarian := Class{"Barbarian", 12, []string{"Int", "Wis", "Cha", "Dex", "Con", "Str"}}
bard := Class{"Bard", 8, []string{"Str", "Con", "Wis", "Int", "Dex", "Cha"}}
cleric := Class{"Cleric", 8, []string{"Dex", "Int", "Cha", "Str", "Con", "Wis"}}
druid := Class{"Druid", 8, []string{"Cha", "Str", "Dex", "Int", "Con", "Wis"}}
fighter_arch := Class{"Fighter", 10, []string{"Int", "Cha", "Wis", "Str", "Con", "Dex"}}
fighter_melee := Class{"Fighter", 10, []string{"Int", "Cha", "Wis", "Dex", "Con", "Str"}}
monk := Class{"Monk", 8, []string{"Cha", "Int", "Str", "Con", "Wis", "Dex"}}
paladin := Class{"Paladin", 10, []string{"Dex", "Int", "Wis", "Con", "Cha", "Str"}}
ranger := Class{"Ranger", 10, []string{"Cha", "Int", "Con", "Str", "Wis", "Dex"}}
rogue := Class{"Rogue", 8, []string{"Wis", "Str", "Con", "Int", "Cha", "Dex"}}
sorcerer := Class{"Sorcerer", 6, []string{"Str", "Dex", "Wis", "Int", "Con", "Cha"}}
warlock := Class{"Warlock", 8, []string{"Str", "Dex", "Wis", "Int", "Con", "Cha"}}
wizard := Class{"Wizard", 6, []string{"Str", "Dex", "Cha", "Con", "Wis", "Int"}}

classlist := []Class{barbarian, bard, cleric, druid, fighter_arch, fighter_melee, monk, paladin, ranger, rogue, sorcerer, warlock, wizard}
rclass := choiceClass(classlist)
return &rclass
}

func (hmn *Race) humanAttr() {
for i := 0; i < 2; i++ {
switch r := rand.Intn(6); r {
case 0:
hmn.Str++
case 1:
hmn.Dex++
case 2:
hmn.Con++
case 3:
hmn.Int++
case 4:
hmn.Wis++
case 5:
hmn.Cha++
}
}
}

func pickRace() *Race {
dragonborn := Race{"Dragonborn", 2, 0, 0, 0, 0, 1, []string{"Draconic Ancestry", "Breath Weapon", "Damage Resistance"}}
dwarf := Race{"Dwarf", 0, 0, 2, 0, 0, 0, []string{"Darkvision", "Dwarven Resilience", "Dwarven Combat Training", "Stonecunning"}}
elf := Race{"Elf", 0, 2, 0, 0, 0, 0, []string{"Darkvision", "Keen Senses", "Fey Ancestry", "Trance"}}
gnome := Race{"Gnome", 0, 0, 0, 2, 0, 0, []string{"Darkvision", "Gnome Cunning"}}
halfelf := Race{"Half-Elf", 0, 0, 0, 0, 0, 2, []string{"Darkvision", "Fey Ancestry", "Skill Versatility"}}
halfling := Race{"Halfling", 0, 2, 0, 0, 0, 0, []string{"Lucky", "Brave", "Halfling Nimbleness"}}
halforc := Race{"Half-Orc", 2, 0, 1, 0, 0, 0, []string{"Darkvision", "Menacing", "Relentless Endurance", "Savage Attacks"}}
human := Race{"Human", 1, 1, 1, 1, 1, 1, []string{"Extra Language"}}
tiefling := Race{"Tiefling", 0, 0, 0, 1, 0, 2, []string{"Darkvision", "Hellish Resistance", "Infernal Legacy"}}

racelist := []Race{dragonborn, dwarf, elf, gnome, halfelf, halfling, halforc, human, tiefling}
rrace := choiceRace(racelist)
if rrace.Name == "Half-Elf" {
rrace.humanAttr()
}
return &rrace
}

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

barb := Class{"Barbarian", 12, []string{"Int", "Wis", "Cha", "Dex", "Con", "Str"}}
fmt.Println("=== Generated D&D character ===")
rolls := stats()
fmt.Println("Rolls: ", rolls)
sort.Ints(rolls)
ply := Player{util.Namegen(), barb, make(map[string]int)}
for k, v := range barb.StatPriority {
ply := Player{util.Namegen(), pickClass(), pickRace(), make(map[string]int)}
for k, v := range ply.Class.StatPriority {
ply.Stats[v] = rolls[k]
}
fmt.Printf("%s - %s\n", ply.Name, ply.Class.Name)
ply.Stats["Str"] += ply.Race.Str
ply.Stats["Dex"] += ply.Race.Dex
ply.Stats["Con"] += ply.Race.Con
ply.Stats["Int"] += ply.Race.Int
ply.Stats["Wis"] += ply.Race.Wis
ply.Stats["Cha"] += ply.Race.Cha
fmt.Printf("%s - %s %s\n", ply.Name, ply.Race.Name, ply.Class.Name)
fmt.Printf("Str: %d\tInt: %d\nDex: %d\tWis: %d\nCon: %d\tCha: %d\n",
ply.Stats["Str"],
ply.Stats["Int"],

+ 1
- 2
src/util/arrays.go 查看文件

@@ -3,7 +3,6 @@ package util
import (
"fmt"
"math/rand"
"time"
)

func Debug(msg ...interface{}) {
@@ -22,7 +21,7 @@ func ChoiceStr(ary []string) string {
return ary[r]
}

func ChoiceMap(ary map[string]int) (string, int) {
func ChoiceSIMap(ary map[string]int) (string, int) {
l := len(ary)
keys := make([]string, 0, l)
for k := range ary {

+ 5
- 1
src/util/namegen.go 查看文件

@@ -6,12 +6,16 @@ import (
)

func Namegen() string {
return FriendlyName(5, 5)
}

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

ln := rand.Intn(5) + 5
ln := rand.Intn(x) + y
vorc := rand.Intn(2)
i := 0
nm := make([]string, ln)

Loading…
取消
儲存