Skip to content

Instantly share code, notes, and snippets.

@Taneb
Created June 5, 2016 13:39
Show Gist options
  • Save Taneb/d8f1ce7ea54ef41d2ac399194d044bbc to your computer and use it in GitHub Desktop.
Save Taneb/d8f1ce7ea54ef41d2ac399194d044bbc to your computer and use it in GitHub Desktop.
Proof of concept for Rust implementation of sirpent
trait Vector {
type Direction;
fn distance(&self, other : &Self) -> Self;
fn neighbour(&self, direction : Self::Direction) -> Self;
}
trait Grid {
type Vector : Vector;
fn dimensions(&self) -> Vec<isize>;
fn is_within_bounds(&self, v : Self::Vector) -> bool;
}
enum HexDir {
North,
NorthEast,
SouthEast,
South,
SouthWest,
NorthWest
}
struct HexVector {
x : isize,
y : isize
}
impl Vector for HexVector {
type Direction = HexDir;
fn distance(&self, other : &HexVector) -> HexVector {
HexVector{x : self.x - other.x, y : self.y - other.y}
}
fn neighbour(&self, direction : HexDir) -> HexVector {
match direction {
HexDir::North => HexVector {x : self.x , y : self.y - 1},
HexDir::NorthEast => HexVector {x : self.x + 1, y : self.y - 1},
HexDir::SouthEast => HexVector {x : self.x + 1, y : self.y },
HexDir::South => HexVector {x : self.x , y : self.y + 1},
HexDir::SouthWest => HexVector {x : self.x - 1, y : self.y + 1},
HexDir::NorthWest => HexVector {x : self.x - 1, y : self.y }
}
}
}
struct HexGrid {
width : isize,
height : isize
}
impl Grid for HexGrid {
type Vector = HexVector;
fn dimensions(&self) -> Vec<isize> {
vec![self.width, self.height]
}
fn is_within_bounds(&self, v : HexVector) -> bool {
v.x >= 0 && v.x < self.width && v.y >= 0 && v.y < self.height
}
}
fn main() {
println!("Hello, world!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment