From 2c1a5159153debce96bbd2cef7c71604cba36d02 Mon Sep 17 00:00:00 2001 From: Dean Fitzgerald Date: Mon, 19 Jan 2015 23:53:45 -0500 Subject: [PATCH 1/3] Mystery Word homework, game and tests --- mystery_word_game.py | 8 +++ mystery_word_tdd.py | 113 +++++++++++++++++++++++++++++++++++++++++++ test_mystery_word.py | 27 +++++++++++ 3 files changed, 148 insertions(+) create mode 100644 mystery_word_game.py create mode 100644 mystery_word_tdd.py create mode 100644 test_mystery_word.py diff --git a/mystery_word_game.py b/mystery_word_game.py new file mode 100644 index 0000000..ee967fa --- /dev/null +++ b/mystery_word_game.py @@ -0,0 +1,8 @@ +import random + +with open("/usr/share/dict/words") as w: + word_list = w.readlines() + +def mystery_word(): + mystery_word = random(word_list) + print("The word has {} letters.".format(len(mystery_word))) diff --git a/mystery_word_tdd.py b/mystery_word_tdd.py new file mode 100644 index 0000000..2683ae2 --- /dev/null +++ b/mystery_word_tdd.py @@ -0,0 +1,113 @@ +import random +import re + +with open("/usr/share/dict/words") as w: + word_list = w.readlines() + +guessed = [] +revealed = [] + + +def random_word_assignment(): + global random_word + random_word = random.choice(word_list).strip().lower() + return random_word + + +def game_intro(random_word): + print_statement = "The word has {} letters. You have 8 guesses.".format( + len(random_word), counter) + print(print_statement) + return print_statement + + +def change_count_guess(counter): + counter -= 1 + guessed.append(guess) + print(counter_statement) + return counter + + +# def display_counter(): +# print(counter_statement) + + +def correct_guess(guess, revealed): + revealed.append(guess) + print("Good job! " + counter_statement) + return revealed + + +def incorrect_guess(guess, counter): + counter -= 1 + guessed.append(guess) + print("Nope. " + counter_statement) + return guessed + + +def display_correct_guesses(guess): + print(" ".join(char if char in revealed else "_" for char in random_word)) + return (" ".join(char if char in revealed else "_" for char + in random_word)) + + +def get_guess(): + guess = input("What's your guess? ").lower() + if guess in re.search[a-z]: + return str(guess) + print(guess) + else: + print("{} is not a valid choice. Try again.".format(guess)) + + +def guess_list(): + print(guessed) + return guessed + + +def game_over(): + play_again = input("You won! Do you want to play again? Y or N ") + if play_again == 'y': + game_intro() + counter = 8 + else: + print("goodbye.") + + +counter = 8 +game_intro(random_word_assignment()) + +while True: + while counter > 0 and len(random_word) != len(revealed): + guess = input("What's your guess? ") + counter_statement = "You have {} guesses left. '\n'".format(counter) + if len(guess) > 1: + guess = input("You can only guess one letter at a time. + Try again. ") + elif guess in random_word: + correct_guess(guess, revealed) + display_correct_guesses(guess) + elif guess not in random_word: + counter -= 1 + incorrect_guess(guess, counter) + display_correct_guesses(guess) + print(guessed) + else: + game_over() + print("You have run out of guesses. The word is {}.". + format(random_word)) + break + + else: + if len(random_word) == len(revealed): + play_again = input("You won! Do you want to play again? Y or N ") + if play_again == 'y': + game_intro() + counter = 8 + else: + break + else: + game_over() + break + +# Still accepts non-letters as correct. Replay function not working. diff --git a/test_mystery_word.py b/test_mystery_word.py new file mode 100644 index 0000000..f8c7fd9 --- /dev/null +++ b/test_mystery_word.py @@ -0,0 +1,27 @@ +#Test each step + +import mystery_word_tdd as mw + +def test_random_word_assignment(): + assert type(mw.random_word_assignment()) == str + +def test_game_intro(): + assert mw.game_intro("word", 8) == "The word has 4 letters. You have 8 guesses." + +def test_change_count_guess(): + assert mw.change_count_guess(8) == 7 + +def test_correct_guess(): + assert (mw.correct_guess(guess)) == ["guess"] + +def test_display_correct_guesses(): + assert type(mw.display_correct_guesses("guess")) == str + +# def test_get_guess(): +# assert mw.get_guess() == "input" + +def test_game_over(): + assert mw.game_over() == "You lose. The word was " + random_word +".'" + +def test_guess_list(): + assert type(mw.guess_list) == list From 0b251a8ff8db7594bdbd088b0b72ec1ac384b3ed Mon Sep 17 00:00:00 2001 From: Dean Fitzgerald Date: Tue, 20 Jan 2015 08:37:36 -0500 Subject: [PATCH 2/3] Mystery Word Game --- mystery_word.py | 113 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 mystery_word.py diff --git a/mystery_word.py b/mystery_word.py new file mode 100644 index 0000000..2683ae2 --- /dev/null +++ b/mystery_word.py @@ -0,0 +1,113 @@ +import random +import re + +with open("/usr/share/dict/words") as w: + word_list = w.readlines() + +guessed = [] +revealed = [] + + +def random_word_assignment(): + global random_word + random_word = random.choice(word_list).strip().lower() + return random_word + + +def game_intro(random_word): + print_statement = "The word has {} letters. You have 8 guesses.".format( + len(random_word), counter) + print(print_statement) + return print_statement + + +def change_count_guess(counter): + counter -= 1 + guessed.append(guess) + print(counter_statement) + return counter + + +# def display_counter(): +# print(counter_statement) + + +def correct_guess(guess, revealed): + revealed.append(guess) + print("Good job! " + counter_statement) + return revealed + + +def incorrect_guess(guess, counter): + counter -= 1 + guessed.append(guess) + print("Nope. " + counter_statement) + return guessed + + +def display_correct_guesses(guess): + print(" ".join(char if char in revealed else "_" for char in random_word)) + return (" ".join(char if char in revealed else "_" for char + in random_word)) + + +def get_guess(): + guess = input("What's your guess? ").lower() + if guess in re.search[a-z]: + return str(guess) + print(guess) + else: + print("{} is not a valid choice. Try again.".format(guess)) + + +def guess_list(): + print(guessed) + return guessed + + +def game_over(): + play_again = input("You won! Do you want to play again? Y or N ") + if play_again == 'y': + game_intro() + counter = 8 + else: + print("goodbye.") + + +counter = 8 +game_intro(random_word_assignment()) + +while True: + while counter > 0 and len(random_word) != len(revealed): + guess = input("What's your guess? ") + counter_statement = "You have {} guesses left. '\n'".format(counter) + if len(guess) > 1: + guess = input("You can only guess one letter at a time. + Try again. ") + elif guess in random_word: + correct_guess(guess, revealed) + display_correct_guesses(guess) + elif guess not in random_word: + counter -= 1 + incorrect_guess(guess, counter) + display_correct_guesses(guess) + print(guessed) + else: + game_over() + print("You have run out of guesses. The word is {}.". + format(random_word)) + break + + else: + if len(random_word) == len(revealed): + play_again = input("You won! Do you want to play again? Y or N ") + if play_again == 'y': + game_intro() + counter = 8 + else: + break + else: + game_over() + break + +# Still accepts non-letters as correct. Replay function not working. From 98846cb1b70d0a509c778548f3dfbac09161aaaf Mon Sep 17 00:00:00 2001 From: Dean Fitzgerald Date: Tue, 20 Jan 2015 08:43:03 -0500 Subject: [PATCH 3/3] Mystery Word Game --- mystery_word.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/mystery_word.py b/mystery_word.py index 2683ae2..8ae6347 100644 --- a/mystery_word.py +++ b/mystery_word.py @@ -65,13 +65,10 @@ def guess_list(): return guessed -def game_over(): - play_again = input("You won! Do you want to play again? Y or N ") - if play_again == 'y': - game_intro() - counter = 8 - else: - print("goodbye.") +# def game_over(): +# game_over = "The word was {}."format(random_word) +# print(game_over) + counter = 8 @@ -82,8 +79,7 @@ def game_over(): guess = input("What's your guess? ") counter_statement = "You have {} guesses left. '\n'".format(counter) if len(guess) > 1: - guess = input("You can only guess one letter at a time. - Try again. ") + guess = input("You can only guess one letter at a time. ") elif guess in random_word: correct_guess(guess, revealed) display_correct_guesses(guess) @@ -93,21 +89,15 @@ def game_over(): display_correct_guesses(guess) print(guessed) else: - game_over() print("You have run out of guesses. The word is {}.". format(random_word)) break else: if len(random_word) == len(revealed): - play_again = input("You won! Do you want to play again? Y or N ") - if play_again == 'y': - game_intro() - counter = 8 - else: - break + print("You won!") + break else: - game_over() break # Still accepts non-letters as correct. Replay function not working.