Skip to content

Instantly share code, notes, and snippets.

@jaredonline
Created August 12, 2014 00:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredonline/35649fe7c60a4e933db3 to your computer and use it in GitHub Desktop.
Save jaredonline/35649fe7c60a4e933db3 to your computer and use it in GitHub Desktop.
use std::io;
use std::rand::Rng;
struct Monster {
strength: int,
life: int,
charisma: int,
weapon: int,
name: String
}
impl Monster {
fn new(s: int, l: int, c: int, w: int, n: String) -> Monster {
Monster { life: l, strength: s, charisma: c, weapon: w, name: n }
}
fn monkey() -> Monster {
Monster::new(35, 46, 91, 2, "Industrial Raver Monkey".to_str())
}
fn angel() -> Monster {
Monster::new(6, 540, 144, 50, "Dwarven Angel".to_str())
}
fn ombudsman() -> Monster {
Monster::new(6, 320, 144, 50, "Assistant Vice Tentacle And Ombudsman".to_str())
}
fn deer() -> Monster {
Monster::new(192, 655, 19, 109, "Teeth Deer".to_str())
}
fn cyclist() -> Monster {
Monster::new(560, 901, 422, 105, "Intrepid Decomposed Cyclist".to_str())
}
fn dragon() -> Monster {
Monster::new(451, 1340, 1020, 939, "Dragon".to_str())
}
fn hit(&mut self, dmg: int) {
let p_up = std::rand::task_rng().gen_range(0i, self.charisma);
let mut life = self.life;
if p_up % 9 == 7 {
life += p_up / 4;
println!("[{:s} magick powers up {:d}!]", self.name, p_up);
}
life -= dmg;
if life < 0 {
println!("[{:s} has died.]", self.name);
}
self.life = life;
}
}
impl Clone for Monster {
fn clone(&self) -> Monster {
Monster { life: self.life, strength: self.strength, charisma: self.charisma, weapon:
self.weapon, name: self.name.clone() }
}
}
struct Player {
strength: int,
life: int,
charisma: int,
bombs: int
}
impl Player {
fn new(s: int, l: int, c: int, b: int) -> Player {
Player { life: l, strength: s, charisma: c, bombs: b }
}
fn rabbit() -> Player {
Player::new(2, 10, 44, 3)
}
fn fight(&mut self, mon: &mut Monster, weapon: int) {
if self.life <= 0 {
println!("[You're too dead to fight!]");
return;
}
let hit = std::rand::task_rng().gen_range(0i, self.strength + weapon);
println!("[You hit {:s} with {:d} with points of damage!]", mon.name, hit);
mon.hit(hit);
if mon.life > 0 {
let hit = std::rand::task_rng().gen_range(0i, mon.strength + mon.weapon);
println!("[{:s} hit you with {:d} points of damage!]", mon.name, hit);
self.hit(hit);
}
}
fn lettuce(&mut self) {
let lettuce = std::rand::task_rng().gen_range(0i, self.charisma);
println!("[Healthy lettuce gives you {:d} life points!!]", lettuce);
self.life += lettuce
}
fn hit(&mut self, dmg: int) {
let p_up = std::rand::task_rng().gen_range(0i, self.charisma);
let mut life = self.life;
if p_up % 9 == 7 {
life += p_up / 4;
println!("[Your magick powers up {:d}!]", p_up);
}
life -= dmg;
if life < 0 {
println!("[You have died.]");
}
self.life = life;
}
}
struct Game {
monsters: Vec<Monster>,
player: Player,
monster: Option<Monster>,
player_weapon: int,
over: bool
}
impl Game {
fn new() -> Game {
let monsters: Vec<Monster> = vec![
Monster::dragon(),
Monster::cyclist(),
Monster::deer(),
Monster::ombudsman(),
Monster::angel(),
Monster::monkey()
];
let player = Player::rabbit();
Game { monsters: monsters, player: player, monster: None, player_weapon: -1, over: false }
}
fn introduce_foe(&mut self) {
let should_get_new = match self.monster {
Some(ref m) => m.life < 1,
None => true
};
if should_get_new {
let opt : Option<Monster> = self.monsters.pop();
self.monster = opt.clone();
let name = match opt {
Some(monster) => monster.name,
None => { self.over = true; return }
};
println!("[Get ready. {:s} has emerged.]", name)
}
}
fn process_input(&mut self) {
self.player_weapon = -1;
println!("How would you like to attack? Boomerang, Hero's Sword, Lettuce or Bombs?");
println!("^ for Boomerang, / for Sword, % for Lettuce, * for Bombs or h for an
explanation.");
let mut reader = io::stdin();
let input = reader.read_line().ok().expect("Failed to read line.");
let input_char: Option<String> = from_str(input.as_slice().trim());
let command = match input_char {
Some(string) => string,
None => fail!("fuck")
};
let sword_dmg = match self.monster.clone() {
Some(monster) => (4 + ((monster.life % 10) ^ 2)),
None => fail!("Game over!")
};
if command == "*".to_str() {
if self.player.bombs > 0 {
self.player_weapon = 86;
self.player.bombs -= 1;
} else {
println!("You're out of bombs!");
}
} else if command == "/".to_str() {
self.player_weapon = sword_dmg;
} else if command == "%".to_str() {
self.player.lettuce();
self.player_weapon = 0;
} else if command == "^".to_str() {
self.player_weapon = 13;
} else if command == "h".to_str() {
println!("Your weapons:
^ (Boomerang) has strength 13
/ (Hero's Sword) has variable strength based on your opponent ({:d})
% (Lettuce) Lettuce will build your strength and extra ruffage will fly in the face of your opponent!!
* (Bomb) You only have {:d} left!", 1i, self.player.bombs);
} else {
println!("That didn't make any sense!");
}
}
fn display_intro(&self) {
println!("A scalding SEETHING LAVA infiltrates the cacauphonous ENGORGED MINESHAFTS deep
within the ageless canopy of the DWEMTHY FOREST... chalky and nocturnal screams from the
belly of the RAVENOUS WILD STORKUPINE... who eats wet goslings RIGHT AFTER they've had a
few graham crackers and a midday nap... amidst starved hippos TECHNICALLY ORPHANED but
truthfully sheltered by umbrellas owned jointly by car dealership conglomerates... beneath
uncapped vials of mildly pulpy BLUE ELIXIR... which shall remain... heretofore...
UNDISTURBED... DWEMTHY!!!");
println!("You have six foes.");
println!("These are the living, breathing monstrosities of Dwemthy's Array. I don't know
how they got there. No one knows. Actually, I'm guessing the IntrepidDecomposedCyclist rode
his ten-speed. But the others: NO ONE knows.
If it's really important for you to know, let's just say the others were born there. Can we
move on??
As Dwemthy's Array gets deeper, the challenge becomes more difficult.");
println!("Fight the Array and the monsters will appear as you go. Godspeed and may you
return with harrowing tales and nary an angel talon piercing through your shoulder.
Oh, and none of this \"I'm too young to die\" business. I'm sick of that crap. I'm not going
to have you insulting our undead young people. They are our future. After our future is
over, that is.");
println!("\nPrepare for battle!\n\n=================\n\n")
}
fn update_game(&mut self) {
let weapon = self.player_weapon;
{
let monster = match self.monster {
Some(ref mut monster) => monster,
None => { self.over = true; return }
};
self.player.fight(monster, weapon);
}
if self.player.life < 1i {
self.revive_rabbit();
}
}
fn revive_rabbit(&mut self) {
println!("You have fallen in battle, but another foolish young rabbit rushes to take your
place!");
self.player = Player::rabbit();
}
fn render(&self) {
println!("You have {:d} health!!", self.player.life);
}
}
fn main() {
let mut game = Game::new();
game.display_intro();
game.introduce_foe();
loop {
&mut game.process_input();
if game.player_weapon != -1 {
&mut game.update_game();
&mut game.introduce_foe();
game.render();
}
if game.over { break; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment