-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattleships.py
347 lines (288 loc) · 10.5 KB
/
battleships.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#see the readme.md file for description and data from typing import Any, Union, Tuple, List
import random
from tkinter import *
from tkinter import ttk
shots = 0 #global variable to count the total number of shots
def ship_position(ship): #returns a list of tuples giving all coordinates of a ship
ship_pos = [(ship[0], ship[1])]
if ship[2] == True:
for i in range(1, ship[3]):
ship_pos.append((ship[0], ship[1] + i))
elif ship[2] == False:
for i in range(1, ship[3]):
ship_pos.append((ship[0] + i, ship[1]))
return ship_pos
def is_sunk(ship):
if ship[3] == len(ship[4]):
return True
else:
return False
def ship_type(ship):
if ship[3] == 4:
return "battleship"
elif ship[3] == 3:
return "cruiser"
elif ship[3] == 2:
return "destroyer"
else:
return "submarine"
def is_open_sea(row, column, fleet):
if (row > 9 or row < 0) or (column > 9 or column < 0):
return False
else:
for ship in fleet:
ship_pos = ship_position(ship)
for pos in ship_pos:
if row == pos[0]:
if column == pos[1] or column == pos[1]+1 or column == pos[1]-1:
return False
if row == pos[0]-1:
if column == pos[1] or column == pos[1] + 1 or column == pos[1] - 1:
return False
if row == pos[0]+1:
if column == pos[1] or column == pos[1]+1 or column == pos[1]-1:
return False
return True
def ok_to_place_ship_at(row, column, horizontal, length, fleet):
hits = set()
tempship = (row, column, horizontal, length, hits)
ok = True
ship_pos = ship_position(tempship)
for pos in ship_pos:
if is_open_sea(pos[0], pos[1], fleet) == False:
ok = False
if ok == True:
return True
else:
return False
def place_ship_at(row, column, horizontal, length, fleet):
hits = set()
new_ship = (row, column, horizontal, length, hits)
fleet.append(new_ship)
def randomly_place_all_ships():
fleet = []
finished = False
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
#battleship
while finished == False:
if ok_to_place_ship_at(row, col, horiz, 4, fleet) == True:
place_ship_at(row,col,horiz,3,fleet)
finished = True
else:
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
finished = False
#re-randomize the values
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
#cruisers
while finished == False:
if ok_to_place_ship_at(row, col, horiz, 3, fleet) == True:
place_ship_at(row,col,horiz,3,fleet)
finished = True
else:
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
finished = False
# re-randomize the values
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
while finished == False:
if ok_to_place_ship_at(row, col, horiz, 3, fleet) == True:
place_ship_at(row,col,horiz,3,fleet)
finished = True
else:
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
finished = False
# re-randomize the values
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
#destroyers
while finished == False:
if ok_to_place_ship_at(row, col, horiz, 2, fleet) == True:
place_ship_at(row,col,horiz,2,fleet)
finished = True
else:
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
finished = False
# re-randomize the values
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
while finished == False:
if ok_to_place_ship_at(row, col, horiz, 2, fleet) == True:
place_ship_at(row,col,horiz,2,fleet)
finished = True
else:
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
finished = False
# re-randomize the values
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
while finished == False:
if ok_to_place_ship_at(row, col, horiz, 2, fleet) == True:
place_ship_at(row,col,horiz,2,fleet)
finished = True
else:
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
finished = False
# re-randomize the values
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
#submarines
while finished == False:
if ok_to_place_ship_at(row, col, horiz, 1, fleet) == True:
place_ship_at(row,col,horiz,1,fleet)
finished = True
else:
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
finished = False
# re-randomize the values
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
while finished == False:
if ok_to_place_ship_at(row, col, horiz, 1, fleet) == True:
place_ship_at(row,col,horiz,1,fleet)
finished = True
else:
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
finished = False
# re-randomize the values
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
while finished == False:
if ok_to_place_ship_at(row, col, horiz, 1, fleet) == True:
place_ship_at(row,col,horiz,1,fleet)
finished = True
else:
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
finished = False
# re-randomize the values
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
while finished == False:
if ok_to_place_ship_at(row, col, horiz, 1, fleet) == True:
place_ship_at(row,col,horiz,1,fleet)
finished = True
else:
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
finished = False
# re-randomize the values
row = random.randint(0, 10)
col = random.randint(0, 10)
horiz = random.choice([True, False])
return fleet
def check_if_hits(row, column, fleet):
hit = False
check_hit = (row, column)
for ship in fleet:
ship_pos = ship_position(ship)
for pos in ship_pos:
if check_hit == pos:
hit = True
if hit == True:
return True
else:
return False
def hit(row, column, fleet):
check_hit = (row, column)
for ship in fleet:
ship_pos = ship_position(ship)
for pos in ship_pos:
if check_hit == pos:
ship[4].add(check_hit)
if ship[3] == len(ship[4]):
print("You sank a " + ship_type(ship))
if are_unsunk_ships_left(fleet) == False:
print("You win! Total shots = " + str(shots))
return (fleet, ship)
def are_unsunk_ships_left(fleet):
sunk_count = 0
for ship in fleet:
if is_sunk(ship) == True:
sunk_count +=1
if len(fleet) == sunk_count:
return False
else:
return True
#the following 3 functions are all needed for createbuttons()
def labelx(t, c, r, b):
ttk.Label(b, text=t).grid(column=c, row=r, sticky=W, padx=5)
def labely(t, c, r, b):
ttk.Label(b, text=t).grid(column=c, row=r, sticky=W)
def shoot(r, c, fleet, b):
global shots
if check_if_hits(r, c, fleet) == True:
print("You hit!")
hit(r, c, fleet) #the hit function then deals with checking if a ship has been sunk and communicating that
b.configure(bg="green")
shots +=1
else:
print("You missed")
b.configure(bg="red")
shots += 1
def createbuttons(board, tempfleet): #this creates the axis and the button grid
for i in range(0, 10):
labelx(i, i + 2, 1, board)
for j in range(0, 10):
labely(j, 1, j + 2, board)
for i in range(2, 12):
for j in range(2, 12):
btn = Button(board)
btn.config(width=3, command=lambda r=i-2, c=j-2, fleet=tempfleet, b=btn: shoot(r, c, fleet, b)) #this feeds the button corrdinates into shoot() when the button is clicked
btn.grid(column=i, row=j)
def quitter(): #this is needed for the quit button
sys.exit()
def main():
current_fleet = randomly_place_all_ships()
#the following sets up the board
root = Tk()
title = ttk.Label(root)
title.configure(text="Battleships", anchor="center")
title.grid(column = 1, row = 1)
subtitle = ttk.Label(root)
subtitle.configure(text="Click on a square to shoot!", anchor="center")
subtitle.grid(column=1, row=2)
board = ttk.Frame(root, padding="5 5 5 5")
board.grid(column=1, row=3, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
#each square is a button, created using loops
createbuttons(board, current_fleet)
#this creates the lower portion of the board and the quit button
scores = ttk.Frame(root, padding="5 5 5 5")
scores.grid(column=1, row=4, sticky=(W, E))
quit_button = Button(scores)
quit_button.configure(text="Quit", bg="red", command=quitter)
quit_button.grid(column=1, row=1)
root.mainloop()
if __name__ == '__main__': #keep this in
main()