-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
56 lines (42 loc) · 1.88 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import map
class Player:
def __init__(self, playerNumber):
self.playerNumber = playerNumber
self.myMap = map.Map()
self.myMap.regenerate_ships()
self.enemyMap = map.Map()
self.myShoots = []
def generate_layout(self):
self.myMap.regenerate_ships()
def handle_shoot(self, sht, other):
if sht not in self.myShoots and not self.enemyMap.is_shot_into_used_cell(sht):
self.myShoots.append(sht)
sht = self.myMap.handle_shoot(sht)
else:
sht.make_invalid()
dictionary = self.update_enemy_map(sht, other)
return sht, dictionary
def update_enemy_map(self, sht, other_player):
to_return = {'message': ''}
if sht.is_invalid():
to_return['message'] = "You've already shot into that cell!"
return to_return
if sht.is_missed():
self.enemyMap.mark_as_missed(sht)
to_add = other_player.playerNumber
if other_player.playerNumber == 0:
to_add += 2
to_return['message'] = "Missed! Player " + str(to_add) + ", please shoot!"
return to_return
if sht.is_wounded():
self.enemyMap.mark_as_wounded(sht)
to_return['message'] = "You hit a ship! Player " + str(self.playerNumber + 1) + " continue!"
if sht.is_dead() or sht.is_end_game():
self.enemyMap.mark_as_wounded(sht)
self.enemyMap.increment_dead_ships_count()
found_ship = self.enemyMap.find_ship(sht, other_player.myMap.ships)
self.enemyMap.mark_cells(found_ship)
to_return['message'] = "The ship is sank! Player " + str(self.playerNumber + 1) + " continue!"
if sht.is_end_game():
to_return['message'] = "The game is ended! Player " + str(self.playerNumber + 1) + " won the game!"
return to_return