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

Example: hangman game #231

Merged
merged 11 commits into from
Mar 31, 2021
Merged

Example: hangman game #231

merged 11 commits into from
Mar 31, 2021

Conversation

enaut
Copy link
Contributor

@enaut enaut commented Feb 25, 2021

working:

  • drawing the hangman
  • drawing the lines for a constant password
  • writing letters onto the lines
  • getting input (probably possible by listening to keyboard events?)
  • draw hangman step on error
  • draw letter on correct guess
  • loose on the last hangman step
  • win on all letters guessed right
  • make the secret a random word

I think I have a rough idea about how to solve the non bold ones... the bold ones I could only do by "stealing" from the other pull request (#226).

references: #227

things would be much nicer if #22 and #60 would be implemented

working so far: drawing the hangman, drawing the lines for a constant password.
@codecov

This comment has been minimized.

Copy link
Owner

@sunjay sunjay left a comment

Choose a reason for hiding this comment

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

Thanks for taking the time to put this together @enaut! Looking forward to reviewing in more detail once you're ready. Given that #22 and #60 won't be ready any time soon, let's stick to using the terminal for text input and output for now. I don't think #226 should be used outside of that example.

The player can have a terminal window side by side with the turtle window while they guess. I think it's still a great example even with that limitation. Very cool that you've used multiple turtles to divide up the drawing! 😁

Regarding your list:

  • writing letters onto the lines

Let's print the lines into the terminal for now. We can still draw them on the screen too. Maybe we can draw an X or some other symbol to indicate letters they've gotten right (while also printing something useful in the terminal).

  • getting input (probably possible by listening to keyboard events?)

Let's prompt for input in the terminal by printing something like "Enter your guess:" and then doing something with the letter they pick. You can also potentially use keyboard events for this. I'll leave it up to your creativity. :)

  • draw hangman step on error

I see you've already got a list of every step, so you should be able to go through them and draw the next one every time there is an error.

  • draw letter on correct guess

As I said above, let's try drawing some other symbol for correct letters and use the terminal for printing the text.

  • loose on the last hangman step

Maybe we can draw a big sad face across the turtle window when this happens or something.

  • win on all letters guessed right

Idea: Big smiley face or having the turtle run around the screen? Again, I'll leave that up to you.

  • make the secret a random word

Since this is just an example for this crate, let's leave it as a hard-coded word in a variable like you currently have. There's no word list modules in Rust as there are in Python, so I think it's fine just to choose the simpler option for this. If this was a full hangman app (outside of this repo), I'd recommend using a crate that gives us similar word lists as there are in Python.

Let me know if you need any help! Thanks for working on this! 😁 🎉

examples/hangman.rs Outdated Show resolved Hide resolved
* green dots for correctly guessed letters
* input via terminal
* react to correctly or wrongly guessed letters
* react to win or loose
* add a small worlist to randomly choose from
@enaut
Copy link
Contributor Author

enaut commented Feb 26, 2021

Should all be done now... do these builds also build the examples? Asking because I'd like to stay compatible with everything...

@enaut enaut changed the title WIP: Example: hangman game Example: hangman game Feb 26, 2021
Copy link
Owner

@sunjay sunjay left a comment

Choose a reason for hiding this comment

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

Nice work @enaut! 🎉 I will review this in more detail over the next few days, but I've left a few cursory comments of things I noticed while quickly going through the code.

For my full review I'll check out your code locally and spend some time running the example to see what it's like. I'll also look closer at each line of code to see if there are any other things we can improve. Looking forward to playing hangman! 😁

Feel free to keep making changes/improvements, and let me know when you're ready to go again. Happy to answer any questions you may have. Thanks for putting in all the effort to get this working! :)

examples/hangman.rs Outdated Show resolved Hide resolved
examples/hangman.rs Outdated Show resolved Hide resolved
examples/hangman.rs Show resolved Hide resolved
examples/hangman.rs Outdated Show resolved Hide resolved
@enaut
Copy link
Contributor Author

enaut commented Mar 2, 2021

Sorry I could have sworn I tested it... but obviously i forgot to save or something... I did not manage to get the choose function to work without importing the trait... I made this clearer in the comment.

@sunjay
Copy link
Owner

sunjay commented Mar 9, 2021

I was a bit too busy this weekend so I haven't gotten to this yet. Will try to review soon. :)

I did not manage to get the choose function to work without importing the trait... I made this clearer in the comment.

To do this you need to import the choose function, not use the choose method. Run cargo doc --open to see the docs for the turtle::rand module. Unfortunately the version on docs.rs/turtle does not reflect the current API.

Hope that helps!

@enaut
Copy link
Contributor Author

enaut commented Mar 16, 2021

To do this you need to import the choose function, not use the choose method. Run cargo doc --open to see the docs for the turtle::rand module. Unfortunately the version on docs.rs/turtle does not reflect the current API.

This is what I did... however that would require &str to implement Random (at least the compiler says so...) which cannot be done because str is not fixed size.

the not working lines are (tried in different permuttations):

let secret: &str = turtle::rand::choose(WORDS).expect("Failed to choose a random word.");

The only way I managed to get this working is by creating a vec from the fixed size array and choose from that - I dislike that and find my original solution much cleaner.

let secret: &str = turtle::rand::choose(&WORDS.to_vec()).expect("Failed to choose a random word.");

@sunjay
Copy link
Owner

sunjay commented Mar 16, 2021

Ooo looks like there is a bug on the random slice implementations for fixed sized arrays that is forcing the type stored in the array to implement Random. I'll fix that shortly. In the meantime changing the type of WORDS to &[&str] instead of &[&str; 28] will allow you to use the function version of choose. The method version was essentially coercing to that type anyway. Sorry about that!

@sunjay
Copy link
Owner

sunjay commented Mar 16, 2021

I just merged in #236 with the fix so if you rebase you may not even need to change the type in order to use the function. Let me know if you run into any further issues. :)

@enaut
Copy link
Contributor Author

enaut commented Mar 17, 2021

New things:

  • should use the unbugged rand.choose function
  • reveals the hidden word on loosing

@sunjay
Copy link
Owner

sunjay commented Mar 23, 2021

Hey! Really sorry for not getting to your PR yet. Going through some things and will try to find time soon. I usually try to be really quick with these things, but it unfortunately didn't work out this time. I will get to this! :)

@enaut
Copy link
Contributor Author

enaut commented Mar 28, 2021

If you want to we could go through this on discord or similar... that might be faster than writing everything...

Copy link
Owner

@sunjay sunjay left a comment

Choose a reason for hiding this comment

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

Great work @enaut! I checked out the code locally and really enjoyed playing hangman. Sorry the review took so long! Appreciate you working with the current input/text rendering limitations of turtle and making this anyway.

Just need to update to the latest master branch and this should be good to go.

@sunjay
Copy link
Owner

sunjay commented Mar 31, 2021

Just merged in master. This PR can be merged as soon as the build passes.

@enaut
Copy link
Contributor Author

enaut commented Mar 31, 2021

Thanks!

@sunjay sunjay merged commit bb20f85 into sunjay:master Mar 31, 2021
@enaut enaut deleted the example--hangman branch March 31, 2021 15:50
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.

3 participants