def __init__(self, player_controller, computer_controller): self.__player = player_controller self.__computer = computer_controller self.__computer_planes() self.__first_top = Point(0, 0, "First") self.__first_bot = Point(0, 0, "First") self.__second_top = Point(0, 0, "Second") self.__second_bot = Point(0, 0, "Second")
def __read_point(self): read = input("Insert coordinates: ").split() if len(read) == 3: Validator.validate_types(read) if read[2] not in ["N", "S", "W", "E"]: raise PlaneException("Error!!! Invalid orientation!!!\n") return Point(read[0], read[1], read[2]) elif len(read) == 2: Validator.validate_types(read) return Point(read[0], int(read[1]), "Default") else: raise PlaneException("Error!!! Please insert a valid point!!!\n")
def computer_turn(self): while True: if self.__player.planes_alive == 2: x = randint(self.__first_top.x, self.__first_bot.x) y = randint(self.__first_top.y, self.__first_bot.y) else: x = randint(self.__second_top.x, self.__second_bot.x) y = randint(self.__second_top.y, self.__second_bot.y) point = Point(x, y, self.__player.get_point(Point(x, y, "Default"))) if not self.__computer.check_hit(point): self.__computer.hit(point, self.__player.attacked(point)) break
def __computer_planes(self): planes = 0 directions = ["N", "S", "W", "E"] while planes != 2: head = Point(randint(1, 8), randint(1, 8), directions[randint(0, 3)]) plane = Plane(head) if not Checker.validate_plane(Checker, head): if not Checker.overlap_plane(self.__computer.get_planes, plane.coordinates()): self.__computer.new_plane(head) planes += 1
def __corners(self, head): if head.value == 'N': top_left = Point(head.x, head.y - 2, "top_left") bottom_right = Point(head.x + 3, head.y + 2, "bot_right") return [top_left, bottom_right] if head.value == 'S': top_left = Point(head.x - 3, head.y - 2, "top_left") bottom_right = Point(head.x, head.y + 2, "bot_right") return [top_left, bottom_right] if head.value == 'E': top_left = Point(head.x - 2, head.y - 3, "top_left") bottom_right = Point(head.x + 2, head.y, "bot_right") return [top_left, bottom_right] if head.value == 'W': top_left = Point(head.x - 2, head.y, "top_left") bottom_right = Point(head.x + 2, head.y + 3, "bot_right") return [top_left, bottom_right]
def validate_plane(self, head): if head.value == 'N': top_left = Point(head.x, head.y - 2, "top_left") bottom_right = Point(head.x + 3, head.y + 2, "bot_right") return self.__check_corners(top_left, bottom_right) if head.value == 'S': top_left = Point(head.x - 3, head.y - 2, "top_left") bottom_right = Point(head.x, head.y + 2, "bot_right") return self.__check_corners(top_left, bottom_right) if head.value == 'E': top_left = Point(head.x - 2, head.y - 3, "top_left") bottom_right = Point(head.x + 2, head.y, "bot_right") return self.__check_corners(top_left, bottom_right) if head.value == 'W': top_left = Point(head.x - 2, head.y, "top_left") bottom_right = Point(head.x + 2, head.y + 3, "bot_right") return self.__check_corners(top_left, bottom_right)
def setUp(self): self.p = Point("A", 2, "Default")
def test_planes_alive(self): self.player.new_plane(Point("E", 4, "W")) self.assertEqual(self.player.planes_alive, 1) self.player.hit(Point("E", 4, "W"), self.player.attacked(Point("E", 4, "W"))) self.assertEqual(self.player.planes_alive, 0)
def test_check_hit(self): self.player.new_plane(Point("E", 4, "W")) self.player.hit(Point("E", 5, "#"), self.player.attacked(Point("E", 5, "#"))) self.assertEqual(self.player.check_hit(Point("E", 5, "#")), True)
def test_get_point(self): self.player.new_plane(Point("E", 4, "W")) self.assertEqual(self.player.get_point(Point("E", 4, "Default")), "W")
def test_new_plane(self): self.player.new_plane(Point("E", 4, "W")) self.assertEqual(self.player.planes_alive, 1)
def setUp(self): self.head = Point("E", 4, "W")
def test_add(self): p = Point("A", 1, "#") self.board.add([p]) self.assertEqual(self.board[1][1], "#")