Beispiel #1
0
def test_has_won():
    """
    Verify that has_won correctly calculates whether a game is lost or not
    based on the word and the characters chosen.
    """
    # TODO : upper / lower test
    assert not hangman.has_won('abc', 'ab')
    assert not hangman.has_won('abc', 'ab123')
    assert hangman.has_won('abc', 'abc')
    assert hangman.has_won('abc', 'abc123')
Beispiel #2
0
def hangman_get():
    """
    Handle the GET for retrieving the hangman string for a particular word and
    chosen characters.

    The result is the hangman string and the updated secret token.
    """
    secret_token = request.args['secret_token']
    word, characters = decode_secret_token(secret_token)
    characters += request.args['character']

    if hangman.has_lost(word, characters):
        status = 'lost'
    elif hangman.has_won(word, characters):
        secret_token = encode_secret_token(word, characters)
        status = 'won'
    else:
        secret_token = encode_secret_token(word, characters)
        status = 'playing'

    hangman_string = hangman.get_hangman_string(word, characters)
    hangman_image = len(hangman.get_incorrect_letters(word, characters))

    return jsonify(
        hangman_string=hangman_string,
        secret_token=secret_token,
        status=status,
        hangman_image=hangman_image,
    )
Beispiel #3
0
 def _verify_title_text(self):
     """
     Verify that the expected title text is shown based on whether our model
     tells us the game is won, lost or still being played.
     """
     if hangman.has_lost(self.word, self.incorrect_characters):
         assert page.has_text(LOST_TEXT)
     elif hangman.has_won(self.word, self.correct_characters):
         assert page.has_text(WON_TEXT)
     elif self.word:
         assert page.has_text(PLAYING_TEXT)
Beispiel #4
0
    def _guess_character_precondition(self, character):
        """
        Common preconditions for selecting a character in the actual game used for
        both incorrect and correct character guesses.

        We need to prevent a character being chosen which has been disabled in the
        front end, so we ensure that a character is not chosen in if the game is
        over, or if the particular character has already been chosen.

        :param character: The character selected by hypothesis.
        """
        assume(not hangman.has_lost(self.word, self.incorrect_characters))
        assume(not hangman.has_won(self.word, self.correct_characters))
        characters = set(self.correct_characters) | set(
            self.incorrect_characters)
        assume(character not in characters)
Beispiel #5
0
 def test_has_won_should_return_true_when_underscore_is_not_present_in_the_score(self):
     self.assertEqual(has_won(['j', 'a', 'x']), True)
Beispiel #6
0
 def test_has_won_should_return_false_when_underscore_is_present_in_the_score(self):
     self.assertEqual(has_won(['j', '_', 'x']), False)