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


Python Move.card1方法代码示例

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


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

示例1: make_move

# 需要导入模块: from models import Move [as 别名]
# 或者: from models.Move import card1 [as 别名]
    def make_move(self, request):
        """Makes a move. Returns a game state with message. The two
        positions are a and b"""
        game = get_by_urlsafe(request.urlsafe_game_key, Game)
        if game.game_over:
            return game.to_form('Game already over!')

        cards = Card.query(Card.game == game.key, Card.matched == False)
        position1 = request.a
        position2 = request.b

        mark_card = Card.query(Card.position == position1)
        for card in mark_card:
            card.seen = True
            card.put()

        mark_card = Card.query(Card.position == position2)
        for card in mark_card:
            card.seen = True
            card.put()

        unmatched_cards = []
        for card in cards:
            unmatched_cards.append(card)

        unmatched_cards_count = len(unmatched_cards)
        if unmatched_cards_count < 2:
            raise IndexError('No unmatched pair found but the game is not '
                             'over')

        cards_list = []
        msg = 'Boo'

        game.attempts += 1
        move = Move()
        move.card1 = position1
        move.card2 = position2
        move.game = game.key
        move.result = msg

        for card in unmatched_cards:
            if card.position == position1 or card.position == position2:
                cards_list.append(card)

        try:
            if cards_list[0].matched or cards_list[1].matched:
                move.result = 'Card already matched'
                move.put()
                return game.to_form('Card already matched')

        except:
            msg = 'One of the cards is already matched. Please Try again.'
            move.result = msg
            move.put()
            return game.to_form(msg)

        if cards_list[0].value == cards_list[1].value:
            cards_list[0].matched = cards_list[1].matched = True
            cards_list[0].put()
            cards_list[1].put()
            msg = 'Yay'
            move.result = msg
            if unmatched_cards_count == 2:
                game.game_over = True
                game.end_game()
                msg = 'You win, Game Over'
                move.put()
                users = User.query(User.key == game.user)
                for user in users:
                    user.games_played += 1
                    user.average_attempt = game.attempts
                    user.calculate_score()
                    user.put()
                return game.to_form(msg)

        game.put()

        move.put()
        return game.to_form(msg)
开发者ID:khanal-abhi,项目名称:game-api,代码行数:81,代码来源:api.py


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