Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. extern crate rand;
  2.  
  3. use std::cmp;
  4. use std::collections::HashMap;
  5. use rand::distributions::{IndependentSample, Range};
  6.  
  7. #[derive(Debug)]
  8. struct Boss {
  9. hp: i32,
  10. damage: i32
  11. }
  12. #[derive(Debug)]
  13. struct Buff<'a> {
  14. spell: &'a Spell,
  15. duration: i32
  16. }
  17.  
  18. impl<'a> Buff<'a> {
  19. fn new(spell: &'a Spell, duration: i32) -> Buff {
  20. Buff{ spell: spell, duration: duration }
  21. }
  22. }
  23.  
  24. #[derive(Debug)]
  25. struct Player {
  26. damage: i32,
  27. armor: i32,
  28. hp: i32,
  29. mana: i32
  30. }
  31.  
  32. impl Player {
  33. fn new() -> Player {
  34. Player{ damage: 0, armor: 0, hp: 50, mana: 500 }
  35. }
  36. }
  37.  
  38. #[derive(Debug)]
  39. enum Target {
  40. PLAYER, BOSS
  41. }
  42.  
  43. #[derive(Debug)]
  44. struct Spell {
  45. name: String,
  46. duration: i32,
  47. mana_cost: i32,
  48. spell_effects: Vec<SpellEffect>
  49. }
  50.  
  51. impl Spell {
  52. fn new(name: &str, mana_cost: i32, duration: i32) -> Spell {
  53. Spell{ name: String::from(name), duration: duration, mana_cost: mana_cost, spell_effects: Vec::<SpellEffect>::new() }
  54. }
  55.  
  56. fn add_spell_effect(self: &mut Spell, spell_effect: SpellEffect) {
  57. self.spell_effects.push(spell_effect);
  58. }
  59. }
  60.  
  61. #[derive(Debug)]
  62. struct SpellEffect {
  63. target: Target,
  64. health: i32,
  65. armor: i32,
  66. mana: i32
  67. }
  68.  
  69. impl SpellEffect {
  70. fn new(target: Target, health: i32) -> SpellEffect {
  71. SpellEffect{ target: target, health: health, armor: 0, mana: 0 }
  72. }
  73.  
  74. fn new_buff(target: Target, health: i32, armor: i32, mana: i32) -> SpellEffect {
  75. SpellEffect{ target: target, health: health, armor: armor, mana: mana }
  76. }
  77. }
  78.  
  79. fn apply_spell_effect(player: &mut Player, boss: &mut Boss, spell_effect: &SpellEffect) {
  80. match spell_effect.target {
  81. Target::PLAYER => {
  82. player.hp += spell_effect.health;
  83. player.armor = spell_effect.armor;
  84. player.mana += spell_effect.mana;
  85. },
  86. Target::BOSS => {
  87. boss.hp += spell_effect.health;
  88. }
  89. }
  90. }
  91.  
  92. fn apply_buffs<'a>(player: &mut Player, boss: &mut Boss, buffs: &mut Vec<Buff<'a>>) {
  93. player.armor = 0;
  94. for buff in buffs.iter_mut() {
  95. buff.duration -= 1;
  96. if buff.duration > 0 || buff.spell.name != "shield" {
  97. for spell_effect in buff.spell.spell_effects.iter() {
  98. apply_spell_effect(player, boss, spell_effect);
  99. }
  100. }
  101. }
  102. buffs.retain(|buff| buff.duration > 0);
  103. }
  104.  
  105. fn buff_is_active(buffs: &Vec<Buff>, name: &String) -> bool {
  106. buffs.iter().filter(|buff| buff.spell.name == *name).collect::<Vec<_>>().len() > 0
  107. }
  108.  
  109. fn choose_spell<'a>(spells: &'a HashMap<String, Spell>, spell_keys: &Vec<String>, player: &Player, boss: &Boss, buffs: &Vec<Buff>) -> &'a Spell {
  110. if player.mana < 53 {
  111. // return any spell, as we are out of mana anyways
  112. spells.get("magic_missle").unwrap()
  113. } else {
  114. let mut rng = rand::thread_rng();
  115. let mut rand_index;
  116. let range = Range::new(0, spells.len());
  117. loop {
  118. rand_index = range.ind_sample(&mut rng);
  119. let spell = spells.get(&spell_keys[rand_index]).unwrap();
  120. if spell.mana_cost <= player.mana && !buff_is_active(buffs, &spell.name) {
  121. break;
  122. }
  123. }
  124.  
  125. spells.get(&spell_keys[rand_index]).unwrap()
  126. }
  127. }
  128.  
  129. fn reset(player: &mut Player, boss: &mut Boss, buffs: &mut Vec<Buff>) {
  130. player.hp = 50;
  131. player.mana = 500;
  132. boss.hp = 71;
  133. buffs.clear();
  134. }
  135.  
  136. fn main() {
  137. let mut boss = Boss{ hp: 71, damage: 10 };
  138. let mut player = Player::new();
  139. let mut min_hp = 10000;
  140.  
  141. let mut magic_missle = Spell::new("magic_missle", 53, 0);
  142. magic_missle.add_spell_effect(SpellEffect::new(Target::BOSS, -4));
  143.  
  144. let mut drain = Spell::new("drain", 73, 0);
  145. drain.add_spell_effect(SpellEffect::new(Target::BOSS, -2));
  146. drain.add_spell_effect(SpellEffect::new(Target::PLAYER, 2));
  147.  
  148. let mut shield = Spell::new("shield", 113, 6);
  149. shield.add_spell_effect(SpellEffect::new_buff(Target::PLAYER, 0, 7, 0));
  150.  
  151. let mut poison = Spell::new("poison", 173, 6);
  152. poison.add_spell_effect(SpellEffect::new_buff(Target::BOSS, -3, 0, 0));
  153.  
  154. let mut recharge = Spell::new("recharge", 229, 5);
  155. recharge.add_spell_effect(SpellEffect::new_buff(Target::PLAYER, 0, 0, 101));
  156.  
  157. let mut spells = HashMap::<String, Spell>::new();
  158. let spell_keys = vec![
  159. magic_missle.name.clone(), drain.name.clone(), shield.name.clone(), poison.name.clone(), recharge.name.clone()
  160. ];
  161. spells.insert(magic_missle.name.clone(), magic_missle);
  162. spells.insert(drain.name.clone(), drain);
  163. spells.insert(shield.name.clone(), shield);
  164. spells.insert(poison.name.clone(), poison);
  165. spells.insert(recharge.name.clone(), recharge);
  166. let mut buffs = Vec::<Buff>::new();
  167. let mut mana = 0;
  168. loop {
  169. if player.hp > 0 {
  170. // start player turn
  171. apply_buffs(&mut player, &mut boss, &mut buffs);
  172. let spell = choose_spell(&spells, &spell_keys, &player, &boss, &buffs);
  173. if spell.duration > 0 {
  174. buffs.push(Buff::new(spell, spell.duration));
  175. } else {
  176. for spell_effect in spell.spell_effects.iter() {
  177. apply_spell_effect(&mut player, &mut boss, spell_effect);
  178. }
  179. }
  180. player.mana -= spell.mana_cost;
  181. mana += spell.mana_cost;
  182. if player.mana < 0 {
  183. reset(&mut player, &mut boss, &mut buffs);
  184. mana = 0;
  185. continue
  186. }
  187. // start boss turn
  188. apply_buffs(&mut player, &mut boss, &mut buffs);
  189. if boss.hp > 0 {
  190. let mut damage = boss.damage - player.armor;
  191. if damage < 1 {
  192. damage = 1;
  193. }
  194. player.hp -= damage;
  195. } else {
  196. println!("Mana total: {:?}", mana);
  197. println!("{:?} {:?}", player.hp, boss.hp);
  198. break;
  199. }
  200. } else {
  201. let t = cmp::min(min_hp, boss.hp);
  202. if t < min_hp {
  203. min_hp = t;
  204. println!("{:?}", min_hp);
  205. }
  206. reset(&mut player, &mut boss, &mut buffs);
  207. mana = 0;
  208. continue
  209. }
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement