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


Python Field.draw_field方法代码示例

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


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

示例1: Player

# 需要导入模块: from field import Field [as 别名]
# 或者: from field.Field import draw_field [as 别名]
class Player(object):
    def __init__(self, name, size):
        self.name = name
        self.ships = []
        self.field_size = size
        self.my_field = Field(size, is_enemy=False)
        self.enemy_field = Field(size, is_enemy=True)
        self.verbose = True

    def __str__(self):
        return self.name

    def print_for_player(self, message):
        if self.verbose:
            print(message)

    def place_ships(self):
        self.print_for_player("Now it's time for "+self.name+' to place ships!')
        for length, count in SHIPS_SET:
            for _ in range(count):
                while True:
                    try:
                        ship = self.__class__.ship_input(length, self.field_size)
                        if not ship.valid_ship_position(self.field_size):
                            self.print_for_player('Ship is out of field.')
                            continue
                        for other_ship in self.ships:
                            if other_ship.intersects(ship):
                                raise IndexError
                        self.ships.append(ship)
                        self.print_for_player('Ship is added!')
                        self.my_field.add_ship(ship)
                        if self.verbose:
                            self.my_field.draw_field()
                    except ValueError:
                        self.print_for_player('Bad input.')
                        continue
                    except IndexError:
                        self.print_for_player('Ship is intersecting with other ship')
                        continue
                    else:
                        break

    @staticmethod
    def ship_input(length, field_size):
        print('Place ship with length '+str(length))
        orientation = '-'
        if length != 1:
            orientation = get_input('Enter orientation, | or - :')
            if orientation not in ['|', '-']:
                raise ValueError()
        cords = get_input('Enter coordinates of upper-left corner of ship (F7 for example):')
        x = letter_to_int(cords[0])
        y = int(cords[1:])-1
        if (x not in range(0, field_size)) or (y not in range(0, field_size)):
            raise ValueError()
        ship = Ship(x, y, length, orientation)
        return ship

    def draw_fields(self):
        print('Your field:')
        self.my_field.draw_field()
        print('Your shots:')
        self.enemy_field.draw_field()

    def make_move(self):
        while True:
            try:
                cords = get_input(self.name+', take a shot! Enter shot coordinates (A1 for example):')
                x = letter_to_int(cords[0])
                y = int(cords[1:])-1
                if (x not in range(0, self.field_size)) or (y not in range(0, self.field_size)):
                    raise ValueError()
            except ValueError:
                print('Bad input.')
                continue
            else:
                break

        return x, y
开发者ID:andyzt,项目名称:tceh,代码行数:82,代码来源:players.py


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