You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ddchar.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "sort"
  6. "time"
  7. )
  8. type Class struct {
  9. Name string
  10. HitDie int
  11. StatPriority []string
  12. }
  13. type Player struct {
  14. Name string
  15. Class Class
  16. Stats map[string]int
  17. }
  18. func roll(x, y int) []int {
  19. rolls := make([]int, x)
  20. for i := 0; i < x; i++ {
  21. rolls[i] = (rand.Intn(y) + 1)
  22. }
  23. return rolls
  24. }
  25. func sum(ary []int) int {
  26. s := 0
  27. for _, v := range ary {
  28. s += v
  29. }
  30. return s
  31. }
  32. func stats() []int {
  33. rolls := make([]int, 6)
  34. for i := 0; i < 6; i++ {
  35. thisRoll := roll(4, 6)
  36. for k, v := range thisRoll {
  37. // fmt.Println(thisRoll)
  38. for v == 1 {
  39. // fmt.Println("Replacing 1")
  40. newv := roll(1, 6)
  41. thisRoll[k] = newv[0]
  42. v = newv[0]
  43. }
  44. }
  45. sort.Ints(thisRoll)
  46. top3 := make([]int, 3)
  47. copy(top3, thisRoll[1:])
  48. rolls[i] = sum(top3)
  49. }
  50. return rolls
  51. }
  52. func main() {
  53. barb := Class{"Barbarian", 12, []string{"Int", "Wis", "Cha", "Dex", "Con", "Str"}}
  54. rand.Seed(time.Now().UnixNano())
  55. fmt.Println("=== Generated D&D character ===")
  56. rolls := stats()
  57. fmt.Println("Rolls: ", rolls)
  58. sort.Ints(rolls)
  59. ply := Player{"Bob", barb, make(map[string]int)}
  60. for k, v := range barb.StatPriority {
  61. ply.Stats[v] = rolls[k]
  62. }
  63. fmt.Println(ply)
  64. }