-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[WIP]Implement exercise hangman #1160
Changes from 4 commits
93d8ddc
fd14531
8166d5a
8fd9f15
8639d32
241cde4
17c7fc9
d244773
09000e9
7e325cf
f6c67db
3462370
db1eafc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Need edit | ||
Implement the logic of the hangman game using functional reactive programming. | ||
|
||
Hangman is a simple word guessing game. | ||
|
||
Functional Reactive Programming is a way to write interactive programs. It differs from the usual perspective in that instead of saying "when the button is pressed increment the counter", you write "the value of the counter is the sum of the number of times the button is pressed." | ||
|
||
Implement the basic logic behind hangman using functional reactive programming. You'll need to install an FRP library for this, this will be described in the language/track specific files of the exercise. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this exercise isn't about functional reactive programming now, then references to it need to be removed. This needs a track-specific description, which should be put into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, do you have any suggestion on how the description should be? And the file description.md that you mention is in the problem specification right? |
||
## Exception messages | ||
|
||
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to | ||
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not | ||
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include | ||
a message. | ||
|
||
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of | ||
`raise Exception`, you shold write: | ||
|
||
```python | ||
raise Exception("Meaningful message indicating the source of the error") | ||
``` | ||
|
||
|
||
## Submitting Exercises | ||
|
||
Note that, when trying to submit an exercise, make sure the solution is in the `exercism/python/<exerciseName>` directory. | ||
|
||
For example, if you're submitting `bob.py` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/python/bob/bob.py`. | ||
|
||
For more detailed information about running tests, code style and linting, | ||
please see the [help page](http://exercism.io/languages/python). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be a section at the bottom about submitting incomplete solutions. It's best to generate these using the You can install it by running There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that you will have to clone exercism/problem-specifications to generate README files. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class Hangman: | ||
def __init__(self, word): | ||
self.remainingGuesses = 9 | ||
self.status = 'busy' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think A better approach to handling status would be to declare some constants at the start of the class ( I also think that we need a test to check that a game that has been lost refuses further interaction. Currently, your example would fail this test, since if I just guess every letter in the alphabet, the status would go to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do want me to stop the game after it is finished (either win or lose)? Should I raise an Exception or Error? or should i just use an if statement and stop it from updating everything in guess()? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @khoa102 Can you clarify what you mean by "stop the game"? Since this is not an interactive program, "stop" loses its meaning. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cmccandless N-Parson mentions earlier that "a game that has been lost refuses further interaction." And that is what I mean by stopping the game after it's finished (win or lose) |
||
self.word = word | ||
self.masked_word = '' | ||
self.guesses = [] | ||
for i in self.word: | ||
self.masked_word += '_' | ||
|
||
def guess(self, char): | ||
self.update_remaining_guesses(char) | ||
self.update_masked_word() | ||
self.update_status() | ||
|
||
def update_masked_word(self): | ||
self.masked_word = '' | ||
for i in self.word: | ||
if i not in self.guesses: | ||
self.masked_word += '_' | ||
else: | ||
self.masked_word += i | ||
|
||
def update_remaining_guesses(self, char): | ||
if char not in self.word or char in self.guesses: | ||
self.remainingGuesses -= 1 | ||
else: | ||
self.guesses.append(char) | ||
|
||
def update_status(self): | ||
if self.masked_word == self.word: | ||
self.status = 'win' | ||
elif self.remainingGuesses < 0: | ||
self.status = 'lose' | ||
else: | ||
self.status = 'busy' | ||
|
||
def get_masked_word(self): | ||
return self.masked_word | ||
|
||
def get_status(self): | ||
return self.status |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Hangman(object): | ||
def __init__(self, word): | ||
self.remainingGuesses = 9 | ||
self.status = 'busy' | ||
|
||
def guess(self, char): | ||
pass | ||
|
||
def get_masked_word(self): | ||
pass | ||
|
||
def get_status(self): | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import unittest | ||
|
||
from example import Hangman | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import from |
||
|
||
|
||
# Adapt from c-sharp track | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change to the following: Normally, we would have a comment here citing the version of the canonical data that these tests were based on. In this case, there is no canonical data for this exercise, so this is fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @khoa102 Please apply this fix. |
||
class HangmanTests(unittest.TestCase): | ||
def test_initially_9_failures_are_allowed(self): | ||
game = Hangman('foo') | ||
self.assertEqual(game.get_status(), 'busy') | ||
self.assertEqual(game.remainingGuesses, 9) | ||
|
||
def test_initially_no_letters_are_guessed(self): | ||
game = Hangman('foo') | ||
|
||
self.assertEqual(game.get_masked_word(), '___') | ||
|
||
def test_after_10_failures_the_game_is_over(self): | ||
game = Hangman('foo') | ||
|
||
for i in range(10): | ||
game.guess('x') | ||
|
||
self.assertEqual(game.get_status(), 'lose') | ||
|
||
def test_feeding_a_correct_letter_removes_underscores(self): | ||
game = Hangman('foobar') | ||
|
||
game.guess('b') | ||
self.assertEqual(game.get_status(), 'busy') | ||
self.assertEqual(game.remainingGuesses, 9) | ||
self.assertEqual(game.get_masked_word(), '___b__') | ||
|
||
game.guess('o') | ||
self.assertEqual(game.get_status(), 'busy') | ||
self.assertEqual(game.remainingGuesses, 9) | ||
self.assertEqual(game.get_masked_word(), '_oob__') | ||
|
||
def test_feeding_a_correct_letter_twice_counts_as_a_failure(self): | ||
game = Hangman('foobar') | ||
|
||
game.guess('b') | ||
self.assertEqual(game.get_status(), 'busy') | ||
self.assertEqual(game.remainingGuesses, 9) | ||
self.assertEqual(game.get_masked_word(), '___b__') | ||
|
||
game.guess('b') | ||
self.assertEqual(game.get_status(), 'busy') | ||
self.assertEqual(game.remainingGuesses, 8) | ||
self.assertEqual(game.get_masked_word(), '___b__') | ||
|
||
def test_getting_all_the_letters_right_makes_for_a_win(self): | ||
game = Hangman('hello') | ||
|
||
game.guess('b') | ||
self.assertEqual(game.get_status(), 'busy') | ||
self.assertEqual(game.get_status(), 'busy') | ||
self.assertEqual(game.remainingGuesses, 8) | ||
self.assertEqual(game.get_masked_word(), '_____') | ||
|
||
game.guess('e') | ||
self.assertEqual(game.get_status(), 'busy') | ||
self.assertEqual(game.remainingGuesses, 8) | ||
self.assertEqual(game.get_masked_word(), '_e___') | ||
|
||
game.guess('l') | ||
self.assertEqual(game.get_status(), 'busy') | ||
self.assertEqual(game.remainingGuesses, 8) | ||
self.assertEqual(game.get_masked_word(), '_ell_') | ||
|
||
game.guess('o') | ||
self.assertEqual(game.get_status(), 'busy') | ||
self.assertEqual(game.remainingGuesses, 8) | ||
self.assertEqual(game.get_masked_word(), '_ello') | ||
|
||
game.guess('h') | ||
self.assertEqual(game.get_status(), 'win') | ||
self.assertEqual(game.get_masked_word(), 'hello') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be the title of the exercise