-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBattleship.py
266 lines (204 loc) · 7.3 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
import random
import time
##-------------------------------------------------
##
## Initial Game Board
##
##-------------------------------------------------
board = [[" ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]]
for i in range(9):
board.append([" "+str(i+1), "O", "O", "O", "O", "O", "O", "O", "O", "O", "O",])
board.append([str(10), "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"])
def print_board():
for i in range(11):
print(" ".join(board[i]))
##-------------------------------------------------
##
## Alphabet to Number Dictionary
##
##-------------------------------------------------
alph = {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5, "F": 6, "G": 7, "H": 8, "I": 9, "J": 10}
##-------------------------------------------------
##
## Initial Ship Positions
##
##-------------------------------------------------
# Choose an initial orientation for a ship. 1 is horizontal, 2 is vertical.
def orient():
return random.choice((1,2))
s5_orient = orient()
s4_orient = orient()
s3_1_orient = orient()
s3_2_orient = orient()
s2_orient = orient()
# Find the cells covered by "ship" givin its orientation k, size n,
# and initial position with coordinates r_init, c_init. Prints the coordinates
# of a ship as list [[r1, c1], [r2, c2],..., [rn, cn]].
def position(k, n, r_init, c_init, ship):
if k == 1:
for i in range(n):
ship.append([r_init, c_init + i])
else:
for i in range(n):
ship.append([r_init + i, c_init])
return ship
# Small procedure to guarantee that ships do not overlap.
def overlap(s1, s2):
counter = 0
for j in range(len(s1)):
for m in range(len(s2)):
if s1[j] == s2[m]:
counter = counter + 1
if counter > 0:
return 1
else:
return 0
# Choose and record initial positions of each ship.
def s5_position():
if s5_orient == 1:
s5_init_row = random.randint(1,10)
s5_init_col = random.randint(1,6)
return position(s5_orient, 5, s5_init_row, s5_init_col, [])
else:
s5_init_row = random.randint(1,6)
s5_init_col = random.randint(1,10)
return position(s5_orient, 5, s5_init_row, s5_init_col, [])
s5_pos = s5_position()
def s4_position():
loop = 0
while loop == 0:
if s4_orient == 1:
s4_init_row = random.randint(1,10)
s4_init_col = random.randint(1,7)
pos = position(s4_orient, 4, s4_init_row, s4_init_col, [])
else:
s4_init_row = random.randint(1,7)
s4_init_col = random.randint(1,10)
pos = position(s4_orient, 4, s4_init_row, s4_init_col, [])
if overlap(s5_pos,pos) == 0:
return pos
loop = 1
s4_pos = s4_position()
def s3_1_position():
loop = 0
while loop == 0:
if s3_1_orient == 1:
s3_1_init_row = random.randint(1,10)
s3_1_init_col = random.randint(1,8)
pos = position(s3_1_orient, 3, s3_1_init_row, s3_1_init_col, [])
else:
s3_1_init_row = random.randint(1,8)
s3_1_init_col = random.randint(1,10)
pos = position(s3_1_orient, 3, s3_1_init_row, s3_1_init_col, [])
if overlap(s5_pos,pos) == 0 and overlap (s4_pos,pos) == 0:
return pos
loop = 1
s3_1_pos = s3_1_position()
def s3_2_position():
loop = 0
while loop == 0:
if s3_2_orient == 1:
s3_2_init_row = random.randint(1,10)
s3_2_init_col = random.randint(1,8)
pos = position(s3_2_orient, 3, s3_2_init_row, s3_2_init_col, [])
else:
s3_2_init_row = random.randint(1,8)
s3_2_init_col = random.randint(1,10)
pos = position(s3_2_orient, 3, s3_2_init_row, s3_2_init_col, [])
if overlap(s5_pos,pos) == 0 and overlap(s4_pos,pos) == 0 and overlap(s3_1_pos,pos) == 0:
return pos
loop = 1
s3_2_pos = s3_2_position()
def s2_position():
loop = 0
while loop == 0:
if s2_orient == 1:
s2_init_row = random.randint(1,10)
s2_init_col = random.randint(1,9)
pos = position(s2_orient, 2, s2_init_row, s2_init_col, [])
else:
s2_init_row = random.randint(1,9)
s2_init_col = random.randint(1,10)
pos = position(s2_orient, 2, s2_init_row, s2_init_col, [])
if overlap(s5_pos,pos) == 0 and overlap(s4_pos,pos) == 0 and overlap(s3_1_pos,pos) == 0 and overlap(s3_2_pos,pos) == 0:
return pos
loop = 1
s2_pos = s2_position()
##print(s5_pos)
##print(s4_pos)
##print(s3_1_pos)
##print(s3_2_pos)
##print(s2_pos)
##-------------------------------------------------
##
## Game Play
##
##-------------------------------------------------
print("Welcome to Battleship! Here is your gameboard:")
i = 0
while i < 60:
print_board()
c = 1
while c > 0:
while True:
try:
user_row = int(input("Choose a row number: "))
break
except (SyntaxError, ValueError):
print("Please enter a valid row number!")
if user_row in range(1,11):
c = 0
else:
print("Please enter a valid row number!")
while c < 1:
while True:
try:
user_col = str(input("Choose a column letter: ")).upper()
break
except (SyntaxError, ValueError):
print("Please enter a valid column letter!")
if user_col in alph:
c = 1
else:
print("Please enter a valid column letter!")
guess = [user_row, alph[user_col]]
if guess in s5_pos :
print("You hit a ship!\n")
board[user_row][alph[user_col]] = "X"
s5_pos.remove(guess)
if s5_pos == []:
print("You sank my aircraft carrier!\n")
elif guess in s4_pos:
print("You hit a ship!\n")
board[user_row][alph[user_col]] = "X"
s4_pos.remove(guess)
if s4_pos == []:
print("You sank my battleship!\n")
elif guess in s3_1_pos:
print("You hit a ship!\n")
board[user_row][alph[user_col]] = "X"
s3_1_pos.remove(guess)
if s3_1_pos == []:
print("You sank my destroyer!\n")
elif guess in s3_2_pos:
print("You hit a ship!\n")
board[user_row][alph[user_col]] = "X"
s3_2_pos.remove(guess)
if s3_2_pos == []:
print("You sank my submarine!\n")
elif guess in s2_pos:
print("You hit a ship!\n")
board[user_row][alph[user_col]] = "X"
s2_pos.remove(guess)
if s2_pos == []:
print("You sank my patrol boat!\n")
else:
print("You missed.\n")
board[user_row][alph[user_col]] = "."
time.sleep(1)
if s5_pos == [] and s4_pos == [] and s3_1_pos == [] and s3_2_pos == [] and s2_pos == []:
print("You sank all of my ships! You won the game!")
i = 60
elif i == 59:
print("You lost!")
i = i + 1