-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
280 lines (246 loc) · 7.49 KB
/
main.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
from random import randrange
import random
# Function to check if a boat placement is valid
def check_ok(boat, taken):
boat.sort()
for i in range(len(boat)):
num = boat[i]
if num in taken:
boat = [-1]
break
elif num < 0 or num > 99:
boat = [-1]
break
elif num % 10 == 9 and i < len(boat) - 1:
if boat[i + 1] % 10 == 0:
boat = [-1]
break
if i != 0:
if boat[i] != boat[i - 1] + 1 and boat[i] != boat[i - 1] + 10:
boat = [-1]
break
return boat
# Function to get user input for placing their ship
def get_ship(long, taken):
ok = True
while ok:
ship = []
# ask the user to enter numbers
print("enter your ship of length ", long)
for i in range(long):
boat_num = input("please enter a number")
ship.append(int(boat_num))
# check that ship
ship = check_ok(ship, taken)
if ship[0] != -1:
taken = taken + ship
break
else:
print("error - please try again")
return ship, taken
# Function to create ships for the player
def create_ships(taken, boats):
ships = []
# boats = [5,4,3,3,2,2]
for boat in boats:
ship, taken = get_ship(boat, taken)
ships.append(ship)
return ships, taken
# Function to check if a boat placement is valid based on start position and direction
def check_boat(b, start, dirn, taken):
boat = []
if dirn == 1:
for i in range(b):
boat.append(start - i * 10)
elif dirn == 2:
for i in range(b):
boat.append(start + i)
elif dirn == 3:
for i in range(b):
boat.append(start + i * 10)
elif dirn == 4:
for i in range(b):
boat.append(start - i)
boat = check_ok(boat, taken)
return boat
# Function to create computer-generated ships
def create_boats(taken, boats):
ships = []
# boats = [5,4,3,3,2,2]
for b in boats:
boat = [-1]
while boat[0] == -1:
boat_start = randrange(99)
boat_direction = randrange(1, 4)
# print(b,boat_start,boat_direction)
boat = check_boat(b, boat_start, boat_direction, taken)
ships.append(boat)
taken = taken + boat
# print(ships)
return ships, taken
# Function to display the game board for the computer's placement
def show_board_c(taken):
print(" battleships ")
print(" 0 1 2 3 4 5 6 7 8 9")
place = 0
for x in range(10):
row = ""
for y in range(10):
ch = " _ "
if place in taken:
ch = " o "
row = row + ch
place = place + 1
print(x, " ", row)
# Function to get a computer-generated shot
def get_shot_comp(guesses, tactics):
ok = "n"
while ok == "n":
try:
if len(tactics) > 0:
shot = tactics[0]
else:
shot = randrange(99)
if shot not in guesses:
ok = "y"
guesses.append(shot)
break
except:
print("incorrect entry - please enter again")
return shot, guesses
# Function to display the game board with hits, misses, and computer's shots
def show_board(hit, miss, comp):
print(" battleships ")
print(" 0 1 2 3 4 5 6 7 8 9")
place = 0
for x in range(10):
row = ""
for y in range(10):
ch = " _ "
if place in miss:
ch = " x "
elif place in hit:
ch = " o "
elif place in comp:
ch = " O "
row = row + ch
place = place + 1
print(x, " ", row)
# Function to process a shot and update game variables
def check_shot(shot, ships, hit, miss, comp):
missed = 0
for i in range(len(ships)):
if shot in ships[i]:
ships[i].remove(shot)
if len(ships[i]) > 0:
hit.append(shot)
missed = 1
else:
comp.append(shot)
missed = 2
if missed == 0:
miss.append(shot)
return ships, hit, miss, comp, missed
# Function to calculate tactics for computer's shots
def calc_tactics(shot, tactics, guesses, hit):
temp = []
if len(tactics) < 1:
temp = [shot - 1, shot + 1, shot - 10, shot + 10]
else:
if shot - 1 in hit:
temp = [shot + 1]
for num in [2, 3, 4, 5, 6, 7, 8]:
if shot - num not in hit:
temp.append(shot - num)
break
elif shot + 1 in hit:
temp = [shot - 1]
for num in [2, 3, 4, 5, 6, 7, 8]:
if shot + num not in hit:
temp.append(shot + num)
break
if shot - 10 in hit:
temp = [shot + 10]
for num in [20, 30, 40, 50, 60, 70, 80]:
if shot - num not in hit:
temp.append(shot - num)
break
elif shot + 10 in hit:
temp = [shot - 10]
for num in [20, 30, 40, 50, 60, 70, 80]:
if shot + num not in hit:
temp.append(shot + num)
break
# tactics longer
cand = []
for i in range(len(temp)):
if temp[i] not in guesses and temp[i] < 100 and temp[i] > -1:
cand.append(temp[i])
random.shuffle(cand)
return cand
# Function to get a user's shot
def get_shot(guesses):
ok = "n"
while ok == "n":
try:
shot = input("please enter your guess")
shot = int(shot)
if shot < 0 or shot > 99:
print("incorrect number, please try again")
elif shot in guesses:
print("incorrect number, used before")
else:
ok = "y"
break
except:
print("incorrect entry - please enter again")
return shot
# Function to check if all lists in a list are empty
def check_if_empty_2(list_of_lists):
return all([not elem for elem in list_of_lists])
# before game
hit1 = []
miss1 = []
comp1 = []
guesses1 = []
missed1 = 0
tactics1 = []
taken1 = []
taken2 = []
hit2 = []
miss2 = []
comp2 = []
guesses2 = []
missed2 = 0
tactics2 = []
battleships = [5, 4, 3, 3, 2, 2]
# game amount of ships
# computer creates a board for player 1
ships1, taken1 = create_boats(taken1, battleships)
# user creates the board for player 2 - show board
ships2, taken2 = create_ships(taken2, battleships)
show_board_c(taken2)
# loop
for i in range(80):
# player shoots
guesses1 = hit1 + miss1 + comp1
shot1 = get_shot(guesses1)
ships1, hit1, miss1, comp1, missed1 = check_shot(shot1, ships1, hit1, miss1, comp1)
show_board(hit1, miss1, comp1)
# repeat until ships empty
if check_if_empty_2(ships1):
print("end of game - winner in", i)
break
# computer shoots
shot2, guesses2 = get_shot_comp(guesses2, tactics2)
ships2, hit2, miss2, comp2, missed2 = check_shot(shot2, ships2, hit2, miss2, comp2)
show_board(hit2, miss2, comp2)
if missed2 == 1:
tactics2 = calc_tactics(shot2, tactics2, guesses2, hit2)
elif missed2 == 2:
tactics2 = []
elif len(tactics2) > 0:
tactics2.pop(0)
if check_if_empty_2(ships2):
print("end of game - computer wins", i)
break