-
Notifications
You must be signed in to change notification settings - Fork 10
/
Battleship3.py
285 lines (268 loc) · 11 KB
/
Battleship3.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from random import randint
import copy
#Constants and globals
OCEAN = "O"
FIRE = "X"
HIT = "*"
SIZE = 10
SHIPS = [5, 4, 3, 3, 2]
#globals
orientation = -1 # Stores the hit ship orientation. Determined on second hit
total_hits = [] # Stores the ship number every time AI hits a ship while ship is afloat
miss = 1 # Stores whether last AI shot was a miss
# Player variables
player_alive = 17 # -1 every time a ship is hit
player_radar = []
player_board = []
# AI variables
ai_alive = 17
ai_radar = []
ai_board = []
ship_position = [] # Stores the first hit of ships which will be eliminated [row, col]
ship_length = [] # Stores the length of ship on first hit
#Set up variables
SEA = [] # Blank Board
for x in range(SIZE):
SEA.append([OCEAN] * SIZE)
#Functions
def print_board():
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#print(numbers.join(""))
print(" 0 1 2 3 4 5 6 7 8 9 || 0 1 2 3 4 5 6 7 8 9")
i = 0
for row in range(SIZE):
print(i, " ".join(player_radar[row]), "||" , " ".join(player_board[row]))
i += 1
def random_row(is_vertical, size):
if is_vertical:
return randint(0, SIZE - size)
else:
return randint(0, SIZE -1)
def random_col(is_vertical, size):
if is_vertical:
return randint(0, SIZE - 1)
else:
return randint(size-1, SIZE -1)
def is_ocean(row, col, b): # true if ocean
if row < 0 or row >= SIZE:
return 0
elif col < 0 or col >= SIZE:
return 0
if b[row][col] == OCEAN:
return 1
else:
return 0
def is_oceanin(row,col,b):
if type(row) is not int or type(col) is not int:
return 0
if row < 0 or row >= SIZE:
return 0
elif col < 0 or col >= SIZE:
return 0
if b[row][col] == OCEAN:
return 1
else:
return 0
def make_ship(size, board, set_ship = None):
# Find an unoccupied spot, then place ship on board
# Put set_ship on ship_number_board if set_ship
is_vertical = randint(0, 1) # vertical ship if true
occupied = True
while(occupied):
occupied = False
ship_row = random_row(is_vertical, size)
ship_col = random_col(is_vertical, size)
if is_vertical:
for p in range(size):
if not is_ocean(ship_row+p, ship_col, board):
occupied = True
else:
for p in range(size):
if not is_ocean(ship_row, ship_col-p, board):
occupied = True
#Place ship on boards
if is_vertical:
board[ship_row][ship_col] = "^"
board[ship_row+size-1][ship_col] = "v"
if set_ship != None:
number_board[ship_row][ship_col] = set_ship
number_board[ship_row+size-1][ship_col] = set_ship
for p in range(size -2):
board[ship_row+p+1][ship_col] = "+"
if set_ship != None:
number_board[ship_row+p+1][ship_col] = set_ship
else:
board[ship_row][ship_col] = ">"
board[ship_row][ship_col-size+1] = "<"
if set_ship != None:
number_board[ship_row][ship_col] = set_ship
number_board[ship_row][ship_col-size+1] = set_ship
for p in range(size -2):
board[ship_row][ship_col-p-1] = "+"
if set_ship != None:
number_board[ship_row][ship_col-p-1] = set_ship
return board
def ship_number(r, c):
# Returns -1 if not found
if is_ocean(r, c, number_board):
return -1
#print("ship_number() returning: ", str(SHIPS[number_board[r][c]]))
return SHIPS[number_board[r][c]]
def ship_sunk(): # true if ship sunk
#print("Total Hits: ", str(total_hits.count(ship_length[0]), ":", str(ship_length[2]))
#for item in number_board:
# print(item[0], ' '.join(map(str, item[1:])))
if total_hits.count(total_hits[0]) == ship_length[0]:
return 1
return 0
# Make the boards
player_radar = copy.deepcopy(SEA)
player_board = copy.deepcopy(SEA)
ai_radar = copy.deepcopy(SEA)
ai_board = copy.deepcopy(SEA)
number_board = copy.deepcopy(SEA)
for x in range(len(SHIPS)):
player_board = make_ship(SHIPS[x], player_board, x)
ai_board = make_ship(SHIPS[x], ai_board)
print("Let's play Battleship!")
print_board()
while player_alive and ai_alive:
# player turn
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
while not is_oceanin(guess_row, guess_col, player_radar):
print("Sorry, that is not a valid shot")
guess_row = int(input("Guess Row:"))
guess_col = int(input("Guess Col:"))
# Legal Guess
if ai_board[guess_row][guess_col] != OCEAN:
ai_alive -= 1
if ai_alive:
print("Admiral, we've hit the enemy ship!")
player_radar[guess_row][guess_col] = HIT
else:
player_radar[guess_row][guess_col] = HIT
print("Congratulations! You sunk my battleship!")
break
else:
print("Admiral, we've missed the enemy battleship!")
player_radar[guess_row][guess_col] = FIRE
# AI turn
print("target orientation: ", orientation)
if not len(ship_length): # No current targets
#print("No Current Targets")
second_shot = 0
ai_guess_row = randint(0, SIZE-1)
ai_guess_col = randint(0, SIZE-1)
while not is_ocean(ai_guess_row, ai_guess_col, ai_radar):
ai_guess_row = randint(0, SIZE-1)
ai_guess_col = randint(0, SIZE-1)
if not is_ocean(ai_guess_row, ai_guess_col, player_board): # AI hit
miss = 0
player_alive -= 1
#print("Hit ship length: ", ship_number(ai_guess_row, ai_guess_col))
ship_length.append((ship_number(ai_guess_row, ai_guess_col)))
#print("ship_position length: ", str(len(ship_position)))
ship_position.extend([ai_guess_row, ai_guess_col])
#print("ship_position length: ", str(len(ship_position)))
orientation = -1
player_board[ai_guess_row][ai_guess_col] = HIT
ai_radar[ai_guess_row][ai_guess_col] = HIT
total_hits.append(number_board[ai_guess_row][ai_guess_col])
print(("Attenton Admiral! You have been hit!"))
else:
miss = 1
player_board[ai_guess_row][ai_guess_col] = FIRE
ai_radar[ai_guess_row][ai_guess_col] = FIRE
print("Good news! They've missed!")
else: # Find next spot to shoot on ship
#print("Current Targets: ", " ".join(map(str, ship_length)),":", " ".join(map(str,total_hits)))
#print("Last shot was a miss: ", miss)
if orientation == -1: # shot-test for orientation of hit ship
#ship_position[ swapped for ai_hit_
print("Ship has no orientation")
if is_ocean(ship_position[0]+1, ship_position[1], ai_radar):
ai_guess_row = ship_position[0]+1
ai_guess_col = ship_position[1]
elif is_ocean(ship_position[0]-1, ship_position[1], ai_radar):
ai_guess_row = ship_position[0]-1
ai_guess_col = ship_position[1]
elif is_ocean(ship_position[0], ship_position[1]-1, ai_radar):
ai_guess_row = ship_position[0]
ai_guess_col = ship_position[1]-1
else:
ai_guess_row = ship_position[0]
ai_guess_col = ship_position[1]+1
elif orientation: # Shoot at verticle ship
#print("Previous Guess: ", ai_guess_row, ":", ai_guess_col)
for item in ai_radar:
print(item[0], ' '.join(map(str, item[1:])))
if is_ocean(ai_guess_row+1, ai_guess_col, ai_radar) and not miss:
ai_guess_row += 1
else:
#print("Adjusting guess to lower row number")
ai_guess_row -= 1
while not is_ocean(ai_guess_row, ai_guess_col, ai_radar): # not is important here
ai_guess_row -= 1
#print("New Guess: ", ai_guess_row, ":", ai_guess_col)
else: # Shoot at horizontal ship
#print("Previous Guess: ", ai_guess_row, ":", ai_guess_col)
for item in ai_radar:
print(item[0], ' '.join(map(str, item[1:])))
if is_ocean(ai_guess_row, ai_guess_col-1, ai_radar) and not miss:
ai_guess_col = ai_guess_col-1
else:
#print("Adjusting guess to higher col number")
ai_guess_col = ai_guess_col+1
while not is_ocean(ai_guess_row, ai_guess_col, ai_radar):
ai_guess_col += 1
#print("New Guess: ", ai_guess_row, ":", ai_guess_col)
# Set boards after shots
if not is_ocean(ai_guess_row, ai_guess_col, player_board):
#number_board[ai_guess_row][ai_guess_col] = OCEAN
#print("Setting Board: ", ai_guess_row, ":", ai_guess_col)
player_board[ai_guess_row][ai_guess_col] = HIT
ai_radar[ai_guess_row][ai_guess_col] = HIT
total_hits.append(number_board[ai_guess_row][ai_guess_col])
#ship_position.extend([ai_guess_row, ai_guess_col])
player_alive -= 1
#if second_shot: # set orientation
#print("DEBUG: ", total_hits.count(total_hits[0]), ship_number(ai_guess_row, ai_guess_col), ship_number(ship_position[0], ship_position[1]))
if total_hits.count(total_hits[0]) == 2 and ship_number(ai_guess_row, ai_guess_col) == ship_number(ship_position[0], ship_position[1]):
if ai_guess_col != ship_position[1]:
orientation = 0
else:
orientation = 1
print("New Orientation: ", orientation)
elif total_hits[0] != number_board[ai_guess_row][ai_guess_col]: # Other ship was shot
ship_length.append((ship_number(ai_guess_row, ai_guess_col)))
ship_position.extend([ai_guess_row, ai_guess_col])
if player_alive:
miss = 0
print("Attenton Admiral! You have been hit!")
else:
print("I'm sorry sir, but we're going down")
print_board()
break
else: # AI missed
#print("DEBUG: r,c: ", ai_guess_row, ", ", ai_guess_col)
miss = 1
player_board[ai_guess_row][ai_guess_col] = FIRE
ai_radar[ai_guess_row][ai_guess_col] = FIRE
print("Good news! They've missed!")
if ship_sunk(): # Reset variables
#print("Ship sunk")
orientation = -1
ship_position.pop(0)
ship_position.pop(0)
ship_length.pop(0)
t = total_hits[0]
for x in range(total_hits.count(t)):
total_hits.remove(t)
#print("Targets after sinking: ", " ".join(map(str, ship_length)),":", " ".join(map(str,total_hits)))
if len(ship_length) != 0:
miss = 0
else:
miss = 1
#print("ship_position list: ", " ".join(map(str, ship_position)))
print_board()