def start_game(): global myFleet, enemyFleet # clear the screen if(platform.system().lower()=="windows"): cmd='cls' else: cmd='clear' os.system(cmd) print(r''' __ / \ .-. | | * _.-' \ \__/ \.-' \ / _/ | _ / | /_\ \ \_/ """"""""''') while True: print() print("Player, it's your turn") position = parse_position(input("Enter coordinates for your shot :")) is_hit = GameController.check_is_hit(enemyFleet, position) if is_hit: print(r''' \ . ./ \ .:"";'.:.."" / (M^^.^~~:.'""). - (/ . . . \ \) - ((| :. ~ ^ :. .|)) - (\- | \ / | /) - -\ \ / /- \ \ / /''') print("Yeah ! Nice hit !" if is_hit else "Miss") position = get_random_position() is_hit = GameController.check_is_hit(myFleet, position) print() print(f"Computer shoot in {position.column.name}{position.row} and {'hit your ship!' if is_hit else 'miss'}") if is_hit: print(r''' \ . ./ \ .:"";'.:.."" / (M^^.^~~:.'""). - (/ . . . \ \) - ((| :. ~ ^ :. .|)) - (\- | \ / | /) - -\ \ / /- \ \ / /''')
def initialize_enemyFleet(): # TODO: Корабли противника должны выставлятся случайным образом # TODO: Докстринг global enemyFleet import numpy as np col_mapper = { 'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8 } ivn_col_mapper = {v: k for k, v in col_mapper.items()} def add_ship_position(size: int, ship=Ship) -> None: for i in range(size): col_letter = lambda x: ivn_col_mapper[np.random.randint(1, 9, x)[0] ] row_num = lambda x: np.random.randint(1, 9, x)[0] rand_col = Letter.__dict__['_member_map_'][col_letter(1)] rand_row = row_num(1) ship.positions.append(Position(rand_col, rand_row)) return None enemyFleet = GameController.initialize_ships() [add_ship_position(ship.size, ship) for ship in enemyFleet]
def initialize_myFleet(): # TODO: Докстринг # TODO: Корабли должны выставлятся согласно правил игры global myFleet myFleet = GameController.initialize_ships() show_grid( [Back.BLUE + Fore.BLACK + '___' + Style.RESET_ALL for x in range(64)]) show_grid([ Fore.GREEN + '_X_' + Style.RESET_ALL if (x % 2 == 0) else Fore.RED + '_X_' + Style.RESET_ALL for x in range(100, 164) ]) print( "Please position your fleet (Game board has size from A to H and 1 to 8) :" ) for ship in myFleet: print() print( Fore.BLUE + f"Please enter the positions for the {ship.name} (size: {ship.size})" + Style.RESET_ALL) for i in range(ship.size): position_input = input( f"Enter position {i} of {ship.size} (i.e A3):") ship.add_position(position_input) print([_ship.positions for i, _ship in enumerate(myFleet)])
def initialize_enemyFleet(): global enemyFleet enemyFleet = GameController.initialize_ships() enemyFleet[0].positions.append(Position(Letter.B, 4)) enemyFleet[0].positions.append(Position(Letter.B, 5)) enemyFleet[0].positions.append(Position(Letter.B, 6)) enemyFleet[0].positions.append(Position(Letter.B, 7)) enemyFleet[0].positions.append(Position(Letter.B, 8)) enemyFleet[1].positions.append(Position(Letter.E, 6)) enemyFleet[1].positions.append(Position(Letter.E, 7)) enemyFleet[1].positions.append(Position(Letter.E, 8)) enemyFleet[1].positions.append(Position(Letter.E, 9)) enemyFleet[2].positions.append(Position(Letter.A, 3)) enemyFleet[2].positions.append(Position(Letter.B, 3)) enemyFleet[2].positions.append(Position(Letter.C, 3)) enemyFleet[3].positions.append(Position(Letter.F, 8)) enemyFleet[3].positions.append(Position(Letter.G, 8)) enemyFleet[3].positions.append(Position(Letter.H, 8)) enemyFleet[4].positions.append(Position(Letter.C, 5)) enemyFleet[4].positions.append(Position(Letter.C, 6))
def add_position(self, position: str): # TODO: Корабль должен выставлятся согласно правил игры from torpydo.game_controller import GameController position = GameController.validate_position(position) letter = Letter[position.upper()[0]] number = int(position[1]) self.positions.append(Position(letter, number)) return position
def parse_position(position: str): """ Проверяет позицию """ # TODO: Нельзя стрелять дважды в одну и ту же клетку # TODO: Показывать выбранную клетку на карте position = GameController.validate_position(position) letter = Letter[position.upper()[0]] number = int(position[1]) position = Position(letter, number) return position
def initialize_myFleet(): global myFleet myFleet = GameController.initialize_ships() print("Please position your fleet (Game board has size from A to H and 1 to 8) :") for ship in myFleet: print() print(f"Please enter the positions for the {ship.name} (size: {ship.size})") for i in range(ship.size): position_input = input(f"Enter position {i} of {ship.size} (i.e A3):") ship.add_position(position_input)
def start_game(): global myFleet, enemyFleet print(r''' #### WARSHIPS #### ##### SCRUM ##### #### BATTLE ##### * __ * * /|| \ .-. | o | _.-' \ | Oo | .-' \ \__/ / SUSHA _/ | _ / | /_\ \ \_/ """""""" ________________ ver. 0.1 ''') while True: # TODO: Избавится от бесконечного цикла print("It's your turn,", Fore.CYAN + f"{player}" + Style.RESET_ALL) show_grid([ Fore.GREEN + '_X_' + Style.RESET_ALL if (x // 2 == 0) else Fore.RED + '_X_' + Style.RESET_ALL for x in range(100, 164) ]) position = parse_position(input("Enter coordinates for your shot :")) is_hit = GameController.check_is_hit(enemyFleet, position) if is_hit: print(Fore.LIGHTRED_EX + r''' \ . ./ \ .:"";'.:.."" /d (M^^.^~~:.'""). - (/ . . . \ \) - ((| :. ~ ^ :. .|)) - (\- | \ / | /) - -\ \ / /- \ \ / /''' + Style.RESET_ALL) print("Yeah ! Nice hit !" if is_hit else Fore.LIGHTBLACK_EX + "Miss" + Style.RESET_ALL) position = get_random_position() is_hit = GameController.check_is_hit(myFleet, position) print() print( f"Computer shoot in {position.column.name}{position.row} and {'hit your ship!' if is_hit else Fore.LIGHTBLUE_EX + 'miss' + Style.RESET_ALL}" ) if is_hit: print(Fore.MAGENTA + r''' \ . ./ \ .:"";'.:.."" / (M^^.^~~:.'""). - (/ . . . \ \) - ((| :. ~ ^ :. .|)) - (\- | \ / | /) - -\ \ / /- \ \ / /''' + Style.RESET_ALL)
def test_is_ship_valid_true(self): self.assertTrue(GameController.is_ship_valid(self.ships[0]))
def test_is_ship_valid_false(self): self.assertFalse( GameController.is_ship_valid(Ship("Not Valid", 3, Color.RED)))
def test_check_is_hit_ship_none(self): with self.assertRaises(ValueError): self.assertRaises( GameController.check_is_hit(None, Position(Letter.A, 1)))
def test_check_is_hit_position_none(self): with self.assertRaises(ValueError): self.assertRaises(GameController.check_is_hit(self.ships, None))
def test_check_is_hit_false(self): self.assertFalse( GameController.check_is_hit(self.ships, Position(Letter.B, 1)))
def test_check_is_hit_true(self): self.assertTrue( GameController.check_is_hit(self.ships, Position(Letter.A, 1)))