From a1d7194b7e17669c9485d9064605fffd99aea996 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Fri, 19 Jan 2024 23:52:27 -0800 Subject: Remove the Fruit struct. It really wasn't pulling its own weight. --- src/main.rs | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index c98c0ca..13f4271 100644 --- a/src/main.rs +++ b/src/main.rs @@ -226,22 +226,11 @@ impl Snake { } } -#[derive(Debug)] -struct Fruit { - pos: Pos, -} - -impl Fruit { - fn print(&self) -> Result<(), Box> { - move_cursor(self.pos)?; - write!(stdout(), "@")?; - Ok(()) - } - - fn new_pos(&mut self) { - self.pos.row = rand(i32::from(Screen::ROWS) - 4) as i8 + 2; - self.pos.col = rand(i32::from(Screen::COLS) - 4) as i8 + 2; - } +fn new_pos() -> Pos { + pos( + rand(i32::from(Screen::ROWS) - 4) as i8 + 2, + rand(i32::from(Screen::COLS) - 4) as i8 + 2, + ) } fn snake() -> Result> { @@ -251,8 +240,7 @@ fn snake() -> Result> { direction: Dir::Right, growing: 0, }; - let mut fruit = Fruit { pos: pos(0, 0) }; - fruit.new_pos(); + let mut fruit = new_pos(); let mut score: i64 = 0; let mut real_score: i64 = 0; let mut delay = Duration::from_millis(200); @@ -261,7 +249,8 @@ fn snake() -> Result> { move_cursor(pos(0, 3))?; write!(stdout(), " Score: {} ", score)?; snake.print()?; - fruit.print()?; + move_cursor(fruit)?; + write!(stdout(), "@")?; move_cursor(pos(Screen::ROWS - 1, Screen::COLS - 1))?; stdout().flush()?; @@ -275,11 +264,11 @@ fn snake() -> Result> { }; snake.step(); - if snake.body[0] == fruit.pos { + if snake.body[0] == fruit { snake.growing += 6; loop { - fruit.new_pos(); - if !snake.body.contains(&fruit.pos) { + fruit = new_pos(); + if !snake.body.contains(&fruit) { break; } } -- cgit v1.3.1