Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. use rand::Rng;
  2. use rand::prelude::{thread_rng, ThreadRng};
  3. use rand::distributions::Uniform;
  4.  
  5. fn main() {
  6. let mut pc = Token::new("BigG0ku", "Fighter");
  7. pc.attributes.attribute_gen();
  8. pc.wearing = Armor::new("Leather Tunic", 14, "An old tunic, made of patches of mismatched leather.");
  9. pc.wielding = Weapon::new("Halberd", 12, "A rusty halberd. It's seen many battles, and taken many more lives.");
  10. pc.read_stats();
  11.  
  12. let mut creature = Token::new("LittleGoh4n", "Monk");
  13. creature.attributes.attribute_gen();
  14. creature.wearing = Armor::new("Old Robes", 12, "Dusty brown robes, that give the impression of having once been white.");
  15. creature.wielding = Weapon::new("Shortsword", 6, "A shortsword, sharpened once again into a battle-ready state.");
  16. creature.read_stats();
  17.  
  18. for i in 0..5 {
  19. println!("{} swings his {}, dealing {} damage!", pc.name, pc.wielding.name, pc.wielding.damage());
  20. }
  21.  
  22. println!("\n---> Exitting program <---");
  23. }
  24.  
  25. struct Dice {
  26. amount: i32,
  27. #[allow(dead_code)]
  28. size: i32,
  29. range: Uniform<i32>,
  30. rng: ThreadRng,
  31. }
  32.  
  33. impl Dice {
  34. /*
  35. roll() - returns i32 from total summation of a group of dice
  36. roll_poll() - returns Vec<i32> from a group of dice, retaining individual value.
  37. */
  38. pub fn new(amount: i32, size: i32) -> Self {
  39. Self {
  40. amount,
  41. size,
  42. range: Uniform::new(1, size),
  43. rng: thread_rng(),
  44. }
  45. }
  46.  
  47. fn roll(self) -> i32 {
  48. // Roll dice and then sum the die results together, returning one integer.
  49. let amount = self.amount;
  50. let range = self.range;
  51. let mut rng = self.rng;
  52.  
  53. let throw_dice: Vec<i32> = (0..amount).map(|_| rng.sample(range)).collect();
  54. let result: i32 = throw_dice.iter().fold(0, |acc, elem| acc + *elem);
  55.  
  56. // Output.
  57. return result;
  58. }
  59.  
  60. fn roll_pool(self) -> Vec<i32> {
  61. // Roll dice as a "pool," ie a collection of individual dice. Return this as a vector.
  62. let amount: i32 = self.amount;
  63. let range = self.range;
  64. let mut rng = self.rng;
  65.  
  66. let throw_dice: Vec<i32> = (0..amount).map(|_| rng.sample(range)).collect();
  67.  
  68. // Output.
  69. return throw_dice;
  70. }
  71. }
  72.  
  73. // The data that makes each Token numerically unique.
  74. struct TokenAttributes {
  75. strength: i32,
  76. dex: i32,
  77. wis: i32,
  78. hp: i32,
  79. }
  80.  
  81. // Extends TokenAttributes to have the capability of generating and returning values for stats.
  82. impl TokenAttributes {
  83. fn new(strength: i32, dex: i32, wis: i32, hp: i32) -> Self {
  84. Self {
  85. strength,
  86. dex,
  87. wis,
  88. hp,
  89. }
  90. }
  91.  
  92. fn attribute_gen(&mut self) -> i32 {
  93. let _attribute_array: Vec<i32> = vec![0, 0, 0]
  94. .into_iter()
  95. .map(|_| Dice::new(3, 6).roll().clone())
  96. .collect();
  97. self.strength = _attribute_array[0];
  98. self.dex = _attribute_array[1];
  99. self.wis = _attribute_array[2];
  100. self.hp = 10 + self.strength;
  101.  
  102. // This is a hack, and isn't the proper way to do this, I think.
  103. return 1;
  104. }
  105. }
  106.  
  107. // The core of this structure. Everything here will be implemented into Token in some format.
  108. // To be used for characters, as token is the generic term for any piece on a gameboard.
  109. struct Token {
  110. name: &'static str,
  111. class: &'static str,
  112. dead: bool,
  113. attributes: TokenAttributes,
  114. wearing: Armor,
  115. wielding: Weapon,
  116. }
  117.  
  118. // Traits for Token.
  119. trait TokenFeatures {
  120. // Static constructor, hence 'Self'.
  121. fn new(name: &'static str, class: &'static str) -> Self;
  122.  
  123. // Return strings.
  124. fn name(&self) -> &'static str;
  125. fn class(&self) -> &'static str;
  126. }
  127.  
  128.  
  129. impl Token {
  130. // Unique functions to the struct, not generic-as-from-Trait.
  131. fn is_dead(&self) -> bool {
  132. self.dead
  133. }
  134.  
  135. fn health(&mut self) {
  136. if self.is_dead() {
  137. // Implementor methods can use implementor trait methods.
  138. println!("{} is already dead...", self.name());
  139. } else {
  140. println!("{} is alive and well!", self.name);
  141. }
  142. }
  143.  
  144. fn assign_shout(&self) -> &'static str {
  145. if self.is_dead() {
  146. "'I'm... doomed...'"
  147. } else {
  148. "'Hrrrgh, die, monster!!'"
  149. }
  150. }
  151.  
  152. // Default methods can be overridden.
  153. fn battle_shout(&self) {
  154. println!("{} shouts out a phrase: {}", self.name, self.assign_shout());
  155. }
  156.  
  157. fn read_stats(&self) {
  158. println!("# {} the {} \n\
  159. ############ Attributes ###########\n\
  160. # Health Points: {} \n\
  161. # Armor Class: {} \n\
  162. # Strength: {} \n\
  163. # Dexterity: {} \n\
  164. # Wisdom: {} \n\
  165. ############ Equipment ############\n\
  166. # Wearing: {} \n\
  167. # Wielding: {} \n\
  168. ###################################\n", self.name, self.class,
  169. self.attributes.hp, self.wearing.value, self.attributes.strength, self.attributes.dex, self.attributes.wis,
  170. self.wearing.description, self.wielding.description)
  171. }
  172. }
  173.  
  174. impl TokenFeatures for Token {
  175. // Self is the implementor type: 'Token'.
  176. fn new(name: &'static str, class: &'static str) -> Self {
  177. Self {
  178. name,
  179. class,
  180. dead: false,
  181. attributes: TokenAttributes::new(8, 8, 8, 8),
  182. wearing: Armor::new("Plain Clothes", 10, "nothing"),
  183. wielding: Weapon::new("Bare Hands", 1, "unarmed"),
  184. }
  185. }
  186.  
  187. fn name(&self) -> &'static str {
  188. self.name
  189. }
  190.  
  191. fn class(&self) -> &'static str {
  192. self.class
  193. }
  194. }
  195.  
  196. // TODO Implement boolean states for broken/unbroken.
  197. struct Armor {
  198. name: &'static str,
  199. value: i32,
  200. description: &'static str,
  201. broken: bool,
  202. }
  203.  
  204. struct Weapon {
  205. name: &'static str,
  206. value: i32,
  207. description: &'static str,
  208. broken: bool,
  209. }
  210.  
  211. trait EquipmentFeatures {
  212. fn new(name: &'static str, value: i32, description: &'static str) -> Self;
  213.  
  214. fn name(&self) -> &'static str;
  215. fn description(&self) -> &'static str;
  216. }
  217.  
  218. impl EquipmentFeatures for Armor {
  219. fn new(name: &'static str, value: i32, description: &'static str) -> Self {
  220. Self {
  221. name,
  222. value,
  223. description,
  224. broken: false,
  225.  
  226. }
  227. }
  228.  
  229. fn name(&self) -> &'static str {
  230. self.name
  231. }
  232.  
  233. fn description(&self) -> &'static str {
  234. self.description
  235. }
  236. }
  237.  
  238. impl EquipmentFeatures for Weapon {
  239. fn new(name: &'static str, value: i32, description: &'static str) -> Self {
  240. Self {
  241. name,
  242. value,
  243. description,
  244. broken: false,
  245. }
  246. }
  247.  
  248. fn name(&self) -> &'static str {
  249. self.name
  250. }
  251. fn description(&self) -> &'static str {
  252. self.description
  253. }
  254. }
  255.  
  256. impl Weapon {
  257. fn is_broken(&self) -> bool {
  258. self.broken
  259. }
  260.  
  261. fn damage(&self) -> i32 {
  262. if self.is_broken() {
  263. 1
  264. } else {
  265. Dice::new(1, self.value).roll()
  266. }
  267. }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement