SHARE
TWEET

Untitled

a guest Jan 20th, 2020 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use rand::prelude::*;
  2.  
  3. /*
  4. rust port of:
  5. https://gist.github.com/sagebind/8358734
  6. ^ was the first result of "simple c++ game"
  7. port by nullflex
  8. */
  9.  
  10. fn create_zombie() -> u8 {
  11.     let mut rng = thread_rng();
  12.  
  13.     let chance = rng.gen_range(0, 100);
  14.     if chance > 75 {
  15.         return 11;
  16.     }
  17.  
  18.     return rng.gen_range(1, 10);
  19. }
  20.  
  21. #[allow(non_camel_case_types)]
  22. struct player_info {
  23.     skill: u8,
  24.     score: u16,
  25.     name: String
  26. }
  27.  
  28. #[allow(non_camel_case_types)]
  29. struct zombie_info {
  30.     count: u16,
  31.     killed: u16
  32. }
  33.  
  34. fn main() {
  35.     use std::io::stdin;
  36.  
  37.     // game stats
  38.     let rand_player_skill: u8 = {
  39.         let mut rng = thread_rng();
  40.         rng.gen_range(2, 12)
  41.     };
  42.  
  43.     let mut player = player_info{skill: rand_player_skill, score: 1, name: String::from("")};
  44.     let mut zombie_stats = zombie_info{count: 0, killed: 0};
  45.  
  46.     let mut input_value: String = String::from("");
  47.     // title
  48.     println!("Welcome to Zombie War (rust port). \nPress [ENTER] to start.");
  49.     stdin().read_line(&mut input_value).expect("how did you break the first input prompt?");
  50.  
  51.     // player name
  52.     println!("Enter your name: ");
  53.     stdin().read_line(&mut player.name).expect("something invalid passed in name input");
  54.     player.name = player.name.trim_end().to_string();
  55.  
  56.     // ask how many zombies
  57.     println!("How many zombies do you want to fight? ");
  58.     {
  59.         let mut zombie_count_str: String = String::from("");
  60.         stdin().read_line(&mut zombie_count_str).expect("something invalid passed in zombies fight input");
  61.         zombie_count_str = zombie_count_str.trim_end().to_string();
  62.         zombie_stats.count = zombie_count_str.parse::<u16>().expect("invalid number entered in zombies fight");
  63.     }
  64.  
  65.     println!("Get ready to fight {}!", player.name);
  66.  
  67.     // main game loop
  68.     while zombie_stats.killed < zombie_stats.count {
  69.         // create a random zombie
  70.         let zombie_skill: u8 = create_zombie();
  71.  
  72.         // battle sequence
  73.         if zombie_skill > 10 {
  74.             println!("boss zombie alert!");
  75.         }
  76.         else {
  77.             println!("here comes zombie {}", zombie_stats.killed);
  78.         }
  79.  
  80.         println!("fighting...");
  81.         std::thread::sleep(std::time::Duration::from_secs(2));
  82.  
  83.         // zombie killed the player
  84.         if player.skill < zombie_skill {
  85.             println!("you have died.");
  86.             break;
  87.         }
  88.  
  89.         let skill_delta: i8 = (player.skill - zombie_skill) as i8;
  90.  
  91.         if skill_delta > 7 {
  92.             println!("you wasted the zombie");
  93.             player.score *= 2;
  94.         }
  95.         else if skill_delta > 5 {
  96.             println!("you decapitated the zombie");
  97.             player.score *= 2;
  98.         }
  99.         else if skill_delta > 0 {
  100.             println!("you killed the zombie");
  101.             player.score *= 2;
  102.         }
  103.         else {
  104.             println!("you killed the zombie, but suffered injuries");
  105.         }
  106.  
  107.         zombie_stats.killed += 1;
  108.         println!("hit enter to continue.");
  109.         stdin().read_line(&mut input_value).expect("how did you break an input that doesn't even require anything?");
  110.     }
  111.  
  112.     // game ended
  113.     if zombie_stats.killed == zombie_stats.count {
  114.         println!("you survived");
  115.     }
  116.     else {
  117.         println!("you did not survive");
  118.     }
  119.  
  120.     println!("zombies killed: {}, final score: {}", zombie_stats.killed, player.score);
  121. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top