-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
34 lines (29 loc) · 971 Bytes
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from board import Board
from ship import Ship
from copy import deepcopy
default = [Ship('Aircraft carrier', 5), Ship('Battleship', 4),
Ship('Cruiser', 2), Ship('Cruiser', 2), Ship('Cruiser', 2), Ship('Destroyer', 1)]
class Player:
next_id = 1
def __init__(self, name, board_size, ship_list=default):
self.id = Player.next_id
Player.next_id += 1
self.name = name
self.board = Board(board_size)
self.guessBoard = Board(board_size)
self.ship_list = ship_list
def __str__(self):
"""Returns the player's name"""
return str(self.name)
def left_to_set(self):
"""Returns a sorted list of ship objects which yet need to be set."""
dupl = [deepcopy(ship) for ship in self.ship_list if not ship.placed]
left = []
for ship in set(dupl):
count = sum(1 for s in dupl if s == ship)
if count > 1:
ship.string = f"{count}x {ship}"
left.append(ship)
else:
left.append(ship)
return sorted(left, key=lambda s: (s.name, s.length))