Sha256: 830a7345060da51fedd83a83ece9ca3f76bd6674fc0fb2a236afc6956771dc15
Contents?: true
Size: 1.59 KB
Versions: 395
Compression:
Stored size: 1.59 KB
Contents
pub struct Queen { position: ChessPosition, } pub trait ChessPiece { fn position(&self) -> &ChessPosition; fn can_attack<T: ChessPiece>(&self, other: &T) -> bool; } impl ChessPiece for Queen { fn position(&self) -> &ChessPosition { &self.position } fn can_attack<T: ChessPiece>(&self, piece: &T) -> bool { self.position.horizontal_from(&piece.position()) || self.position.vertical_from(&piece.position()) || self.position.diagonal_from(&piece.position()) } } impl Queen { pub fn new(position: ChessPosition) -> Queen { Queen { position: position } } } pub struct ChessPosition { pub rank: i8, pub file: i8, } impl ChessPosition { pub fn new(rank: i8, file: i8) -> Result<ChessPosition, String> { let position = ChessPosition { rank: rank, file: file, }; if position.is_valid() { Ok(position) } else { Err(String::from("Invalid Position")) } } fn is_valid(&self) -> bool { self.rank >= 0 && self.rank <= 7 && self.file >= 0 && self.file <= 7 } fn horizontal_from(&self, other: &ChessPosition) -> bool { self.rank == other.rank } fn vertical_from(&self, other: &ChessPosition) -> bool { self.file == other.file } fn diagonal_from(&self, other: &ChessPosition) -> bool { self.sum() == other.sum() || self.difference() == other.difference() } fn sum(&self) -> i8 { self.rank + self.file } fn difference(&self) -> i8 { self.rank - self.file } }
Version data entries
395 entries across 395 versions & 1 rubygems