-
Notifications
You must be signed in to change notification settings - Fork 0
/
computer.py
152 lines (143 loc) · 8.57 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import random
from coordinate import Coordinate
class ComputerPlayer:
def __init__(self):
self.guesses = []
self.adjacent_coords = []
self.has_hit = False
self.checking_adjacent = False
self.adjacent_hit = False
self.last_rand_guess_loc = None
self.adjacent_hit_loc = None
def create_computer_ships(self, ship_size, computer_ship_instance, dict_name):
ship_start_loc = (random.randint(0, 9), random.randint(0, 9))
orientation = random.randint(1, 4)
if orientation == 1:
ship_location = Coordinate.coords_up(ship_start_loc, ship_size, 10)
if computer_ship_instance.check_all_locs(ship_location) or len(ship_location) != ship_size:
self.create_computer_ships(ship_size, computer_ship_instance, dict_name)
else:
computer_ship_instance.place_ship(ship_location, dict_name)
elif orientation == 2:
ship_location = Coordinate.coords_down(ship_start_loc, ship_size, 10)
if computer_ship_instance.check_all_locs(ship_location) or len(ship_location) != ship_size:
self.create_computer_ships(ship_size, computer_ship_instance, dict_name)
else:
computer_ship_instance.place_ship(ship_location, dict_name)
elif orientation == 3:
ship_location = Coordinate.coords_right(ship_start_loc, ship_size, 10)
if computer_ship_instance.check_all_locs(ship_location) or len(ship_location) != ship_size:
self.create_computer_ships(ship_size, computer_ship_instance, dict_name)
else:
computer_ship_instance.place_ship(ship_location, dict_name)
elif orientation == 4:
ship_location = Coordinate.coords_left(ship_start_loc, ship_size, 10)
if computer_ship_instance.check_all_locs(ship_location) or len(ship_location) != ship_size:
self.create_computer_ships(ship_size, computer_ship_instance, dict_name)
else:
computer_ship_instance.place_ship(ship_location, dict_name)
def computer_guess(self, player_ship_instance, player_board_instance, computer_instance):
if self.has_hit:
if self.checking_adjacent:
self.comp_get_adj_guess(player_ship_instance, player_board_instance, computer_instance)#behaviour after random guess hit, if next guess out of adjacent coords is a miss.
else:
if self.adjacent_hit:
self.comp_found_adj_hit_guess(player_ship_instance, player_board_instance, computer_instance)#behaviour if adjacent coord is also a hit, finds orientation of ship to guide further guesses.
else:
self.comp_find_adj_hit_guess(player_ship_instance, player_board_instance, computer_instance)#behaviour after a random guess hits, finds adjacent coords to hit and choses one.
else:
self.rand_comp_guess(player_ship_instance, player_board_instance, computer_instance)#default behaviour, a random guess which cannot be a previusly guessed coord.
def rand_comp_guess(self, player_ship_instance, player_board_instance, computer_instance):
comp_guess = (random.randint(0, 9), random.randint(0, 9))
if comp_guess in self.guesses:
return self.computer_guess(player_ship_instance, player_board_instance, computer_instance)
else:
self.guesses.append(comp_guess)
output_guess = Coordinate.index_to_user(comp_guess)
if player_ship_instance.does_this_hit(comp_guess):
self.last_rand_guess_loc = comp_guess
print(f'The computer has hit your ship at {output_guess}')
player_board_instance.update_board(comp_guess, 'X')
sunk_ship = player_ship_instance.has_ship_sunk(computer_instance)
if sunk_ship:
sunk_ship = (sunk_ship[0]).split(' ', 1)
print(f'The computer has sunk your {sunk_ship[0]}!')
else:
self.has_hit = True
else:
print(f'The computer missed your ships shooting at {output_guess}')
player_board_instance.update_board(comp_guess, 'O')
self.has_hit = False
def comp_find_adj_hit_guess(self, player_ship_instance, player_board_instance, computer_instance):
self.adjacent_coords = Coordinate.all_adjacent(self.last_rand_guess_loc, 10)#make possible adjacent coords on board
avaliable_adjacent_coords = [x for x in self.adjacent_coords if x not in self.guesses]#filter out guessed adjacent
comp_guess = random.choice(avaliable_adjacent_coords)
self.guesses.append(comp_guess)
output_guess = Coordinate.index_to_user(comp_guess)
if player_ship_instance.does_this_hit(comp_guess):#check if rand guess from adjacent coords hits
print(f'The computer has hit your ship at {output_guess}')
player_board_instance.update_board(comp_guess, 'X')
sunk_ship = player_ship_instance.has_ship_sunk(computer_instance)
if sunk_ship:
sunk_ship = (sunk_ship[0]).split(' ', 1)
print(f'The computer has sunk your {sunk_ship[0]}!')
self.has_hit = False
else:
self.adjacent_hit_loc = comp_guess
self.adjacent_hit = True
else:
print(f'The computer missed your ships shooting at {output_guess}')
player_board_instance.update_board(comp_guess, 'O')
self.checking_adjacent = True
def comp_found_adj_hit_guess(self, player_ship_instance, player_board_instance, computer_instance):
orientation = Coordinate.find_orientation(self.last_rand_guess_loc, self.adjacent_hit_loc)
coords1 = Coordinate.dir_adjacent(orientation, self.last_rand_guess_loc, 10)
coords2 = Coordinate.dir_adjacent(orientation, self.adjacent_hit_loc, 10)
adj_coords = [x for x in coords1 + coords2 if x not in self.guesses]
if adj_coords:
comp_guess= random.choice(adj_coords)
self.guesses.append(comp_guess)
output_guess = Coordinate.index_to_user(comp_guess)
if player_ship_instance.does_this_hit(comp_guess):#check if rand guess from adjacent coords hits
print(f'The computer has hit your ship at {output_guess}')
player_board_instance.update_board(comp_guess, 'X')
sunk_ship = player_ship_instance.has_ship_sunk(computer_instance)
if sunk_ship:
sunk_ship = (sunk_ship[0]).split(' ', 1)
print(f'The computer has sunk your {sunk_ship[0]}!')
self.has_hit = False
self.adjacent_hit = False
else:
self.adjacent_hit_loc = comp_guess
else:
print(f'The computer missed your ships shooting at {output_guess}')
player_board_instance.update_board(comp_guess, 'O')
else:
self.adjacent_hit = False
self.checking_adjacent = True
return self.computer_guess(player_ship_instance, player_board_instance, computer_instance)
def comp_get_adj_guess(self, player_ship_instance, player_board_instance, computer_instance):
avaliable_adjacent_coords = [x for x in self.adjacent_coords if x not in self.guesses]
if avaliable_adjacent_coords:
comp_guess = random.choice(avaliable_adjacent_coords)
self.guesses.append(comp_guess)
output_guess = Coordinate.index_to_user(comp_guess)
if player_ship_instance.does_this_hit(comp_guess):
print(f'The computer has hit your ship at {output_guess}')
player_board_instance.update_board(comp_guess, 'X')
self.checking_adjacent = False
sunk_ship = player_ship_instance.has_ship_sunk(computer_instance)
if sunk_ship:
sunk_ship = (sunk_ship[0]).split(' ', 1)
print(f'The computer has sunk your {sunk_ship[0]}!')
self.has_hit = False
else:
self.adjacent_hit_loc = comp_guess
self.adjacent_hit = True
else:
print(f'The computer missed your ships shooting at {output_guess}')
player_board_instance.update_board(comp_guess, 'O')
else:
self.has_hit = False
self.checking_adjacent = False
return self.computer_guess(player_ship_instance, player_board_instance, computer_instance)