-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattleship.py
302 lines (293 loc) · 11.4 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
import random
import re
import time
from random import randint
class game:
def __init__(self):
self.cross = "X"
self.water = "≈"
self.question = "?"
self.boat = "*"
self.boat_lengths = [2, 2, 3, 3]
self.lettersToNumbers = {"A" : 1, "B" : 2, "C" : 3, "D" : 4, "E" : 5, "F" : 6}
self.gridPlayer = """ A B C D E F
+----+----+----+----+----+----+
1 | | | | | | |
+----+----+----+----+----+----+
2 | | | | | | |
+----+----+----+----+----+----+
3 | | | | | | |
+----+----+----+----+----+----+
4 | | | | | | |
+----+----+----+----+----+----+
5 | | | | | | |
+----+----+----+----+----+----+
6 | | | | | | |
+----+----+----+----+----+----+"""
self.gridBot = self.gridPlayer
self.gridBotVisible = self.gridPlayer
self.xPosIndex = {"A" : 7, "B" : 12, "C" : 17, "D" : 22, "E" : 27, "F" : 32}
def updateGrid(self, gridIdentifier, position, insert_type):
xPos = int(self.xPosIndex[position[0]])
yPos = int(position[1])*2
if gridIdentifier == 0:
temp_grid = self.gridPlayer
elif gridIdentifier == 1:
temp_grid = self.gridBot
elif gridIdentifier == 2:
temp_grid = self.gridBotVisible
temp_grid = temp_grid.splitlines()
temp_grid_line = list(temp_grid[yPos])
# del temp_grid_line[xPos]
if insert_type == 0:
temp_grid_line[xPos] = self.question
elif insert_type == 1:
temp_grid_line[xPos] = self.water
elif insert_type == 2:
temp_grid_line[xPos] = self.cross
elif insert_type == 3:
temp_grid_line[xPos] = self.boat
temp_grid[yPos] = ''.join(temp_grid_line)
temp_grid = '\n'.join(temp_grid)
if gridIdentifier == 0:
self.gridPlayer = temp_grid
elif gridIdentifier == 1:
self.gridBot = temp_grid
elif gridIdentifier == 2:
self.gridBotVisible = temp_grid
def viewLocationData(self, position):
xPos = int(self.xPosIndex[position[0]])
yPos = int(position[1])*2
temp_grid = self.gridBot
temp_grid = temp_grid.splitlines()
temp_grid_line = list(temp_grid[yPos])
return temp_grid_line[xPos]
def viewLocationDataBot(self, position):
xPos = int(self.xPosIndex[position[0]])
yPos = int(position[1])*2
temp_grid = self.gridPlayer
temp_grid = temp_grid.splitlines()
temp_grid_line = list(temp_grid[yPos])
return temp_grid_line[xPos]
def startGame(self):
user_choices = []
boat_lengths = self.boat_lengths
for i in range(len(boat_lengths)):
first_pos_valid = False
while first_pos_valid == False:
first_pos = chr(65+randint(0, 5)) + str(randint(1, 6))
locations, first_pos_valid = self.directionChoose(user_choices, first_pos, boat_lengths[i])
user_choices.append(locations)
self.user_choices = user_choices
bot_choices = []
boat_lengths = self.boat_lengths
for i in range(len(boat_lengths)):
first_pos_valid = False
while first_pos_valid == False:
first_pos = chr(65+randint(0, 5)) + str(randint(1, 6))
locations, first_pos_valid = self.directionChoose(bot_choices, first_pos, boat_lengths[i])
bot_choices.append(locations)
self.bot_choices = bot_choices
# print(bot_choices)
def directionChoose(self, locations, currentChoice, boatLength):
if (any(currentChoice in i for i in locations)):
return [], False
current_pos_y = int(currentChoice[1])
current_pos_x = int(self.lettersToNumbers[currentChoice[0]])
direction1 = [currentChoice]
direction2 = [currentChoice]
direction3 = [currentChoice]
direction4 = [currentChoice]
directions = [direction1, direction2, direction3, direction4]
valids = [False, False, False, False]
if current_pos_y-boatLength+1>0:
valids[0] = True
if current_pos_x+boatLength-1<=6:
valids[1] = True
if current_pos_y+boatLength-1<=6:
valids[2] = True
if current_pos_x-boatLength+1>0:
valids[3] = True
for k in range(boatLength-1):
if valids[0] == True:
currentLocation = directions[0][len(directions[0])-1]
currentLocationX = int(self.lettersToNumbers[currentLocation[0]])
currentLocationY = int(currentLocation[1])
newLocation = chr(64+currentLocationX) + str(currentLocationY-1)
if (any(newLocation in i for i in locations)):
valids[0] = False
else:
directions[0].append(newLocation)
if valids[1] == True:
currentLocation = directions[1][len(directions[1])-1]
currentLocationX = int(self.lettersToNumbers[currentLocation[0]])
currentLocationY = int(currentLocation[1])
newLocation = chr(64+currentLocationX+1) + str(currentLocationY)
if (any(newLocation in i for i in locations)):
valids[1] = False
else:
directions[1].append(newLocation)
if valids[2] == True:
currentLocation = directions[2][len(directions[2])-1]
currentLocationX = int(self.lettersToNumbers[currentLocation[0]])
currentLocationY = int(currentLocation[1])
newLocation = chr(64+currentLocationX) + str(currentLocationY+1)
if (any(newLocation in i for i in locations)):
valids[2] = False
else:
directions[2].append(newLocation)
if valids[3] == True:
currentLocation = directions[3][len(directions[3])-1]
currentLocationX = int(self.lettersToNumbers[currentLocation[0]])
currentLocationY = int(currentLocation[1])
newLocation = chr(64+currentLocationX-1) + str(currentLocationY)
if (any(newLocation in i for i in locations)):
valids[3] = False
else:
directions[3].append(newLocation)
options = []
if valids[0] == True:
options.append(0)
if valids[1] == True:
options.append(1)
if valids[2] == True:
options.append(2)
if valids[3] == True:
options.append(3)
if options == []:
return [], False
random_direction = random.choice(options)
direction = directions[random_direction]
return direction, True
def resetGrids(self):
self.gridPlayer = """ A B C D E F
+----+----+----+----+----+----+
1 | | | | | | |
+----+----+----+----+----+----+
2 | | | | | | |
+----+----+----+----+----+----+
3 | | | | | | |
+----+----+----+----+----+----+
4 | | | | | | |
+----+----+----+----+----+----+
5 | | | | | | |
+----+----+----+----+----+----+
6 | | | | | | |
+----+----+----+----+----+----+"""
self.gridBot = self.gridPlayer
def takeShot(self, location):
data = self.viewLocationData(location)
if data == "*" and data != "X":
self.updateGrid(2, location, 2)
self.updateGrid(1, location, 2)
return True
else:
self.updateGrid(2, location, 1)
return False
def takeShotBot(self, location):
data = self.viewLocationDataBot(location)
if data == "*" and data != "X":
self.updateGrid(0, location, 2)
return True
else:
self.updateGrid(0, location, 1)
return False
print("Welcome to battlefield! There are 4 ships of lengths 2, 2, 3 and 3. You need to sink the opponents ships before they sink yours to win!\n\n")
print("* represents a boat, X marks a hit, ≈ marks a miss\n\n")
ch = False
x = game()
gameOngoing = True
while ch == False:
x.resetGrids()
x.startGame()
for a in range(len(x.user_choices)):
for b in range(len(x.user_choices[a])):
x.updateGrid(0, x.user_choices[a][b], 3)
print(x.gridPlayer)
correct_input = False
while correct_input == False:
user_ch = input("Do you want to continue with this layout? y/n: ")
if user_ch == "y" or user_ch == "Y":
correct_input = True
ch = True
elif user_ch == "n" or user_ch == "N":
correct_input = True
ch = False
else:
correct_input = False
# test = x.gridPlayer
# test = test.splitlines()
for a in range(len(x.bot_choices)):
for b in range(len(x.bot_choices[a])):
x.updateGrid(1, x.bot_choices[a][b], 3)
#print(x.gridBot)
print(x.gridPlayer)
print("You will get 3 shots every turn")
bot_guesses = []
users_turn = True
bot_turn = True
while gameOngoing == True:
num_turns = 0
while users_turn == True:
try:
game_choice = int(input("""Enter-
1: Take chance
2: View your board
3: view your hits on opponents board
Choice: """))
except ValueError:
print('Please enter a correct value!')
continue
if game_choice == 1:
print("Here are your shots on opponents board:")
print(x.gridBotVisible)
pattern = re.compile("[A-F]+[1-6]")
correct_input = False
while correct_input == False:
shot_location = input("Please enter location on board such as A1, B2 etc: ")
if bool(pattern.match(shot_location)):
hit = x.takeShot(shot_location)
num_turns += 1
correct_input = True
print(x.gridBotVisible)
if hit:
print("You successfully hit a boat!")
else:
print("You missed! Better luck next time!")
print("Turn " + str(num_turns) + " of 3")
elif game_choice == 2:
print(x.gridPlayer)
elif game_choice == 3:
print(x.gridBotVisible)
if num_turns == 3:
users_turn = False
bot_turn = True
if "*" not in x.gridBot:
print("Game over! You win!")
gameOngoing = False
break
if gameOngoing == True:
print("Bot's turn now!")
num_turns = 0
while bot_turn == True:
time.sleep(1.5)
bot_guess_unique = False
while bot_guess_unique == False:
guess = chr(65+randint(0, 5)) + str(randint(1, 6))
if guess not in bot_guesses:
bot_guesses.append(guess)
bot_guess_unique = True
hit = x.takeShotBot(guess)
if hit:
print("Bot shot at " + guess + " and hit your ship!")
else:
print("Bot shot at " + guess + " and missed!")
num_turns += 1
if num_turns == 3:
bot_turn = False
users_turn = True
print(x.gridPlayer)
if "*" not in x.gridPlayer:
print("Game over! You lose :( Here is the bot's map:")
print(x.gridBot)
gameOngoing = False