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


Python UI.print_map方法代码示例

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


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

示例1: __init__

# 需要导入模块: from ui import UI [as 别名]
# 或者: from ui.UI import print_map [as 别名]
class Game:
    welcome_message = '\n{} {} {}\nWrite start to play or exit to quit!'.format(
                      '-' * 10, 'Tic Tac Toe!', '-' * 10)

    winner_message = 'Game Over! Winner is {}'

    winning_routes = [[(0, 0), (0, 1), (0, 2)],
                      [(0, 0), (1, 0), (2, 0)],
                      [(0, 0), (1, 1), (2, 2)],
                      [(1, 0), (1, 1), (1, 2)],
                      [(0, 1), (1, 1), (2, 1)],
                      [(0, 2), (1, 2), (2, 2)],
                      [(2, 0), (2, 1), (2, 2)],
                      [(2, 0), (1, 1), (0, 2)]]

    def __init__(self):
        self.UI = UI()

        self.AI = AI(self.UI.BOARD, self.winning_routes)
        self.player = None
        self.game_over = False

    def input_choice(self):
        x = int(self.UI.get_input('x: '))
        y = int(self.UI.get_input('y: '))

        if self.are_x_y_valid(x, y) or self.UI.BOARD[x][y] != ' ':
            raise ValueError

        return (x, y)

    def are_x_y_valid(self, x, y):
        return x >= self.UI.SIZE or y >= self.UI.SIZE or x < 0 or y < 0

    def start(self):
        while self.game_over is False:
            self.UI.print_map()

            self.tick()

    def tick(self):
        try:
            choice = self.input_choice()

        except ValueError:
            self.UI.display('\nInvalid input! Please reenter.\n')
            return

        self.UI.BOARD[choice[0]][choice[1]] = self.UI.X

        if self.is_board_full():
            self.trigger_game_over(None)
            return

        self.AI.attack()

        is_there_a_winner = self.check_for_winner()

        if is_there_a_winner is not False:
            self.trigger_game_over(is_there_a_winner)
            return

    def check_for_winner(self):
        for route in self.winning_routes:
            if all(self.UI.BOARD[coords[0]][coords[1]] == self.UI.X for coords in route):
                return self.UI.X

            if all(self.UI.BOARD[coords[0]][coords[1]] == self.UI.O for coords in route):
                return self.UI.O

        return False

    def is_board_full(self):
        return all(all(block != ' ' for block in line) for line in self.UI.BOARD)

    def trigger_game_over(self, winner):
        self.game_over = True
        self.UI.print_map()
        self.UI.display(self.winner_message.format('Player' if winner == self.UI.X else
                                                   ('Enemy' if winner == self.UI.O else winner)))

    def go(self):
        self.UI.display(self.welcome_message)

        if self.UI.get_input('> ') == 'start':
            self.start()
开发者ID:betrakiss,项目名称:HackBulgaria,代码行数:88,代码来源:game.py


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