def place_ship(ship, board): clear() print(board) while True: try: cell = input("Place {} where? [A1-J10] ".format(ship)) board[cell] = ship[0] break except (IndexError, KeyError, ValueError): clear() print(board, "What?", sep='\n', end=' ') clear() print(board) direction = input("Which way? [ldru] ") board.place(ship, cell, direction)
from itertools import cycle UNKNOWN = '·' MISS = ' ' HIT = '#' allies = btl.Field("Allies", default=UNKNOWN) enemies = btl.Field("Enemies", default=UNKNOWN) # main try: ships = "Carrier Battleship Destroyer Submarine Patrolboat".split(' ') for ship in ships: prompt.place_ship(ship, allies) btl.clear() btl.print_side_by_side(enemies, allies) enemies_first = input("Are you going first? [yN] ").lower() != 'y' is_enemy_move = cycle( (True, False) if enemies_first else (False, True) ) except KeyboardInterrupt: print("\nQuit before game began!") exit(1) btl.clear() btl.print_side_by_side(enemies, allies) allies_have_gone = False # game loop while True: