Пример #1
0
    def make_move(self, request):
        """Makes a move. Returns a game state with message"""
        game = get_by_urlsafe(request.urlsafe_game_key, Game)
        # Check if the game is finished
        if game.game_over:
            return game.to_form('Game already finished!')

        # Only alphabetic character is allowed
        if not request.guess.isalpha():
            return game.to_form('Only alphabetic character is allowed!')
        guess = request.guess.upper()

        # Only one character is allowed
        if len(guess) != 1:
            return game.to_form('Only one character allowed')

        # We will check if the character is already used
        game_key = ndb.Key(Game, request.urlsafe_game_key)
        ls = GameHistory.query(ancestor=game_key).filter(
            GameHistory.guess == guess).count()
        if ls != 0:
            return game.to_form('Character already used' + guess)

        found = False
        temp = list(game.guess_word)
        temp_wimp = list(game.word_in_progress)
        for i, val in enumerate(temp):
            if val == guess:
                temp_wimp[i] = guess
                found = True
                msg = 'Key found ' + guess
                GameHistory.create_game_history(request.urlsafe_game_key,
                                                guess, found, i, 'Found')

        game.word_in_progress = ''.join(temp_wimp)

        if found is False:
            GameHistory.create_game_history(request.urlsafe_game_key, guess,
                                            found, -1, 'Not found')
            game.attempts_remaining -= 1
            msg = 'Character not found in word: ' + request.guess

        if game.word_in_progress == game.guess_word:
            game.end_game(True)
            msg = 'You win'

        if game.attempts_remaining < 1:
            game.end_game(False)
            GameHistory.create_game_history(request.urlsafe_game_key, guess,
                                            found, -1,
                                            'Not attempts remaining you lose')
            return game.to_form(msg + ' Game over!')
        else:
            game.put()
            return game.to_form(msg)