-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathComputer.py
90 lines (81 loc) · 3.76 KB
/
Computer.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import random
from Player import Player, in_bounds
BOARD_SIZE = 10
class Computer(Player):
set1 = {5: 1, 4: 1, 3: 2, 2: 1}
set2 = {4: 1, 3: 2, 2: 3, 1: 4}
def __init__(self, mode):
super().__init__()
self.mode = mode
self.last_hit = None
self.battleships_set = random.choice([Computer.set1, Computer.set2])
self.total_possible_hits = sum([key*self.battleships_set[key] for key in self.battleships_set.keys()])
def get_start_point(self, battleship_size, direction):
"""chooses a location for placing a battleship randomly"""
problem = True
horizontal_offset = 1 if direction == 'horizontal' else 0
vertical_offset = 1 if direction == 'vertical' else 0
start_x, start_y = None, None
while problem:
problem = False
start_x, start_y = random.choice(self.available_places)
for i in range(battleship_size):
if not (start_x + i * vertical_offset, start_y + i * horizontal_offset) in self.available_places:
problem = True
return start_x, start_y
def place_battleships(self):
for battleship_size in self.battleships_set.keys():
while self.battleships_set[battleship_size] > 0:
self.battleships_set[battleship_size] -= 1
direction = random.choice(['vertical', 'horizontal'])
start_x, start_y = self.get_start_point(battleship_size, direction)
self._place_battleship(direction, start_x, start_y, battleship_size)
def hard_mode(self):
x, y = self.last_hit[0], self.last_hit[1]
"""if game mode is hard, ensures that the computer will continue to shoot a battleship it hit previously"""
horizontal = [self.opponent_board[location] for location in (filter(in_bounds, [(x, y+1), (x, y-1)]))]
adjacent_x = x
if 'X' not in horizontal:
while adjacent_x < BOARD_SIZE-1 and self.opponent_board[adjacent_x, y] == 'X':
adjacent_x += 1
if self.opponent_board[adjacent_x, y] == ' ':
return adjacent_x, y
adjacent_x = x
while adjacent_x > 0 and self.opponent_board[adjacent_x, y] == 'X':
adjacent_x -= 1
if self.opponent_board[adjacent_x, y] == ' ':
return adjacent_x, y
adjacent_y = y
while adjacent_y < BOARD_SIZE-1 and self.opponent_board[x, adjacent_y] == 'X':
adjacent_y += 1
if self.opponent_board[x, adjacent_y] == ' ':
return x, adjacent_y
adjacent_y = y
while adjacent_y > 0 and self.opponent_board[x, adjacent_y] == 'X':
adjacent_y -= 1
if self.opponent_board[x, adjacent_y] == ' ':
return x, adjacent_y
def turn(self, opponent: Player):
while True:
if self.mode == 'hard' and self.last_hit:
location = self.hard_mode()
else:
location = random.choice(self.opponent_available_places)
self.opponent_available_places.remove(location)
result = opponent.hit(location)
if result == 'miss':
self.opponent_board[location] = 'M'
print('Opponent missed!\n')
return result
elif result == 'sank':
self.last_hit = None
self.opponent_board[location] = 'X'
print('Opponent sank your battleship!\n')
print(opponent.player_board)
elif result == 'hit':
self.last_hit = location
self.opponent_board[location] = 'X'
print('Opponent hit your battleship!\n')
print(opponent.player_board)
else:
return result