Compare commits
4 Commits
037699f6be
...
8a524236cc
| Author | SHA1 | Date | |
|---|---|---|---|
| 8a524236cc | |||
| d8136c2ff4 | |||
| ce37b21d3d | |||
| 465d18ea67 |
@@ -10,6 +10,27 @@ pub struct Crossword {
|
|||||||
height: usize,
|
height: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct WordPuller<'a> {
|
||||||
|
x: usize,
|
||||||
|
y: usize,
|
||||||
|
direction: (isize, isize),
|
||||||
|
crossword: &'a Crossword,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> WordPuller<'a> {
|
||||||
|
pub fn take(&self, num_of_letters: usize) -> String {
|
||||||
|
let mut word = String::new();
|
||||||
|
|
||||||
|
for i in 0..num_of_letters {
|
||||||
|
let x = self.x as isize + (i as isize * self.direction.0);
|
||||||
|
let y = self.y as isize + (i as isize * self.direction.1);
|
||||||
|
word.push(self.crossword.data[x as usize][y as usize]);
|
||||||
|
}
|
||||||
|
|
||||||
|
word
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Crossword {
|
impl Crossword {
|
||||||
pub fn new(file: File) -> Result<Crossword, Box<dyn Error>> {
|
pub fn new(file: File) -> Result<Crossword, Box<dyn Error>> {
|
||||||
let mut input_buffer = BufReader::new(file);
|
let mut input_buffer = BufReader::new(file);
|
||||||
@@ -44,6 +65,71 @@ impl Crossword {
|
|||||||
pub fn height(&self) -> usize {
|
pub fn height(&self) -> usize {
|
||||||
self.height
|
self.height
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn n(&self, x: usize, y: usize) -> WordPuller {
|
||||||
|
WordPuller {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
direction: (-1, 0),
|
||||||
|
crossword: self,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn ne(&self, x: usize, y: usize) -> WordPuller {
|
||||||
|
WordPuller {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
direction: (-1, 1),
|
||||||
|
crossword: self,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn e(&self, x: usize, y: usize) -> WordPuller {
|
||||||
|
WordPuller {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
direction: (0, 1),
|
||||||
|
crossword: self,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn se(&self, x: usize, y: usize) -> WordPuller {
|
||||||
|
WordPuller {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
direction: (1, 1),
|
||||||
|
crossword: self,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn s(&self, x: usize, y: usize) -> WordPuller {
|
||||||
|
WordPuller {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
direction: (1, 0),
|
||||||
|
crossword: self,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn sw(&self, x: usize, y: usize) -> WordPuller {
|
||||||
|
WordPuller {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
direction: (1, -1),
|
||||||
|
crossword: self,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn w(&self, x: usize, y: usize) -> WordPuller {
|
||||||
|
WordPuller {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
direction: (0, -1),
|
||||||
|
crossword: self,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn nw(&self, x: usize, y: usize) -> WordPuller {
|
||||||
|
WordPuller {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
direction: (-1, -1),
|
||||||
|
crossword: self,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Crossword {
|
impl Display for Crossword {
|
||||||
|
|||||||
@@ -24,6 +24,14 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let crossword = Crossword::new(File::open(input_file_string)?)?;
|
let crossword = Crossword::new(File::open(input_file_string)?)?;
|
||||||
|
|
||||||
println!("{}", crossword);
|
println!("{}", crossword);
|
||||||
|
println!("n: {}", crossword.n(3, 3).take(4));
|
||||||
|
println!("ne: {}", crossword.ne(3, 3).take(4));
|
||||||
|
println!("e: {}", crossword.e(3, 3).take(4));
|
||||||
|
println!("se: {}", crossword.se(3, 3).take(4));
|
||||||
|
println!("s: {}", crossword.s(3, 3).take(4));
|
||||||
|
println!("sw: {}", crossword.sw(3, 3).take(4));
|
||||||
|
println!("w: {}", crossword.w(3, 3).take(4));
|
||||||
|
println!("nw: {}", crossword.nw(3, 3).take(4));
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user