Skip to content

Instantly share code, notes, and snippets.

@distransient
Last active August 29, 2015 14:13
Show Gist options
  • Save distransient/9b15b82bd622193fe230 to your computer and use it in GitHub Desktop.
Save distransient/9b15b82bd622193fe230 to your computer and use it in GitHub Desktop.
fun lil test game
use std::io;
use std::rand;
struct Player<'a> { name: &'a str, health: uint, heroism: uint }
struct Place { index: uint, danger: uint }
static MAX_HEALTH: uint = 100;
static MAX_HEROISM: uint = 100;
static PLACE_NAMES: [&'static str, ..5] = [
"Woods", "Cave", "Mountain", "Town", "Desert"];
static ENEMY_NAMES: [&'static str, ..5] = [
"Troll", "Goblin", "Golem", "Soldier", "Scorpion"];
static PLACE_DANGERS: [&'static str, ..5] = [
"Calm", "Restless", "Wild", "Dangerous", "Evil"];
fn main() {
// Loop to ensure name is input
let mut input_name: String;
loop {
print!("Enter hero name: ");
input_name = io::stdin().read_line().ok().expect("Err");
if input_name.trim().len() > 0 { break; } else {
println!("Please enter a name.");
};
};
// Struct used for player over whole game
let mut player = Player {
name: input_name.trim(), health: MAX_HEALTH, heroism: 0 };
loop { // Game loop
// Generate locations to choose from
let options = [gen_place(),gen_place(),gen_place()];
let mut input_num: uint = 0;
// Loop to ensure correct location selection
loop {
println!("{} arrives at a crossroad... go to", player.name);
// Print locations options like "num) the adjective noun"
for i in range(0,3) {
println!(" {}) The {} {}", i + 1,
PLACE_DANGERS[options[i].danger],
PLACE_NAMES[options[i].index]);
};
print!("Enter a number: ");
let input = io::stdin().read_line().ok().expect("Err");
input_num = input.trim().parse::<uint>().unwrap_or(0);
// Make sure input is correct
if input_num > 3 || input_num == 0 {
println!("Please enter a number between 1 and 3.");
} else { input_num -= 1; break; };
};
println!("");
// Roll die to see if player is victorious
if rand::random::<uint>() % (options[input_num].danger + 1) == 0 {
println!("{} defeats a {} in The {} {}.", player.name,
ENEMY_NAMES[options[input_num].index],
PLACE_DANGERS[options[input_num].danger],
PLACE_NAMES[options[input_num].index]);
println!("{} gains {} heroism.", player.name,
options[input_num].danger + 1);
player.heroism += options[input_num].danger + 1;
player.health += (options[input_num].danger + 1) * 2;
// Player has become MAX HERO. Epic Win.
if player.heroism > MAX_HEROISM { break; };
if player.health > MAX_HEALTH { player.health = MAX_HEALTH; };
} else {
// Player lost battle.
player.health -= ( PLACE_DANGERS.len() / (
player.heroism / MAX_HEROISM + 1 ) ) * (
options[input_num].danger + 1 );
// Integer could overflow when going under zero.
if player.health == 0 || player.health > MAX_HEALTH {
println!("{} has been defeated by a {} in The {} {}",
player.name, ENEMY_NAMES[options[input_num].index],
PLACE_DANGERS[options[input_num].danger],
PLACE_NAMES[options[input_num].index]);
break; // 0 hp = Game Over
} else {
// Lost the battle, didn't lose the war....
println!("{} retreats from The {} {}, losing a battle with a {}!",
player.name, PLACE_DANGERS[options[input_num].danger],
PLACE_NAMES[options[input_num].index],
ENEMY_NAMES[options[input_num].index]);
};
};
println!("{} {}\n", player.name, match player.health / 10 {
1 => "is in grave pain!",
2 => "is grievously wounded.",
3 => "is beaten and battered",
4 => "is feeling defeated.",
5 => "is bleeding heavily",
6 => "is bleeding lightly.",
7 => "is starting to hurt.",
8 => "is a little bruised up.",
9 => "is a little scratched up.",
10 => "is in perfect health.",
_ => "is in mortal danger!"
});
}; // End game loop
println!("Game over! Score: {}", player.heroism);
}
fn gen_place() -> Place {
Place {
index: rand::random::<uint>() % PLACE_NAMES.len(),
danger: rand::random::<uint>() % PLACE_DANGERS.len()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment