-
Notifications
You must be signed in to change notification settings - Fork 0
/
Battleship.py
324 lines (281 loc) · 11 KB
/
Battleship.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
#Battleship game!!!#
from random import randint
# JUI for the ship
player_Board = [['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], [
'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
bot_Board = [['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], [
'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O']]
def printBoard(board):
counterx = 0
countery = 0
while counterx < 5:
countery = 0
print("")
while countery < 5:
print(board[counterx][countery], end=" ")
countery += 1
counterx += 1
def placePiece(board, x, y, piece):
int(x)
int(y)
x -= 1
y -= 1
board[x][y] = piece
def bot_locations(): # Gets bot's ships' locations#
global bot_ship_1
global bot_ship_2
global bot_ship_3
bot_ship_1 = {"row": randint(1, 5), "col": randint(1, 5)}
bot_ship_2 = {"row": randint(1, 5), "col": randint(1, 5)}
bot_ship_3 = {"row": randint(1, 5), "col": randint(1, 5)}
#Following lines check if ships overlap#
if bot_ship_1["row"] == bot_ship_2[
"row"] and bot_ship_1["col"] == bot_ship_2["col"]:
bot_locations()
if bot_ship_1["row"] == bot_ship_3[
"row"] and bot_ship_1["col"] == bot_ship_3["col"]:
bot_locations()
if bot_ship_2["row"] == bot_ship_3[
"row"] and bot_ship_2["col"] == bot_ship_3["col"]:
bot_locations()
def player_auto(): # automatically gives player's ships' locations#
global player_ship_1
global player_ship_2
global player_ship_3
player_ship_1 = {"row": randint(1, 5), "col": randint(1, 5)}
player_ship_2 = {"row": randint(1, 5), "col": randint(1, 5)}
player_ship_3 = {"row": randint(1, 5), "col": randint(1, 5)}
if player_ship_1["row"] == player_ship_2[
"row"] and player_ship_1["col"] == player_ship_2["col"]:
player_auto()
if player_ship_1["row"] == player_ship_3[
"row"] and player_ship_1["col"] == player_ship_3["col"]:
player_auto()
if player_ship_2["row"] == player_ship_3[
"row"] and player_ship_2["col"] == player_ship_3["col"]:
player_auto()
ship_1x = player_ship_1["row"]
ship_2x = player_ship_2["row"]
ship_3x = player_ship_3["row"]
ship_1y = player_ship_1["col"]
ship_2y = player_ship_2["col"]
ship_3y = player_ship_3["col"]
placePiece(player_Board, ship_1x, ship_1y, "S")
placePiece(player_Board, ship_2x, ship_2y, "S")
placePiece(player_Board, ship_3x, ship_3y, "S")
def player_manual():
global player_ship_1
global player_ship_2
global player_ship_3
player_ship_1 = {
"row": int(
input("Choose a row for your first ship(1-5)")),
"col": int(
input("Choose a coloum for your first ship: "))}
if player_ship_1["row"] not in range(1, 6):
print("Invalid input. Please input location again: ")
player_manual()
player_ship_2 = {
"row": int(
input("Choose a row for your second ship(1-5)")),
"col": int(
input("Choose a coloum for your second ship: "))}
if player_ship_2["row"] not in range(1, 6):
print("Invalid input. Please input location again: ")
player_manual()
if player_ship_1["row"] == player_ship_2[
"row"] and player_ship_1["col"] == player_ship_2["col"]:
print("Invalid input. Ships can't overlap.")
player_manual()
player_ship_3 = {
"row": int(
input("Choose a row for your third ship(1-5)")),
"col": int(
input("Choose a coloum for your third ship: "))}
if player_ship_3["row"] not in range(1, 6):
print("Invalid input. Please input location again: ")
player_manual()
if (player_ship_1["row"] == player_ship_3["row"]) and (
player_ship_1["col"] == player_ship_3["col"]):
print("Invalid input. Ships can't overlap.")
player_manual()
if (player_ship_2["row"] == player_ship_3["row"]) and (
player_ship_2["col"] == player_ship_3["col"]):
print("Invalid input. Ships can't overlap.")
player_manual()
ship_1x = player_ship_1["row"]
ship_2x = player_ship_2["row"]
ship_3x = player_ship_3["row"]
ship_1y = player_ship_1["col"]
ship_2y = player_ship_2["col"]
ship_3y = player_ship_3["col"]
placePiece(player_Board, ship_1x, ship_1y, "S")
placePiece(player_Board, ship_2x, ship_2y, "S")
placePiece(player_Board, ship_3x, ship_3y, "S")
"""
def check_int(x):
try:
int(x)
except ValueError:
print("Invalid Input. Only numbers.")
x=input("Input again")
check_int(x)
if x not in range(1,6):
print("Invalid Input. Only numbers from 1 to 5")
x=input("Input again")
check_int(x)
"""
# Whenever player has to input an integer, this should be used. Catches errors.
def input_int():
x = input("")
try:
int(x)
except ValueError:
print("Invalid. Only integer numbers are accepted!")
print("Try again: ")
x = input_int()
x = int(x)
return x
def player_fire():
global player_fired_cols
global player_fired_rows
global player_fired_no
global fire_col
global fire_row
print("Choose a row to fire upon: ")
fire_row = input_int()
if fire_row in range(1, 6):
print("Choose a coloum to fire upon: ")
fire_col = input_int()
if fire_col in range(1, 6):
check = 1
#Checks if player has already fired there#
for i in range(0, len(player_fired_rows)):
if (player_fired_rows[i] == fire_row) and (
player_fired_cols[i] == fire_col):
check = 0
break
if check == 1:
player_fired_cols.append(fire_col)
player_fired_rows.append(fire_row)
else:
print("You have already fired there! Try again")
player_fire()
else:
print("Invalid Input. Only numbers from 1 to 5 are accepted!")
player_fire()
else:
print("Invalid Input. Only numbers from 1 to 5 are accepted!")
player_fire()
def bot_fire():
global bot_fired_rows
global bot_fired_cols
global bot_fired_no
global fire_row
global fire_col
fire_row = randint(1, 5)
fire_col = randint(1, 5)
#Checks if bot has already fired there#
for i in range(0, len(bot_fired_rows)):
if (bot_fired_rows[i] == fire_row) and (bot_fired_cols[i] == fire_col):
bot_fire()
bot_fired_cols.append(fire_col)
bot_fired_rows.append(fire_row)
def battleship():
global bot_fired_rows
global bot_fired_cols
global player_fired_cols
global player_fired_rows
#Stores places already fired upon#
bot_fired_rows = []
bot_fired_cols = []
player_fired_rows = []
player_fired_cols = []
winner = ""
player_ship_1_status = 1
player_ship_2_status = 1
player_ship_3_status = 1
bot_ship_1_status = 1
bot_ship_2_status = 1
bot_ship_3_status = 1
print("Welcome to Battleship. ")
print("Rules:\nYou and the bot have a 5 by 5 board.\nYou can choose to place your ships yourself or let the computer do it."
"\nYou will then choose a place to fire(Between 1-5).\nThe bot will randomly fire ont your board.\nFirst to destroy all ships wins\nGood luck")
print("Here's your board: ")
printBoard(player_Board)
print("")
print("\nHere's the bot's board: ")
printBoard(bot_Board)
bot_locations()
#Print's bot's ship locations. It's not cheating, it's debugging!!!#
choice = input(
"\n\nWould you like to choose your ship location yourself?(Y/N)")
choice = choice[0].lower()
if choice != "y" and choice != "n":
print("Invalid input. Try again")
battleship()
else:
if choice == "y":
player_manual()
elif choice == "n":
player_auto()
printBoard(player_Board)
#Code to see player's ships' locations#
print("\nThese are your ships' locations: ")
print(player_ship_1)
print(player_ship_2)
print(player_ship_3)
while winner == "":
player_fire()
if fire_row == bot_ship_1["row"] and fire_col == bot_ship_1["col"]:
bot_ship_1_status = 0
print("You hit the bot's first ship!!!")
placePiece(bot_Board, fire_row, fire_col, "X")
elif fire_row == bot_ship_2["row"] and fire_col == bot_ship_2["col"]:
bot_ship_2_status = 0
print("You hit the bot's second ship!!!")
placePiece(bot_Board, fire_row, fire_col, "X")
elif fire_row == bot_ship_3["row"] and fire_col == bot_ship_3["col"]:
bot_ship_3_status = 0
print("You hit the bot's third ship!!!")
placePiece(bot_Board, fire_row, fire_col, "X")
else:
print("You missed!!!")
placePiece(bot_Board, fire_row, fire_col, "M")
if bot_ship_1_status == 0 and bot_ship_2_status == 0 and bot_ship_3_status == 0:
winner = "Player"
print("Here's the bot's board: ")
printBoard(bot_Board)
bot_fire()
"""
print("\nThis is where the bot fired: ")
print(bot_fired_rows,bot_fired_cols)
"""
if fire_row == player_ship_1[
"row"] and fire_col == player_ship_1["col"]:
player_ship_1_status = 0
print("\nThe bot hit your first ship!")
placePiece(player_Board, fire_row, fire_col, "X")
elif fire_row == player_ship_2["row"] and fire_col == player_ship_2["col"]:
player_ship_2_status = 0
print("\nThe bot hit your second ship!")
placePiece(player_Board, fire_row, fire_col, "X")
elif fire_row == player_ship_3["row"] and fire_col == player_ship_3["col"]:
player_ship_3_status = 0
print("\nThe bot hit your third ship!")
placePiece(player_Board, fire_row, fire_col, "X")
else:
print("\nThe bot missed!!!")
placePiece(player_Board, fire_row, fire_col, "M")
if player_ship_1_status == 0 and player_ship_2_status == 0 and player_ship_3_status == 0:
winner = "Bot"
print("Here's the player's board: ")
printBoard(player_Board)
print("\nThe " + winner + " wins!!!")
again = input("Want to play again?(Y/N)")
again = again[0].lower()
if again == "y":
battleship()
else:
print("Okay. Thanks for playing!!!")
battleship()