Skip to content

Instantly share code, notes, and snippets.

@gilles-leblanc
Created June 21, 2014 01:46
Rust: enums 2
use std::rand::Rng;
use std::rand;
enum CoordinateFormula {
FirstIteration,
SecondIteration,
SubsequentIteration,
}
impl CoordinateFormula {
fn new() -> CoordinateFormula {
FirstIteration
}
fn change_iteration(&mut self) -> CoordinateFormula {
match *self {
FirstIteration => SecondIteration,
SecondIteration => SubsequentIteration,
SubsequentIteration => SubsequentIteration,
}
}
fn calculate_coordinates(&mut self, map_size: int) -> (int, int) {
let mut rng = rand::task_rng();
match *self {
FirstIteration => (map_size / 2, map_size / 2),
SecondIteration => (rng.gen_range(4, map_size - 4),
rng.gen_range(4, map_size - 4)),
SubsequentIteration => (rng.gen_range(0, map_size / 2) + map_size / 4,
rng.gen_range(0, map_size / 2) + map_size / 4),
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment