diff --git a/crop/Field_Representation_crop.py b/crop/Field_Representation_crop.py index 64eb1b0..cc30aa9 100644 --- a/crop/Field_Representation_crop.py +++ b/crop/Field_Representation_crop.py @@ -43,10 +43,10 @@ class Field: next_level_x = cv.imread("next_level_x.jpg", cv.IMREAD_COLOR) explosives = [] explosives.append(RAINBOW) - explosives.append(ARROW_RIGHT) - explosives.append(ARROW_DOWN) - explosives.append(BIGBOMB) - explosives.append(BOMB) + #explosives.append(ARROW_RIGHT) + #explosives.append(ARROW_DOWN) + #explosives.append(BIGBOMB) + #explosives.append(BOMB) colors = [] colors.append(GREEN) colors.append(YELLOW) @@ -85,13 +85,13 @@ class Field: YELLOW: cv.imread("yellow.jpg", cv.IMREAD_COLOR), PURPLE: cv.imread("purple.jpg", cv.IMREAD_COLOR), BLUE: cv.imread("blue.jpg", cv.IMREAD_COLOR), - RED: cv.imread("red.jpg", cv.IMREAD_COLOR) + RED: cv.imread("red.jpg", cv.IMREAD_COLOR), - # RAINBOW: cv.imread("rainbow.jpg", cv.IMREAD_COLOR), - # BIGBOMB: cv.imread("bigbomb.jpg", cv.IMREAD_COLOR), - # BOMB: cv.imread("bomb.jpg", cv.IMREAD_COLOR), - # ARROW_DOWN: cv.imread("arrow_down.jpg", cv.IMREAD_COLOR), - # ARROW_RIGHT: cv.imread("arrow_right.jpg", cv.IMREAD_COLOR), + RAINBOW: cv.imread("rainbow.jpg", cv.IMREAD_COLOR), + #BIGBOMB: cv.imread("bigbomb.jpg", cv.IMREAD_COLOR), + #BOMB: cv.imread("bomb.jpg", cv.IMREAD_COLOR), + #ARROW_DOWN: cv.imread("arrow_down.jpg", cv.IMREAD_COLOR), + #ARROW_RIGHT: cv.imread("arrow_right.jpg", cv.IMREAD_COLOR) # ROCK_1: cv.imread("rock1.jpg", cv.IMREAD_COLOR), # ROCK_2: cv.imread("rock2.jpg", cv.IMREAD_COLOR), # ROCK_3: cv.imread("rock3.jpg", cv.IMREAD_COLOR), @@ -166,7 +166,7 @@ class Field: self.screenshot = screenshot # gray = cv.cvtColor(screenshot, cv.COLOR_BGR2GRAY) # thresh = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)[1] - ''' + if self.check_for_next_level(screenshot, self.next_level): cv.waitKey(500) screenshot = self.capture_window.get_screenshot() @@ -175,7 +175,7 @@ class Field: cv.waitKey(500) screenshot = self.capture_window.get_screenshot() screenshot = screenshot[58:1134, 230:2113] - ''' + except: # self.capture_window.release() # print("Game window not available - shutting down application") @@ -216,6 +216,51 @@ class Field: return data_coords, screenshot + def detonate_explosive_when_stuck(self, state): + for e in range(0, 8, 1): + for i in range(0, 14, 1): + for explosive in self.explosives: + if self.local_pos_check(state, e, i, 0, 0, explosive): + dest_pt = self.get_click_point(self.data_coordinates[e, i]) + if self.local_pos_checks(state, e, i, 1, 0, self.colors): + src_pt = self.get_click_point(self.data_coordinates[e + 1, i]) + self.move_tile(src_pt, dest_pt) + elif self.local_pos_checks(state, e, i, 0, 1, self.colors): + src_pt = self.get_click_point(self.data_coordinates[e, i + 1]) + self.move_tile(src_pt, dest_pt) + elif self.local_pos_checks(state, e, i, -1, 0, self.colors): + src_pt = self.get_click_point(self.data_coordinates[e - 1, i]) + self.move_tile(src_pt, dest_pt) + elif self.local_pos_checks(state, e, i, 0, -1, self.colors): + src_pt = self.get_click_point(self.data_coordinates[e, i - 1]) + self.move_tile(src_pt, dest_pt) + else: + continue + return + + def check_explosives(self, state, e, i): + for explosive in self.explosives: + if self.local_pos_check(state, e, i, 0, 0, explosive): + dest_pt = self.get_click_point(self.data_coordinates[e, i]) + if self.local_pos_checks(state, e, i, 1, 0, self.explosives): + src_pt = self.get_click_point(self.data_coordinates[e + 1, i]) + self.move_tile(src_pt, dest_pt) + elif self.local_pos_checks(state, e, i, 0, 1, self.explosives): + src_pt = self.get_click_point(self.data_coordinates[e, i + 1]) + self.move_tile(src_pt, dest_pt) + elif self.local_pos_checks(state, e, i, -1, 0, self.explosives): + src_pt = self.get_click_point(self.data_coordinates[e - 1, i]) + self.move_tile(src_pt, dest_pt) + elif self.local_pos_checks(state, e, i, 0, -1, self.explosives): + src_pt = self.get_click_point(self.data_coordinates[e, i - 1]) + self.move_tile(src_pt, dest_pt) + else: + continue + return True + else: + continue + return False + def find_patterns_and_valid_moves(self, state): #score_map = np.zeros((8, 14), dtype=object) @@ -227,6 +272,8 @@ class Field: reserve_moves = [] for x in range(0, 8, 1): for y in range(0, 14, 1): + if self.check_explosives(state, x, y): + return if self.data_score_map[x, y] >= 4: path_option = [] path_option.append([x, y]) @@ -236,7 +283,7 @@ class Field: if result == STATUS_FOUND_ENDING: path_option.append([x, y]) if len(path_option) >= 5: - full_moves.append(path_option) + full_moves.append((state[x,y], path_option)) if self.data_score_map[x, y] >= 1: path_option = [] path_option.append([x, y]) @@ -244,11 +291,12 @@ class Field: recursion_reminder.append([x, y]) self.find_next_same_color_all_directions_recursion2(state, x, y, path_option) if len(path_option) >= 3: - reserve_moves.append(path_option) + reserve_moves.append((state[x,y], path_option)) #print(self.data_score_map) #screenshot = cv.imread("screenshot_scored.jpg") - + full_dic = self.convert_moves_to_dic_by_color(full_moves) + res_dic = self.convert_moves_to_dic_by_color(reserve_moves) full_moves.sort(key=len, reverse=True) reserve_moves.sort(key=len, reverse=True) @@ -256,21 +304,33 @@ class Field: ''' - if len(full_moves) >= 1: - self.print_move(full_moves) - self.execute_move(full_moves) - elif len(reserve_moves) >= 1: - #moves = [] - #moves.append(reserve_moves[random.randint(0, len(reserve_moves)-1)]) - self.print_move(reserve_moves) - self.execute_move(reserve_moves) - else: - # boelk - pass + if len(full_dic) >= 1: + for key in sorted(full_dic): + if len(full_dic[key]) >= 10: + self.print_move(full_dic[key]) + self.execute_move(full_dic[key]) + break + if len(reserve_moves) >= 1: + for key in sorted(res_dic): + self.print_move(res_dic[key]) + self.execute_move(res_dic[key]) + break - def print_move(self, moves): + + def convert_moves_to_dic_by_color(self, reserve_moves): + dic = {} + for color, move in reserve_moves: + if color in dic: + if len(move) > len(dic[color]): + dic[color] = move + else: + dic[color] = move + return dic + + + def print_move(self, move): mp = [] - for point in moves[0]: + for point in move: cp = self.get_click_point(self.data_coordinates[point[0], point[1]]) mp.append(cp) cv.polylines(self.screenshot, @@ -281,21 +341,21 @@ class Field: cv.imwrite('screenshot_pathed.jpg', self.screenshot) - def execute_move(self, moves): + def execute_move(self, move): offset_left = 230 offset_down = 58 - if len(moves) >= 1: - first = True - for cords in moves[0]: - square = self.data_coordinates[cords[0], cords[1]] - pydirectinput.moveTo(get_click_point(square)[0] + offset_left, get_click_point(square)[1]+offset_down) - cv.waitKey(50) - if first: - pydirectinput.mouseDown() - cv.waitKey(100) - first = False - pydirectinput.mouseUp() + + first = True + for cords in move: + square = self.data_coordinates[cords[0], cords[1]] + pydirectinput.moveTo(get_click_point(square)[0] + offset_left, get_click_point(square)[1]+offset_down) cv.waitKey(50) + if first: + pydirectinput.mouseDown() + cv.waitKey(100) + first = False + pydirectinput.mouseUp() + cv.waitKey(50) def get_directions_array(self, current_x, current_y): left_x = current_x @@ -417,7 +477,7 @@ class Field: score = 0 color = state[current_x, current_y] - if color == 0: + if color <= 0 or color >= 6: return score for direction in directions: score = score + self.is_direction_in_bounce_and_same_color(state, direction, color) diff --git a/crop/arrow_down.jpg b/crop/arrow_down.jpg new file mode 100644 index 0000000..e9f60d6 Binary files /dev/null and b/crop/arrow_down.jpg differ diff --git a/crop/arrow_right.jpg b/crop/arrow_right.jpg new file mode 100644 index 0000000..9757744 Binary files /dev/null and b/crop/arrow_right.jpg differ diff --git a/crop/bigbomb.jpg b/crop/bigbomb.jpg new file mode 100644 index 0000000..492214f Binary files /dev/null and b/crop/bigbomb.jpg differ diff --git a/crop/bomb.jpg b/crop/bomb.jpg new file mode 100644 index 0000000..29bb142 Binary files /dev/null and b/crop/bomb.jpg differ diff --git a/crop/next_level.jpg b/crop/next_level.jpg new file mode 100644 index 0000000..e81710c Binary files /dev/null and b/crop/next_level.jpg differ diff --git a/crop/next_level_x.jpg b/crop/next_level_x.jpg new file mode 100644 index 0000000..3dc0ad9 Binary files /dev/null and b/crop/next_level_x.jpg differ diff --git a/crop/rainbow.jpg b/crop/rainbow.jpg new file mode 100644 index 0000000..d52e9d9 Binary files /dev/null and b/crop/rainbow.jpg differ diff --git a/crop/screenshot_pathed.jpg b/crop/screenshot_pathed.jpg index 3cbb334..3b370c4 100644 Binary files a/crop/screenshot_pathed.jpg and b/crop/screenshot_pathed.jpg differ diff --git a/crop/screenshot_scored.jpg b/crop/screenshot_scored.jpg index d26e632..7a9999f 100644 Binary files a/crop/screenshot_scored.jpg and b/crop/screenshot_scored.jpg differ