-
Notifications
You must be signed in to change notification settings - Fork 1
/
player.py
322 lines (290 loc) · 15.1 KB
/
player.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
import ships
import bot
class player: # player class creates a player with its own board and ship locations, Written by Hank Pham & Brennan Miller-Klugman
def __init__(self, name): # blank constructor for player class
# setup default board
self.playerWon = False
self.board = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
# player arsenal is used to keep track of which ships are created inside of the createShips method
self.playerArsenal = []
# add instances of each ship type into the player arsenal list
self.playerArsenal.append(ships.carrier(0, 0, 0, 0))
self.playerArsenal.append(ships.battleShip(0, 0, 0, 0))
self.playerArsenal.append(ships.cruiser(0, 0, 0, 0))
self.playerArsenal.append(ships.submarine(0, 0, 0, 0))
self.playerArsenal.append(ships.destroyer(0, 0, 0, 0))
self.hitBot = bot.bot()
self.shipSunk = 0
self.playerName = name
def getPlayerWon(self): #Written by Hank Pham & Brennan Miller-Klugman
return self.playerWon
# function to print the map visible to the enemy player (ship locations not included)
def printEnemyMap(self): #Written by Brennan Miller-Klugman
print("\n 0 1 2 3 4 5 6 7 8 9")
print("-----------------------------------------")
counter = 0
for row in self.board:
for columns in row:
if columns == 1:
print("%s X" % ("|"), end=" ")
elif columns == 2:
print("%s !" % ("|"), end=" ")
else:
print("%s " % ("|"), end=" ")
print("| %s" % counter)
print("-----------------------------------------")
counter += 1
# function to print the map visible to the enemy player (ship locations included)
def printPlayerMap(self): #Written by Brennan Miller-Klugman
print("\n 0 1 2 3 4 5 6 7 8 9")
print("-----------------------------------------")
counter = 0
for row in self.board:
for columns in row:
if columns == 1:
print("%s X" % ("|"), end=" ")
elif columns == 2:
print("%s !" % ("|"), end=" ")
elif columns == 4:
print("%s -" % ("|"), end=" ")
else:
print("%s " % ("|"), end=" ")
print("| %s" % counter)
print("-----------------------------------------")
counter += 1
def hit(self): # function that enemy player calls to hit a posistion on the players board, Written by Hank Pham & Brennan Miller-Klugman
# ask enemy to enter a location to attack
row = int(input("Enter Row To Hit: "))
column = int(input("Enter column To Hit: "))
try: # try catch to determine if the player strikes an out of bounds spot
# check to see if the posistion hit is housing a ship
if self.board[row][column] == 4:
# set the board posistion to 2 if the enemy hit a ship
self.board[row][column] = 2
print("Enemy ship hit!")
elif self.board[row][column] == 0:
# set the board posistion to 1 if the enemy missed a ship
self.board[row][column] = 1
print("Missed enemy target!")
else: # make sure the player has not attacked the spot previously
print("You already attacked this spot")
self.hit()
if(self.isSunk()):
print("You sunk a ship!")
except Exception:
print("Your value entered is out of bounds")
self.hit()
def botHit(self): # function that enemy player calls to hit a posistion on the players board, Written by Hank Pham & Brennan Miller-Klugman
# ask enemy to enter a location to attack
hit = self.hitBot.generateHit()
row = hit["row"]
column = hit["column"]
try: # try catch to determine if the player strikes an out of bounds spot
# check to see if the posistion hit is housing a ship
if self.board[row][column] == 4:
# set the board posistion to 2 if the enemy hit a ship
self.board[row][column] = 2
self.isSunk()
if(self.hitBot.getHit() == False):
self.hitBot.setHit(row, column)
elif self.board[row][column] == 0:
# set the board posistion to 1 if the enemy missed a ship
self.board[row][column] = 1
else: # make sure the player has not attacked the spot previously
self.botHit()
except Exception:
self.botHit()
def isSunk(self): #Written by Hank Pham
for ship in self.playerArsenal:
if ship.getSunk() == False:
columnStart = ship.getStartColumn()
columnEnd = ship.getEndColumn()
RowStart = ship.getStartRow()
RowEnd = ship.getEndRow()
i = 0
if columnStart == columnEnd:
while self.board[RowStart + i][columnStart] == 2 or self.board[RowStart - i][columnStart] == 2:
i = i + 1
if i == ship.getSize():
self.shipSunk += 1
ship.setSunk(True)
if self.shipSunk == 5:
print("%s Won!" % (self.playerName)) # if 5 ships have been sunk you lost!
self.playerWon = True
return True
else:
while self.board[RowStart][columnStart + i] == 2 or self.board[RowStart][columnStart - i] == 2:
i = i + 1
if i == ship.getSize():
ship.setSunk(True)
self.shipSunk += 1
if self.shipSunk == 5:
print("%s Won!" % (self.playerName)) # if 5 ships have been sunk you lost!
self.playerWon = True
return True
return False
def createShip(self): # function to populate the player board, Written by Hank Pham, and Brennan Miller-Klugman
for ship in self.playerArsenal: # for each ship in the player arsenal, prompt for a ship location and populate the ship on the map
backupBoard = self.board
try: # try catch to reset the board incase the player enters overlapping ships
if ship.isCreated() == False:
self.printPlayerMap()
ship.setCreated()
ShipcolumnStart = int(
input("Enter column For Start Position For %s: " % (ship.getName())))
ShipRowStart = int(
input("Enter Row For Start Posistion For %s : " % (ship.getName())))
shipOrientation = input(
"Enter Orientation for %s (up down left or right): " % (ship.getName()))
if shipOrientation.lower() == "up": # check ship orientation, then populate the map based on the input
ShipRowEnd = ShipRowStart - ship.getSize()
if ShipRowEnd < 0:
raise Exception
ShipcolumnEnd = ShipcolumnStart
i = ShipRowStart
k = i
while k > ShipRowEnd:
if self.board[k][ShipcolumnEnd] == 4:
raise Exception
k -= 1
while i > ShipRowEnd:
self.board[i][ShipcolumnEnd] = 4
i = i-1
elif shipOrientation.lower() == "down":
ShipRowEnd = ShipRowStart + ship.getSize()
if ShipRowEnd > 9:
raise Exception
ShipcolumnEnd = ShipcolumnStart
i = ShipRowStart
k = i
while k < ShipRowEnd:
if self.board[k][ShipcolumnEnd] == 4:
raise Exception
k += 1
while i < ShipRowEnd:
self.board[i][ShipcolumnEnd] = 4
i = i+1
elif shipOrientation.lower() == "left":
ShipcolumnEnd = ShipcolumnStart - ship.getSize()
if ShipcolumnEnd < 0:
raise Exception
ShipRowEnd = ShipRowStart
i = ShipcolumnStart
k = i
while k > ShipcolumnEnd:
if self.board[ShipRowEnd][k] == 4:
raise Exception
k -= 1
while i > ShipcolumnEnd:
self.board[ShipRowEnd][i] = 4
i = i-1
elif shipOrientation.lower() == "right":
ShipcolumnEnd = ShipcolumnStart + ship.getSize()
if ShipcolumnEnd > 9:
raise Exception
ShipRowEnd = ShipRowStart
i = ShipcolumnStart
k = i
while k < ShipcolumnEnd:
if self.board[ShipRowEnd][k] == 4:
raise Exception
k += 1
while i < ShipcolumnEnd:
self.board[ShipRowEnd][i] = 4
i = i+1
else: # for invalid ship orientation warn the player and then reset the function
print("Invalid Ship Location")
self.createShip()
# set ship location inside of ships.py
ship.setShipPos(ShipRowStart, ShipcolumnStart,
ShipRowEnd, ShipcolumnEnd)
ship.printLocation() # print ship location for user to see
else:
continue
except Exception:
print("Invalid Ship Location, Please try again")
self.board = backupBoard
ship.setCreated()
self.createShip()
def botPopulateBoard(self): # function to populate the bot board, Written by Hank Pham, and Brennan Miller-Klugman
playerBot = bot.bot()
for ship in self.playerArsenal: # for each ship in the player arsenal, prompt for a ship location and populate the ship on the map
backupBoard = self.board
try: # try catch to reset the board incase the player enters overlapping ships
if ship.isCreated() == False:
ship.setCreated()
# Return a dictonary that consists of "column", "row", and "orientation"
boardLoc = playerBot.generateBoard()
ShipcolumnStart = boardLoc["column"]
ShipRowStart = boardLoc["row"]
shipOrientation = boardLoc["orientation"]
if shipOrientation.lower() == "up": # check ship orientation, then populate the map based on the input
ShipRowEnd = ShipRowStart - ship.getSize()
if ShipRowEnd < 0:
raise Exception
ShipcolumnEnd = ShipcolumnStart
i = ShipRowStart
k = i
while k > ShipRowEnd:
if self.board[k][ShipcolumnEnd] == 4:
raise Exception
k -= 1
while i > ShipRowEnd:
self.board[i][ShipcolumnEnd] = 4
i = i-1
elif shipOrientation.lower() == "down":
ShipRowEnd = ShipRowStart + ship.getSize()
if ShipRowEnd > 9:
raise Exception
ShipcolumnEnd = ShipcolumnStart
i = ShipRowStart
k = i
while k < ShipRowEnd:
if self.board[k][ShipcolumnEnd] == 4:
raise Exception
k += 1
while i < ShipRowEnd:
self.board[i][ShipcolumnEnd] = 4
i = i+1
elif shipOrientation.lower() == "left":
ShipcolumnEnd = ShipcolumnStart - ship.getSize()
if ShipcolumnEnd < 0:
raise Exception
ShipRowEnd = ShipRowStart
i = ShipcolumnStart
k = i
while k > ShipcolumnEnd:
if self.board[ShipRowEnd][k] == 4:
raise Exception
k -= 1
while i > ShipcolumnEnd:
self.board[ShipRowEnd][i] = 4
i = i-1
elif shipOrientation.lower() == "right":
ShipcolumnEnd = ShipcolumnStart + ship.getSize()
if ShipcolumnEnd > 9:
raise Exception
ShipRowEnd = ShipRowStart
i = ShipcolumnStart
k = i
while k < ShipcolumnEnd:
if self.board[ShipRowEnd][k] == 4:
raise Exception
k += 1
while i < ShipcolumnEnd:
self.board[ShipRowEnd][i] = 4
i = i+1
else: # for invalid ship orientation warn the player and then reset the function
print("Invalid Ship Location")
self.botPopulateBoard()
# set ship location inside of ships.py
ship.setShipPos(ShipRowStart, ShipcolumnStart,
ShipRowEnd, ShipcolumnEnd)
ship.printLocation() # print ship location for user to see
else:
continue
except Exception:
self.board = backupBoard
ship.setCreated()
self.botPopulateBoard()