瀏覽代碼

Improve bomb detection

Taddeus Kroes 5 年之前
父節點
當前提交
9b4a645bc8
共有 1 個文件被更改,包括 21 次插入8 次删除
  1. 21 8
      parse.py

+ 21 - 8
parse.py

@@ -22,7 +22,9 @@ HUE_TOLERANCE = 5
 DETECT_BASIC_X = 9
 DETECT_BASIC_Y = 15
 DETECT_BOMB_X = 22
-DETECT_BOMB_Y = 45
+DETECT_BOMB_Y = 43
+DETECT_BOMB_Y_TOLERANCE = 7
+DETECT_BOMB_CHECK = 9, 20, 137, 230, 9  # x, y, hue, value, tolerance
 MIN_BASIC_SAT = 180
 MIN_BOMB_SAT = 130
 
@@ -79,13 +81,24 @@ def detect_block_type(board, x, y):
             if is_hue(h, hexpect):
                 return ty
 
-    # if no basic block is detected, check another pixel for bomb contents
-    h, s, v = board.getpixel((x + DETECT_BOMB_X,
-                              y + DETECT_BOMB_Y))
-    if s >= MIN_BOMB_SAT:
-        for ty, hexpect in enumerate(BOMB_HUES):
-            if is_hue(h, hexpect):
-                return ty + BOMB_OFFSET
+    # if no basic block is detected, check another range of pixels for bomb
+    # contents
+    def check_bomb():
+        dx, dy, hexpect, minval, tolerance = DETECT_BOMB_CHECK
+        for offset in range(tolerance):
+            h, s, v = board.getpixel((x + dx, y + dy + offset))
+            if is_hue(h, hexpect) and v >= minval:
+                return True
+        return False
+
+    if check_bomb():
+        for offset in range(DETECT_BOMB_Y_TOLERANCE):
+            h, s, v = board.getpixel((x + DETECT_BOMB_X,
+                                    y + DETECT_BOMB_Y + offset))
+            if s >= MIN_BOMB_SAT:
+                for basic_ty, hexpect in enumerate(BOMB_HUES):
+                    if is_hue(h, hexpect):
+                        return basic_ty + BOMB_OFFSET
 
     # no basic block or bomb -> empty slot
     return NOBLOCK