-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattleship_seabattle.py
76 lines (71 loc) · 2.81 KB
/
battleship_seabattle.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
battleField = [[1, 0, 0, 0, 0, 1, 1, 0, 0, 0],
[1, 0, 1, 0, 0, 0, 0, 0, 1, 0],
[1, 0, 1, 0, 1, 1, 1, 0, 1, 0],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
def validate_battlefield(field):
shipped = []
for i, row in enumerate(field):
for col, item in enumerate(row):
if item == 1:
shipped.append((i, col))
for tup in shipped:
if 1 <= tup[0] <= 8 and 1 <= tup[1] <= 8:
for corner in ((tup[0] - 1, tup[1] - 1),
(tup[0] - 1, tup[1] + 1),
(tup[0] + 1, tup[1] - 1),
(tup[0] + 1, tup[1] + 1)):
if corner in shipped:
return False
if (tup[0] - 1, tup[1]) in shipped:
if (tup[0], tup[1] - 1) in shipped or\
(tup[0], tup[1] + 1) in shipped:
return False
if (tup[0] + 1, tup[1]) in shipped:
if (tup[0], tup[1] - 1) in shipped or\
(tup[0], tup[1] + 1) in shipped:
return False
battleship = False
cruisers_2 = False
destroyers_3 = False
submarines_4 = False
while shipped:
for tup in shipped:
a, b, c = (tup[0] + 1, tup[1]), (tup[0] + 2,
tup[1]), (tup[0] + 3, tup[1])
if a in shipped:
if b in shipped:
if c in shipped:
battleship += 1
shipped.remove(c)
else:
cruisers_2 += 1
shipped.remove(b)
else:
destroyers_3 += 1
shipped.remove(a)
break
a, b, c = (tup[0], tup[1] + 1), (tup[0],
tup[1] + 2), (tup[0], tup[1] + 3)
if a in shipped:
if b in shipped:
if c in shipped:
battleship += 1
shipped.remove(c)
else:
cruisers_2 += 1
shipped.remove(b)
else:
destroyers_3 += 1
shipped.remove(a)
break
submarines_4 += 1
shipped.remove(tup)
print(battleship, cruisers_2, destroyers_3, submarines_4)
return battleship == 1 and cruisers_2 == 2 and destroyers_3 == 3 and submarines_4 == 4
print(validate_battlefield(battleField))