当前位置: 首页>>代码示例>>Python>>正文


Python Score.allocate_ids方法代码示例

本文整理汇总了Python中models.Score.allocate_ids方法的典型用法代码示例。如果您正苦于以下问题:Python Score.allocate_ids方法的具体用法?Python Score.allocate_ids怎么用?Python Score.allocate_ids使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在models.Score的用法示例。


在下文中一共展示了Score.allocate_ids方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _new_game

# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import allocate_ids [as 别名]
    def _new_game(self, user, game_name):
        # retrieve key from user_name
        user_key = ndb.Key(User, user.user_name)

        # generate game_id
        game_id = Game.allocate_ids(size=1, parent=user_key)[0]

        # create key using generated game_id and user as its ancestor
        game_key = ndb.Key(Game, game_id, parent=user_key)

        # generate random word for this game
        rw = RandomWords()
        word = rw.random_word()

        guessed_chars_of_word = []

        # make this impl more 'pythonic' way
        for c in word:
            guessed_chars_of_word.append('*')

        game = Game(key=game_key,
                    game_id=game_id,
                    game_name=game_name,
                    word=word,
                    guessed_chars_of_word=guessed_chars_of_word)
        # save game
        game.put()

        # score id
        score_id = Score.allocate_ids(size=1, parent=user_key)[0]

        # score key using generated score_id and user as its ancestor
        score_key = ndb.Key(Score, score_id, parent=user_key)

        # score entity for this game
        score = Score(key=score_key,
                      score_id=score_id,
                      game_key=game_key)
        # save score
        score.put()

        # capture game snapshot
        self._capture_game_snapshot(game, '')

        return game
开发者ID:ankjai,项目名称:fullstack-nanodegree-design-a-game,代码行数:47,代码来源:game.py

示例2: make_move

# 需要导入模块: from models import Score [as 别名]
# 或者: from models.Score import allocate_ids [as 别名]
    def make_move(self, request):
        """Makes a move. Returns a game state with message"""
        # get game with url_safe key
        game = get_by_urlsafe(request.urlsafe_game_key, Game)
        
        p_key = ndb.Key(Game,game.user_name)
        c_id = Score.allocate_ids(size = 1 , parent=p_key)[0]
        s_key = ndb.Key(Score , c_id ,parent=p_key)
        # s_key = ndb.Key(Game,game.user_name) 
        
        # check if user has run out of attempts
        if game.attempts_remaining < 1:
            game.end_game(won=False,user_name=game.user_name,guesses=0)
            return game.to_form(' Game over!')

        # check if game is over
        if game.game_over:
            return game.to_form('Game already over!')
        # reduce the remaining_attempts by one to keep track of number of moves left
        game.attempts_remaining -= 1
        userGuess = request.guess.upper()
        
        # check if user guessed the complete word
        if userGuess == game.target:
            score = Score(won=True,guesses=game.attempts_remaining,
                        user=game.user_name,key=s_key)
            score.put()
            game.end_game(won=True,user_name=game.user_name,
                          guesses=game.attempts_allowed - game.attempts_remaining)
            # store game entry for game history
            game.store_move(guess=userGuess, message='you win',game=game)
            # update guessed word
            game.guess = userGuess
            game.put()
            return game.to_form('You win!')        
        
        # check if user has entered more than one alphabet but not the whole word
        if len(userGuess) > 1:
            game.store_move(guess=userGuess, message='Invalid entry',game=game)
            retStr = 'You can enter more than one alphabet only if you know the complete word'
            return game.to_form(retStr)

        wordLst = list (game.target)
        wordFlag = False
        word = list(game.guess)
        
        # check user entry for any correct guesses
        for idx,val in enumerate(wordLst):
          if val == userGuess:
            wordFlag = True
            word[idx] = val
        #if correct guess
        if(wordFlag):
          game.guess = "".join(word)
          game.put()
          # check if game is compleated
          if game.guess == game.target:
            game.end_game(won=True,user_name=game.user_name,
                          guesses=game.attempts_allowed - game.attempts_remaining)
            score = Score(won=True,guesses=game.attempts_remaining,
                        user=game.user_name,key=s_key)
            score.put()
            game.store_move(guess=userGuess, message='you win',game=game)
            return game.to_form('You win!')        
          else: 
            game.store_move(guess=userGuess, message='correct guess',game=game)
            return game.to_form('right guess')
        else:
          game.store_move(guess=userGuess, message="you're wrong",game=game)
          # provide hint message 
          if(game.attempts_remaining == 5 or game.attempts_remaining == 6):
            retStr = " HINT : seems like you need help the first 3 charecters are "
            return game.to_form(retStr+wordLst[0]+wordLst[1]+wordLst[2])
          return game.to_form("Nope , You're wrong")
开发者ID:ajack13,项目名称:game-api,代码行数:76,代码来源:api.py


注:本文中的models.Score.allocate_ids方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。