-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBShip.py
More file actions
213 lines (160 loc) · 5.56 KB
/
BShip.py
File metadata and controls
213 lines (160 loc) · 5.56 KB
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
import random
vertical = []
horizontal1 = ["0 ", "0 ", "0 ", "0 ", "0 ", "0 "]
horizontal2 = ["0 ", "0 ", "0 ", "0 ", "0 ", "0 "]
horizontal3 = ["0 ", "0 ", "0 ", "0 ", "0 ", "0 "]
horizontal4 = ["0 ", "0 ", "0 ", "0 ", "0 ", "0 "]
horizontal5 = ["0 ", "0 ", "0 ", "0 ", "0 ", "0 "]
horizontal6 = ["0 ", "0 ", "0 ", "0 ", "0 ", "0 "]
vertical.append(horizontal1)
vertical.append(horizontal2)
vertical.append(horizontal3)
vertical.append(horizontal4)
vertical.append(horizontal5)
vertical.append(horizontal6)
boat1X = []
boat1Y = []
boat2X = []
boat2Y = []
boat3X = []
boat3Y = []
boat4X = []
boat4Y = []
boat5 = []
boatX = [boat1X, boat2X, boat3X, boat4X]
boatY = [boat1Y, boat2Y, boat3Y, boat4Y]
boatCount = 0
repeat = False
def print_grid():
for y in vertical:
for x in y:
print(x, end = " ")
print("\n")
def check_repeats(choiceX, choiceY):
#print("choiceX = " + str(choiceX))
#print("choiceY = " + str(choiceY))
for boat in boatX:
boat2 = boatY[boatX.index(boat)]
for block in boat:
#print("block = " + str(block))
respectiveY = boat2[boat.index(block)]
if choiceX == block:
#print("X's match up")
#print("lastY = " + str(respectiveY))
if choiceY == respectiveY:
#print("repeat!")
return True
else:
return False
def randomize_boats(how_many):
count = 0
boat_num = 0
broken = False
while count <= how_many - 1:
# determines if boat is horizontal or vertical
direction = random.randint(1, 2)
broken = False
# vertical
if(direction == 1):
#print("vertical")
# determines length of the boat
length = random.randint(1, 4)
# determines where they show up on the board
chance1 = random.randint(1, 6)
chance2 = random.randint(1, 6)
# starts with random coords, then extends the boat to random length
for r in range(length):
if(check_repeats(chance2, chance1)):
boatX[count].clear()
boatY[count].clear()
count -= 1
broken = True
break
else:
boatX[count].append(chance2)
boatY[count].append(chance1)
if chance1 + 1 <= 6:
chance1 += 1
else:
break
if(broken != True):
boat_num += 1
#print(boat_num)
# horizontal
else:
#print("horizontal")
length = random.randint(1, 4)
chance1 = random.randint(1, 6)
chance2 = random.randint(1, 6)
for r in range(length):
if(check_repeats(chance2, chance1)):
boatX[count].clear()
boatY[count].clear()
count -= 1
broken = True
break
else:
boatX[count].append(chance2)
boatY[count].append(chance1)
if chance2 + 1 <= 6:
chance2 += 1
else:
break
if(broken != True):
boat_num += 1
#print(boat_num)
count += 1
#print(count)
randomize_boats(4)
#print(boatX)
#print(boatY)
print_grid()
def guess():
spotX = input("Enter an X coordinate:")
spotY = input("Now a Y coordinate:")
spotX = int(spotX)
spotY = int(spotY)
if(spotY > 6 or spotX > 6):
print("too big for the grid!")
else:
# to keep track of what index 'num' is at in boatX
count1 = 0
count2 = 0
hit = False
for boat in boatX:
if(hit == True):
break
count1 += 1
#print(count1)
#print(boat)
yboat = boatY[count1 - 1]
#print(yboat)
count2 = 0
for block in boat:
count2 += 1
#print(count2 - 1)
if spotX == block:
if(spotY == yboat[count2 - 1]):
print("HIT!!")
hit = True
vertical[spotY - 1].pop(spotX - 1)
vertical[spotY - 1].insert(spotX - 1, "X ")
boat.pop(count2 - 1)
yboat.pop(count2 - 1)
if(len(boat) == 0 and len(yboat) == 0):
print("Boat destroyed!")
#boatCount += 1
break
else:
print("miss..")
vertical[spotY - 1].pop(spotX - 1)
vertical[spotY - 1].insert(spotX - 1, "- ")
else:
print("miss..")
vertical[spotY - 1].pop(spotX - 1)
vertical[spotY - 1].insert(spotX - 1, "- ")
print_grid()
tries = 10
while(tries > 0):
guess()
tries -= 1