CleanCut / invaders

Open source terminal arcade game with audio - based off of the classic "Space Invaders"
201 stars 102 forks source link

Error when moving #12

Closed christianh814 closed 1 year ago

christianh814 commented 1 year ago

I get the following error when the invaders move "down"

thread 'main' panicked at 'index out of bounds: the len is 40 but the index is 40', src/invaders.rs:75:9
CleanCut commented 1 year ago

Is this from your own implementation (following along from the course), or from the code in this repository?

christianh814 commented 1 year ago

My own implementation following the course. Although I'm comparing it to my code and I can't seem to see where I went wrong

CleanCut commented 1 year ago

Your nested curly braces { } are matched up incorrectly in src/invaders.rs. There should be another closing brace between lines 44-45, and the closing brace at line 52 (53 after the earlier change) should be deleted. Then the code from line 45-52 should be unindented one level (4 spaces).

Here is what the change diff looks like:

diff --git a/src/invaders.rs b/src/invaders.rs
index 342707c..cf2b0f8 100644
--- a/src/invaders.rs
+++ b/src/invaders.rs
@@ -42,14 +42,15 @@ impl Invaders {
                                if min_x == 0 {
                                        self.direction = 1;
                                        downwards = true;
-                               } else {
-                                       let max_x = self.army.iter().map(|invader| invader.x).max().unwrap_or(0);
-                                       if max_x == NUM_COLS - 1 {
-                                               self.direction = -1;
-                                               downwards = true;
-                                       }
+                               }
+                       } else {
+                               let max_x = self.army.iter().map(|invader| invader.x).max().unwrap_or(0);
+                               if max_x == NUM_COLS - 1 {
+                                       self.direction = -1;
+                                       downwards = true;
                                }
                        }
+               
                        if downwards {
                                let new_duration = max(self.move_timer.duration.as_millis() - 250, 250);
                                self.move_timer = Timer::from_millis(new_duration as u64);
CleanCut commented 1 year ago

By the way, Rust generally uses spaces, not tabs. If you run cargo fmt, your entire project will be reformatted nicely.

christianh814 commented 1 year ago

Thank you for that, that did it. Also didn't know about the cargo fmt command. Thanks!

CleanCut commented 1 year ago

Ultimate Rust 2 goes over cargo fmt, cargo clippy, and a lot of other cool stuff.