-
Notifications
You must be signed in to change notification settings - Fork 0
/
battleship.py
134 lines (120 loc) · 5.82 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
#This class plays battleship
import random
from ulab import numpy as np
class battleship:
def __init__(self):
self.board = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]] #change to 1 for hit -1 for miss
self.shots = 0
self.state = 0 #0 for searching, 1 for sinking
self.shot_set = set()
self.last_shot = [0,0]
self.first_hit = [-1,-1]
self.last_hit = [-1,-1]
def hit(self):
self.board[self.last_shot[0]][self.last_shot[1]] = 1
if self.last_hit == [-1,-1]:
self.first_hit = self.last_shot
self.last_hit = self.last_shot
self.state = 1
def miss(self):
self.board[self.last_shot[0]][self.last_shot[1]] = -1
def sink(self):
self.board[self.last_shot[0]][self.last_shot[1]] = 1
self.last_hit = self.last_shot
self.state = 0
def whiff(self):
self.shot_set.remove(self.last_shot)
def calc_shot_rand(self):
#increment shot counter
self.shots += 1
#generate random value pairs until the pair isn't in the set of shots that have been done so far
while True:
pick = random.randint(0,7)
options = [[0,0],[0,2],[1,1],[1,3],[2,0],[2,2],[3,1],[3,3]]
#print('generating rando shot, option ' + str(pick) + ' set ')
#print(self.shot_set)
if tuple(options[pick]) not in self.shot_set:
self.shot_set.add(tuple(options[pick]))
self.shots += 1
return options[pick]
def calc_shot_twostate(self):
#if we hit the last shot, switch to search mode
#random search state
if self.state == 0:
while True:
pick = random.randint(0,7)
options = [[0,0],[0,2],[1,1],[1,3],[2,0],[2,2],[3,1],[3,3]]
if tuple(options[pick]) not in self.shot_set:
self.shot_set.add(tuple(options[pick]))
self.shots += 1
self.last_shot = options[pick]
return options[pick]
#sinking state
if self.state == 1:
#lay out options
i = self.last_hit[0]
j = self.last_hit[1]
print('Searching the area around ' + str(i) + ',' + str(j))
print()
options = [[i-1,j],[i+1,j],[i,j-1],[i,j+1]]
#searching for the next part of the boat
if 0 <= i-1 <= 3 and self.board[options[0][0]][options[0][1]] == 0:
if (i-1,j) not in self.shot_set:
print('Opt a')
print('Hit: ' + str(self.board[options[0][0]][options[0][1]]))
self.shot_set.add(tuple(options[0]))
self.last_shot = options[0]
return options[0]
elif 0 <= i+1 <= 3 and self.board[options[1][0]][options[1][1]] == 0:
if (i+1,j) not in self.shot_set:
self.last_shot = options[1]
self.shot_set.add(tuple(options[1]))
print('Opt b')
print('Hit: ' + str(self.board[options[1][0]][options[1][1]]))
return options[1]
elif 0 <= j-1 <= 3 and self.board[options[2][0]][options[2][1]] == 0:
if (i,j-1) not in self.shot_set:
self.last_shot = options[2]
self.shot_set.add(tuple(options[2]))
print('Opt c')
print('Hit: ' + str(self.board[options[2][0]][options[2][1]]))
return options[2]
elif 0 <= j+1 <= 3 and self.board[options[3][0]][options[3][1]] == 0:
if (i,j+1) not in self.shot_set:
self.last_shot = options[3]
self.shot_set.add(tuple(options[3]))
print('Opt d')
print('Hit: ' + str(self.board[options[3][0]][options[3][1]]))
return options[3]
print('No acceptable coordinates found. Returning search to first hit on this ship.')
i = self.first_hit[0]
j = self.first_hit[1]
print('Searching the area around ' + str(i) + ',' + str(j))
print()
options = [[i-1,j],[i+1,j],[i,j-1],[i,j+1]]
#handling if we get started the sink from the middle of the boat
if 0 <= i-1 <= 3 and self.board[options[0][0]][options[0][1]] == 0:
if (i-1,j) not in self.shot_set:
print('Opt a')
print('Hit: ' + str(self.board[options[0][0]][options[0][1]]))
self.last_shot = options[0]
return options[0]
elif 0 <= i+1 <= 3 and self.board[options[1][0]][options[1][1]] == 0:
if (i+1,j) not in self.shot_set:
self.last_shot = options[1]
print('Opt b')
print('Hit: ' + str(self.board[options[1][0]][options[1][1]]))
return options[1]
elif 0 <= j-1 <= 3 and self.board[options[2][0]][options[2][1]] == 0:
if (i,j-1) not in self.shot_set:
self.last_shot = options[2]
print('Opt c')
print('Hit: ' + str(self.board[options[2][0]][options[2][1]]))
return options[2]
elif 0 <= j+1 <= 3 and self.board[options[3][0]][options[3][1]] == 0:
if (i,j+1) not in self.shot_set:
self.last_shot = options[3]
print('Opt d')
print('Hit: ' + str(self.board[options[3][0]][options[3][1]]))
else:
raise Exception('Impossible board state')