Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save high score and provide a start level as an argument #4

Merged
merged 5 commits into from
May 21, 2023

Conversation

joe-herbert
Copy link
Contributor

Saves high score in file and provides message after game ends.
Allows user to provide an integer argument which specifies the start level in order to skip the easier early levels.
Update README to reflect changes

@PonasKovas
Copy link
Owner

Thank you for this PR! Always exciting to see something like this. Just a couple of comments before merging though.

src/main.rs Outdated
Comment on lines 71 to 77
for i in 1..(start_level + 3) {
initial_snake_parts.insert(((i - (i / terminal_width)) % terminal_width, i / terminal_width));
}

for i in 1..(start_level + 3) {
initial_ordered_snake_parts.push_back(((i - (i / terminal_width)) % terminal_width, i / terminal_width));
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need 2 loops here?

src/main.rs Outdated
snake: Snake {
direction: Direction::Right,
//if there is less than 5 cells before the snake will crash into itself then set the direction to down
direction: if start_level % terminal_width >= terminal_width - 5 { Direction::Down } else { Direction::Right },
Copy link
Owner

@PonasKovas PonasKovas May 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this right? I think this works for the first line, but as the snake loops around the map, this doesn't really help much at higher levels. Maybe a better technique would be to always start going down, since there's almost always much space downwards?

src/main.rs Outdated
input: input.read_async(),
ended: false,
score: 0,
score: u32::from(start_level),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

start_level as u32 would look cleaner here I think

src/main.rs Outdated
Comment on lines 303 to 307
//save preferably in $HOME/.snake but fallback to /etc/.snake
let mut home = match home::home_dir() {
Some(path) => path,
None => PathBuf::from(r"/etc/"),
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think /etc/ is a good fallback to $HOME. I'd rather it just wouldn't save the highscore if $HOME is not defined.

Copy link
Owner

@PonasKovas PonasKovas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than that, seems good. Again, thanks for contributing.

Combine two loops
Change start direction to always down
 - since we always start going down it doesn't make much sense to always
   generate the initial food to the right as this means the user has to
   turn around so the initial position is now always random
Cleaner type conversion
Don't save the high score if $HOME is not accessible
 - includes new game end message for this case
@joe-herbert
Copy link
Contributor Author

Commit 59f422c should fix these changes. Thanks for your comments, this PR was my first time using rust so some of the complexities of the language meant I overlooked some basic programming issues (like the two loops, that was just dumb).

I believe the start direction did work correctly on all levels, but I see what you mean about it being easier to start going down, so I've made that change. That also meant the initial food position didn't make much sense, so I made that always randomly generated.

Hope you're happy with these changes.

@PonasKovas PonasKovas merged commit 6621251 into PonasKovas:master May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants