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.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. rand.Seed(time.Now().UnixNano()) // Keep this the first line in main()
  54. barb := Class{"Barbarian", 12, []string{"Int", "Wis", "Cha", "Dex", "Con", "Str"}}
  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.Printf("%s - %s\n", ply.Name, ply.Class.Name)
  64. fmt.Printf("Str: %d\tInt: %d\nDex: %d\tWis: %d\nCon: %d\tCha: %d\n",
  65. ply.Stats["Str"],
  66. ply.Stats["Int"],
  67. ply.Stats["Dex"],
  68. ply.Stats["Wis"],
  69. ply.Stats["Con"],
  70. ply.Stats["Cha"])
  71. }