diff --git a/crop/Field_Representation_crop.py b/crop/Field_Representation_crop.py index 0fdf4ee..689c205 100644 --- a/crop/Field_Representation_crop.py +++ b/crop/Field_Representation_crop.py @@ -46,12 +46,14 @@ class Field: reset_confirm = cv.imread("reset_confirm.jpg", cv.IMREAD_COLOR) reset_counter = 0 colors_at_standard = True + needles = {} + hsh_needles = {} 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) @@ -86,41 +88,7 @@ class Field: # initialize the StunVision class self.vision_stun = Vision() - self.needles = {GREEN: cv.imread("green.jpg", cv.IMREAD_COLOR), - YELLOW: cv.imread("yellow.jpg", cv.IMREAD_COLOR), - RED: cv.imread("red.jpg", cv.IMREAD_COLOR), - BLUE: cv.imread("blue.jpg", cv.IMREAD_COLOR), - PURPLE: cv.imread("purple.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), - # BURGER: cv.imread("burger.jpg", cv.IMREAD_COLOR), - # GOLDBAR: cv.imread("burger.jpg", cv.IMREAD_COLOR), - # PAB1: cv.imread("pab1.jpg", cv.IMREAD_COLOR), - MAGINENT: cv.imread("maginent.jpg", cv.IMREAD_COLOR), - CHEMTRANT: cv.imread("chemtrant.jpg", cv.IMREAD_COLOR), - TENESENT: cv.imread("tenesent.jpg", cv.IMREAD_COLOR), - CIBUTRANT: cv.imread("cibutrant.jpg", cv.IMREAD_COLOR), - ARTISENT: cv.imread("artisent.jpg", cv.IMREAD_COLOR) - } - self.hsh_needles = {GREEN: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), - YELLOW: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), - PURPLE: HsvFilter(120, 45, 0, 170, 255, 255, 0, 0, 0, 0), - BLUE: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), - RED: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), - RAINBOW: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), - MAGINENT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), - CHEMTRANT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), - TENESENT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), - CIBUTRANT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0), - ARTISENT: HsvFilter(0, 0, 0, 179, 255, 255, 0, 0, 0, 0) - } + self.set_color_order((GREEN, YELLOW, RED, BLUE, PURPLE)) def reset(self): self.observation = [] @@ -134,10 +102,10 @@ class Field: order[4]: cv.imread("purple.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) + 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), @@ -174,7 +142,12 @@ class Field: # no movement detected -> blow explosives or reset self.reset_counter += 1 - if self.reset_counter >= 3: + if self.reset_counter == 1: + pass + elif self.reset_counter == 2: + self.detonate_explosive_when_stuck(new_observation) + return + elif self.reset_counter >= 3: screenshot = self.capture_window.get_screenshot() if self.check_for_button_and_execute(screenshot, self.reset_board): cv.waitKey(500) @@ -190,8 +163,8 @@ class Field: self.colors_at_standard = True self.reset_counter = 0 return - self.detonate_explosive_when_stuck(new_observation) - return + else: + return self.find_patterns_and_valid_moves(new_observation) self.observation = new_observation @@ -561,7 +534,6 @@ class Field: return score - def is_direction_in_bounce_and_same_color(self, state, loc, color): x, y = loc if x <= 7 and x >= 0 and y <= 13 and y >= 0: @@ -585,198 +557,6 @@ class Field: continue return False - def check_5_horizontal(self, state, e, i, color): - try: - # - if state[e, i] == color and state[e, i + 1] == color and state[e, i + 3] == color and state[ - e, i + 4] == color: - if state[e, i + 2] <= 0 or state[e, i + 2] >= 6: - return False - # - if e - 1 >= 0 and i + 2 <= 13: - if state[e - 1, i + 2] == color: - return True - return False - except: - return False - - def check_5_vertical(self, state, e, i, color): - try: - # - if state[e, i] == color and state[e + 1, i] == color and state[e + 3, i] == color and state[ - e + 4, i] == color: - if state[e + 2, i] <= 0 or state[e + 2, i] >= 6: - return False - # third left upper - if e + 2 <= 7 and i - 1 >= 0: - if state[e + 2, i - 1] == color: - print("upper left", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e + 2, i - 1]) - dest_pt = self.get_click_point(self.data_coordinates[e + 2, i]) - self.move_tile(src_pt, dest_pt) - return True - if e + 2 <= 7 and i + 1 <= 13: - if state[e + 2, i + 1] == color: - print("upper left", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e + 2, i + 1]) - dest_pt = self.get_click_point(self.data_coordinates[e + 2, i]) - self.move_tile(src_pt, dest_pt) - return True - return False - except: - return False - - def check_3_with_gap(self, state, e, i, color): - try: - # second color next to starting point - if i + 2 <= 13: - if state[e, i] == color and state[e, i + 2] == color: - # third upper - if e - 1 >= 0 and i + 1 <= 13: - # if state[e - 1, i - 1] == color and (state[e, i - 1] >= 1 and state[e, i - 1] <= 5): - if state[e - 1, i + 1] == color and (state[e, i + 1] in self.colors): - src_pt = self.get_click_point(self.data_coordinates[e - 1, i + 1]) - dest_pt = self.get_click_point(self.data_coordinates[e, i + 1]) - self.move_tile(src_pt, dest_pt) - return True - # third left lower - if e + 1 <= 7 and i + 1 <= 13: - if state[e + 1, i + 1] == color and (state[e, i + 1] in self.colors): - src_pt = self.get_click_point(self.data_coordinates[e + 1, i + 1]) - dest_pt = self.get_click_point(self.data_coordinates[e, i + 1]) - self.move_tile(src_pt, dest_pt) - return True - if e + 2 <= 7: - if state[e, i] == color and state[e + 2, i] == color: - # third upper - if e + 1 >= 0 and i + 1 <= 13: - # if state[e - 1, i - 1] == color and (state[e, i - 1] >= 1 and state[e, i - 1] <= 5): - if state[e + 1, i + 1] == color and (state[e + 1, i] in self.colors): - src_pt = self.get_click_point(self.data_coordinates[e + 1, i + 1]) - dest_pt = self.get_click_point(self.data_coordinates[e + 1, i]) - self.move_tile(src_pt, dest_pt) - return True - # third left lower - if e + 1 <= 7 and i - 1 >= 0: - if state[e + 1, i - 1] == color and (state[e + 1, i] in self.colors): - src_pt = self.get_click_point(self.data_coordinates[e + 1, i - 1]) - dest_pt = self.get_click_point(self.data_coordinates[e + 1, i]) - self.move_tile(src_pt, dest_pt) - return True - except: - return False - - def check_3_horizontal(self, state, e, i, color): - try: - # second color next to starting point - if state[e, i] == color and state[e, i + 1] == color: - # third left upper - if e - 1 >= 0 and i - 1 >= 0: - if state[e - 1, i - 1] == color and (state[e, i - 1] in self.colors): - print("upper left", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e - 1, i - 1]) - dest_pt = self.get_click_point(self.data_coordinates[e, i - 1]) - self.move_tile(src_pt, dest_pt) - return True - # third left lower - if e + 1 <= 7 and i - 1 >= 0: - if state[e + 1, i - 1] == color and (state[e, i - 1] in self.colors): - print("lower left", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e + 1, i - 1]) - dest_pt = self.get_click_point(self.data_coordinates[e, i - 1]) - self.move_tile(src_pt, dest_pt) - return True - # third left with gap - if i - 2 >= 0: - if state[e, i - 2] == color and (state[e, i - 1] in self.colors): - print("left gap ", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e, i - 2]) - dest_pt = self.get_click_point(self.data_coordinates[e, i - 1]) - self.move_tile(src_pt, dest_pt) - return True - # third right upper - if e - 1 >= 0 and i + 2 <= 13: - if state[e - 1, i + 2] == color and (state[e, i + 2] in self.colors): - print("upper right", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e - 1, i + 2]) - dest_pt = self.get_click_point(self.data_coordinates[e, i + 2]) - self.move_tile(src_pt, dest_pt) - return True - # third right lower - if e + 1 <= 7 and i + 2 <= 13: - if state[e + 1, i + 2] == color and (state[e, i + 2] in self.colors): - print("upper lower", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e + 1, i + 2]) - dest_pt = self.get_click_point(self.data_coordinates[e, i + 2]) - self.move_tile(src_pt, dest_pt) - return True - - # third right with gap - if i + 3 <= 13: - if state[e, i + 3] == color and (state[e, i + 2] in self.colors): - print("right gap ", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e, i + 3]) - dest_pt = self.get_click_point(self.data_coordinates[e, i + 2]) - self.move_tile(src_pt, dest_pt) - return True - except: - return False - - def check_3_vertical(self, state, e, i, color): - try: - # second color next to starting point o - if state[e, i] == color and state[e + 1, i] == color: - # third left upper - if e - 1 >= 0 and i - 1 >= 0: - if state[e - 1, i - 1] == color and (state[e - 1, i] in self.colors): - print("upper left", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e - 1, i - 1]) - dest_pt = self.get_click_point(self.data_coordinates[e - 1, i]) - self.move_tile(src_pt, dest_pt) - return True - # third left lower - if e + 2 <= 7 and i - 1 >= 0: - if state[e + 2, i - 1] == color and (state[e + 2, i] in self.colors): - print("lower left", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e + 2, i - 1]) - dest_pt = self.get_click_point(self.data_coordinates[e + 2, i]) - self.move_tile(src_pt, dest_pt) - return True - # third right upper - if e - 1 >= 0 and i + 1 <= 13: - if state[e - 1, i + 1] == color and (state[e - 1, i] in self.colors): - print("upper right", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e - 1, i + 1]) - dest_pt = self.get_click_point(self.data_coordinates[e - 1, i]) - self.move_tile(src_pt, dest_pt) - return True - # third right lower - if e + 2 <= 7 and i + 1 <= 13: - if state[e + 2, i + 1] == color and (state[e + 2, i] in self.colors): - print("upper lower", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e + 2, i + 1]) - dest_pt = self.get_click_point(self.data_coordinates[e + 2, i]) - self.move_tile(src_pt, dest_pt) - return True - # third upper with gap - if e - 2 >= 0: - if state[e - 2, i] == color and (state[e - 1, i] in self.colors): - print("upper gap ", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e - 2, i]) - dest_pt = self.get_click_point(self.data_coordinates[e - 1, i]) - self.move_tile(src_pt, dest_pt) - return True - # third lower with gap - if e + 3 <= 7: - if state[e + 3, i] == color and (state[e + 2, i] in self.colors): - print("lower gap ", color, e, i) - src_pt = self.get_click_point(self.data_coordinates[e + 3, i]) - dest_pt = self.get_click_point(self.data_coordinates[e + 2, i]) - self.move_tile(src_pt, dest_pt) - return True - except: - return False - def move_tile(self, point_source, point_dest): offset_left = 230 offset_down = 58