-
Notifications
You must be signed in to change notification settings - Fork 9
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
Hangman assignment #1
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -1,23 +1,87 @@ | ||
from .exceptions import * | ||
import random | ||
|
||
# Complete with your own, just for fun :) | ||
LIST_OF_WORDS = [] | ||
LIST_OF_WORDS = ['cookies', 'cream', 'love', 'vinegar'] | ||
|
||
|
||
def _get_random_word(list_of_words): | ||
pass | ||
if list_of_words == []: | ||
raise InvalidListOfWordsException | ||
else: | ||
return random.choice(list_of_words) | ||
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. very clean code here ... random library is perfect for this... |
||
|
||
|
||
def _mask_word(word): | ||
pass | ||
if word == "": | ||
raise InvalidWordException | ||
else: | ||
return len(word)*"*" | ||
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. another piece of clean code! |
||
|
||
|
||
def find_substring(string, substring): | ||
""" | ||
Returns list of indices where substring begins in string | ||
>>> find_substring('r', "rmotr") | ||
[0, 4] | ||
""" | ||
indices = [] | ||
index = -1 # Begin at -1 so index + 1 is 0 | ||
while True: | ||
# Find next index of substring, by starting search from index + 1 | ||
index = string.find(substring, index + 1) | ||
if index == -1: | ||
break # All occurrences have been found | ||
indices.append(index) | ||
return indices | ||
|
||
def _uncover_word(answer_word, masked_word, character): | ||
pass | ||
if character == "" or len(character) > 1 : | ||
raise InvalidGuessedLetterException(Exception) | ||
if len(masked_word) != len(answer_word): | ||
raise InvalidWordException | ||
if answer_word =="" or masked_word =="" : | ||
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. it's possible to combine all conditions in line 41 with "or" for the same InvalidWordException |
||
raise InvalidWordException | ||
#convert answer_word and character to lower case | ||
lower_character = str.lower(character) | ||
lower_answer = str.lower(answer_word) | ||
if lower_character not in lower_answer: | ||
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. we can remove line 48 and 49 since there's no change to masked_word ... notice line 55 will return masked_word anyway ... |
||
return masked_word | ||
#loop through list of indices of answer_word to replace characters | ||
if lower_character in lower_answer: | ||
indices=find_substring(lower_answer, lower_character) | ||
for index in indices: | ||
masked_word = masked_word[:index] + lower_character + masked_word[index + 1:] | ||
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. great restructuring of word there! |
||
return masked_word | ||
|
||
|
||
|
||
def guess_letter(game, letter): | ||
pass | ||
|
||
if game['remaining_misses']==0 or game['answer_word'] == game['masked_word']: | ||
raise GameFinishedException | ||
else: | ||
#make game case insensitive by converting all strings to lower case | ||
letter = str.lower(letter) | ||
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. good idea to lowercase the letter and answer_word here.. which means you can remove the lowercase at _uncover_word() function ...! |
||
game['answer_word'] = str.lower(game['answer_word']) | ||
|
||
game['masked_word'] = _uncover_word(game['answer_word'], game['masked_word'], letter) | ||
|
||
if game['answer_word'] != game['masked_word']: | ||
game['previous_guesses'] += letter | ||
|
||
if letter not in game ['answer_word']: | ||
game['remaining_misses'] -= 1 | ||
|
||
if game['remaining_misses']==0: | ||
raise GameLostException | ||
|
||
if game['answer_word'] == game['masked_word']: | ||
raise GameWonException | ||
|
||
return game | ||
|
||
|
||
|
||
|
||
def start_new_game(list_of_words=None, number_of_guesses=5): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
argparse==1.2.1 | ||
wsgiref==0.1.2 |
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.
great words !