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.

main.rs 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. use rand::{ distributions:: {Distribution, Standard},
  2. Rng
  3. };
  4. #[derive(Debug)]
  5. enum Cls {
  6. Artificer,
  7. Barbarian,
  8. Bard,
  9. Cleric,
  10. Druid,
  11. Fighter,
  12. Monk,
  13. Paladin,
  14. Ranger,
  15. Rogue,
  16. Sorcerer,
  17. Warlock,
  18. Wizard
  19. }
  20. impl Default for Cls {
  21. fn default() -> Self { Cls::Fighter }
  22. }
  23. impl Distribution<Cls> for Standard {
  24. fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Cls {
  25. match rng.gen_range(0..=12) {
  26. 0 => Cls::Artificer,
  27. 1 => Cls::Barbarian,
  28. 2 => Cls::Bard,
  29. 3 => Cls::Cleric,
  30. 4 => Cls::Druid,
  31. 5 => Cls::Fighter,
  32. 6 => Cls::Monk,
  33. 7 => Cls::Paladin,
  34. 8 => Cls::Ranger,
  35. 9 => Cls::Rogue,
  36. 10 => Cls::Sorcerer,
  37. 11 => Cls::Warlock,
  38. 12 => Cls::Wizard,
  39. _ => Cls::Fighter
  40. }
  41. }
  42. }
  43. #[derive(Debug, Default)]
  44. struct Character {
  45. cls: Cls,
  46. strength: u32,
  47. dexterity: u32,
  48. constitution: u32,
  49. intelligence: u32,
  50. wisdom: u32,
  51. charisma: u32,
  52. }
  53. // impl Character {
  54. // }
  55. fn roll_stat() -> u32 {
  56. let mut rolls = Vec::new();
  57. while rolls.len()<4 {
  58. let roll = rand::thread_rng().gen_range(1..=6);
  59. if roll == 1 {
  60. continue;
  61. }
  62. rolls.push(roll);
  63. }
  64. println!("{:?}", rolls);
  65. rolls.sort_by(|a,b| b.cmp(a));
  66. println!("{:?}", rolls);
  67. rolls.pop();
  68. println!("{:?}", rolls);
  69. rolls.iter().sum()
  70. }
  71. fn main() {
  72. let mut chr: Character = Default::default();
  73. chr.cls = rand::random();
  74. chr.strength = roll_stat();
  75. chr.dexterity = roll_stat();
  76. chr.constitution = roll_stat();
  77. chr.intelligence = roll_stat();
  78. chr.wisdom = roll_stat();
  79. chr.charisma = roll_stat();
  80. println!("{:?}", chr);
  81. }