def test_place_ship_ew(): board = Board() ship = {'code': 'C', 'size': 2} board.place_ship(0, 0, ship, 'EW') assert board[0][0] == 'C' assert board[0][1] == 'C'
def test_place_ship_exc(): board = Board() ship = {'code': 'C', 'size': 2} with pytest.raises(IndexError) as excinfo: board.place_ship(0, 9, ship, 'NS') assert 'index out of range' in str(excinfo.value)
class Human(Player): def __init__(self): self.brd = Board() self.sunk = 0 self.occupied = set() def name(self): return "Player 1" def set_up(self): """Prompts the user to set up their board; manually choose which ship and hide it with hide_ships(), or automatically hide all. """ fleet = self.brd.fleet fleet_lst = [fleet[ship] for ship in fleet] random.shuffle(fleet_lst) while len(fleet_lst) > 0: print(PROMPT['border']) show_board(self.brd) select = input(PROMPT['which_ship'].format( '\n '.join([str(ship) for ship in fleet_lst]))) if select.lower() == 'a': # automates the hiding process for ship in fleet_lst: self.auto_hide_ships(ship, 1) self._confirm_setup() return # for manually hiding the selected ship if fleet.get(select.upper()) in fleet_lst: check = self.hide_ships(fleet.get(select.upper())) if check is True: fleet_lst.remove(fleet.get(select.upper())) else: continue elif select == '': self.hide_ships(fleet_lst.pop(0)) else: print(PROMPT['which_ship_explain'].format( ' '.join([str(ship.sign) for ship in fleet_lst]))) self._confirm_setup() def _confirm_setup(self): """Display the completed board setup for player to confirm or revise. """ show_board(self.brd) check = input(PROMPT['good2go']).lower() if check == 'n' or check == 'no': print(PROMPT['start_again']) self.brd.remove_fleet() return self.set_up() else: return def hide_ships(self, ship): """Prompts the user to select head using _pick_coord(), _head2tail() to show possible coords for the tail and _full() to select tail to hide a ship sends the ship object and a list of coords to Board. """ self.occupied = set(key for key in self.brd.board if self.brd.board[key] in FLEET.keys()) for n in range(3): print(PROMPT['lets_hide'].format(ship)) head = pick_coord('hide_head') if head in self.occupied: print(PROMPT['occupied']) continue if head is None: continue h2t = self._head2tail(ship, head) pos = self._full(h2t) if pos is None: continue display_pos = (convert(coord) for coord in pos) ans = input(PROMPT['pos_ok?'].format( str(ship), ' '.join(display_pos))).lower() if ans == 'n' or ans == 'no': self.hide_ships(ship) else: self.brd.place_ship(ship, pos) print(PROMPT['player_hidden'].format(str(ship))) return True def _full(self, h2t): """Uses _pick_coord() to prompt user to select the tail coord of the ship from the dict returned by _head2tail() returns the full set of coords to return to hide_ships() if selection is valid. """ if len(h2t) == 0: print(PROMPT['no_tail']) return None options = [convert(key) for key in h2t.keys()] if len(h2t) == 1: ans = input(PROMPT['this_tail_ok'].format( '{ ' + options[0] + ' }')).lower() if ans == 'n' or ans == 'no': return None else: return h2t[convert(options[0])] for n in range(3): print(PROMPT['tail_option'].format( '{ ' + ' '.join(options) + ' }')) tail = pick_coord('hide_tail') if tail in h2t.keys(): return h2t[tail] elif tail == 'r': # exception if user wants to revise head coord return None else: continue else: print(PROMPT['wrong_tail']) return None def where2bomb(self): """Human selects a coordinate to bomb. """ bomb = pick_coord('where2bomb') print(PROMPT['player_attack'].format(convert(bomb))) return bomb def win(self): """Declares Human as the winner and shows the board. """ print(PROMPT['result']) # show_game(self.players[1].brd, self.players[0].brd) print(PROMPT['one_wins'])