-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAICLASSIC.py
177 lines (147 loc) · 5.98 KB
/
AICLASSIC.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
import AIMASTER as M
class AiClassic(M.AiMaster):
"""Shoots randomly until a hit. Then shoots adjaccents until a hit. Then finds the line and shoots up and down until sink or misss"""
def __init__(self):
M.AiMaster.__init__(self)
self.mode = "random_search"
self.search_start = (None, None) # The origin of the checking_adj
self.saved_lines = []
self.current_line = []
def make_decision(self, board):
if self.places_to_shoot == []:
self.mode = "random_search"
self.add_to_places_to_shoot(self.pick_random(board), board)
shot_destination = self.places_to_shoot.pop()
shot_report = board.shoot_spot(shot_destination)
self.log.append([shot_destination, shot_report])
if shot_report == 1: # hit
oldY, oldX = shot_destination
if self.mode == "random_search":
self.mode = "checking_line"
self.search_start = shot_destination
self.saved_lines = self.get_all_directions(shot_destination, board)
self.current_line = self.saved_lines.pop()
for loc in self.current_line[2]:
self.add_to_places_to_shoot(loc, board)
if self.mode == "checking_line":
if self.places_to_shoot == []:
#switch directions
if self.switch_direction(board) == False:
self.current_line = self.saved_lines.pop()
for loc in self.current_line[2]:
self.add_to_places_to_shoot(loc, board)
elif shot_report == 0 and self.mode == "checking_line":
self.places_to_shoot = []
if self.saved_lines != []:
if self.switch_direction(board) == False:
self.current_line = self.saved_lines.pop()
for loc in self.current_line[2]:
self.add_to_places_to_shoot(loc, board)
elif shot_report == 2:
self.places_to_shoot = []
self.saved_lines = []
# Not bieng used currently
def line_orientation_temp(self, point1, point2):
y1, x1 = point1
y2, x2 = point2
if y1 == y2:
return "Horizontal"
elif x1 == x2:
return "Vertical"
else:
return "No Line"
# Not being used currently
def line_direction(self, point1, point2):
"""Line points from point1 to point2"""
orient = self.line_orientation(point1, point2)
y1, x1 = point1
y2, x2 = point2
if orient == "Vertical":
if y2 < y1:
return "Up"
elif y2 > y1:
return "Down"
elif orient == "Horizontal":
if x2 < x1:
return "Left"
elif x2 > x1:
return "Right"
return "Error"
def get_full_line(self, originTuple, orientation, board):
orgY, orgX = originTuple
line_locations = []
hit_miss_high, hit_miss_low = False, False
mod = 1
while not hit_miss_low or not hit_miss_high:
if orientation == "Horizontal":
xMod = mod
yMod = 0
elif orientation == "Vertical":
yMod = mod
xMod = 0
if orgX+xMod >= board.get_length() or orgY+yMod >= board.get_height() or board.firedMap[orgY+yMod][orgX+xMod] == "X":
hit_miss_high = True
elif not hit_miss_high:
line_locations.append((orgY+yMod, orgX+xMod))
if orgX-xMod < 0 or orgY-yMod < 0 or board.firedMap[orgY-yMod][orgX-xMod] == "X":
hit_miss_low = True
elif not hit_miss_low:
line_locations.append((orgY-yMod, orgX-xMod))
mod += 1
line_locations.reverse()
return line_locations
def get_half_line(self, originTuple, direction, board):
orgY, orgX = originTuple
line_locations = []
mod = 1
xMod, yMod = 0, 0
while True:
if direction == "Right":
xMod = mod
elif direction == "Left":
xMod = -mod
elif direction == "Down":
yMod = mod
elif direction == "Up":
yMod = -mod
if orgX+xMod < 0 or orgX+xMod >= board.get_length() or orgY+yMod < 0 or orgY+yMod >= board.get_height() or board.firedMap[orgY+yMod][orgX+xMod] == "X":
break
else:
line_locations.append((orgY+yMod, orgX+xMod))
mod += 1
line_locations.reverse()
return line_locations
def get_all_directions(self, originTuple, board):
lines = []
up = self.get_half_line(originTuple, "Up", board)
right = self.get_half_line(originTuple, "Right", board)
down = self.get_half_line(originTuple, "Down", board)
left = self.get_half_line(originTuple, "Left", board)
if up != []:
lines.append(["Up", len(up), up])
if right != []:
lines.append(["Right", len(right), right])
if down != []:
lines.append(["Down", len(down), down])
if left != []:
lines.append(["Left", len(left), left])
return lines
def get_reverse_direction(self, direction):
if direction == "Up":
return "Down"
elif direction == "Down":
return "Up"
elif direction == "Right":
return "Left"
elif direction == "Left":
return "Right"
def switch_direction(self, board):
new_direction = self.get_reverse_direction(self.current_line[0])
for line in self.saved_lines:
if line[0] == new_direction:
self.current_line = line
self.saved_lines.remove(line)
for loc in self.current_line[2]:
self.add_to_places_to_shoot(loc, board)
return True
return False