Bläddra i källkod

Add detection test suite

Taddeus Kroes 5 år sedan
förälder
incheckning
a296ca9cbf

+ 2 - 0
.gitignore

@@ -1,2 +1,4 @@
 __pycache__
 *.swp
+screens/bitmap*.png
+screens/all.png

+ 39 - 0
check.py

@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+import glob
+import os
+from PIL import Image
+from strategy import State
+
+
+def saved_idents():
+    return sorted(int(path[13:-4]) for path in glob.glob('screens/board*.png'))
+
+
+if __name__ == '__main__':
+    ok = fail = notfound = 0
+
+    for ident in saved_idents():
+        truth_path = 'screens/true%d.txt' % ident
+        if os.path.exists(truth_path):
+            board = Image.open('screens/board%d.png' % ident).convert('HSV')
+            state = State.detect(board, pad=0)
+            detected = state.tostring()
+            with open(truth_path) as f:
+                expected = f.read()
+            if detected != expected:
+                print('=' * 20)
+                print('board', ident, 'FAIL')
+                print('expected:')
+                print(expected)
+                print('detected:')
+                print(detected, end='')
+                print('=' * 20)
+                fail += 1
+            else:
+                print('board', ident, 'OK')
+                ok += 1
+        else:
+            print(truth_path, 'does not exist')
+            notfound += 1
+
+    print('%d boards, %d ok, %d fail' %(ok + fail, ok, fail))

BIN
screens/board1.png


BIN
screens/board10.png


BIN
screens/board11.png


BIN
screens/board12.png


BIN
screens/board13.png


BIN
screens/board14.png


BIN
screens/board15.png


BIN
screens/board16.png


BIN
screens/board17.png


BIN
screens/board18.png


BIN
screens/board19.png


BIN
screens/board2.png


BIN
screens/board20.png


BIN
screens/board21.png


BIN
screens/board3.png


BIN
screens/board4.png


BIN
screens/board5.png


BIN
screens/board6.png


BIN
screens/board7.png


BIN
screens/board8.png


BIN
screens/board9.png


+ 5 - 0
screens/true1.txt

@@ -0,0 +1,5 @@
+ ybyyryp
+ bpgpbyg
+ rgbrprg
+    |
+ --( )--

+ 9 - 0
screens/true10.txt

@@ -0,0 +1,9 @@
+ ybgpbpr
+ ggybrrp
+ yGgbgrb
+ bbggY..
+ bpb....
+ py.....
+ p......
+      |
+ ----( )

+ 8 - 0
screens/true11.txt

@@ -0,0 +1,8 @@
+ rgrypbr
+ gr.pbpb
+ gr.Pbp.
+ yg..rg.
+ rr..rg.
+ p....g.
+      |
+ ----( )

+ 10 - 0
screens/true12.txt

@@ -0,0 +1,10 @@
+ rgrggrb
+ yGbbrpg
+ bgggyyr
+ prbbypp
+ rbrypyp
+ gbbgpgg
+ yppPyGr
+ ...R...
+      |
+ ----( )

+ 10 - 0
screens/true13.txt

@@ -0,0 +1,10 @@
+ pbgrpbg
+ yb.bpgy
+ yr..byY
+ yb..ryp
+ pr..grp
+ bb..yyB
+ gg..rr.
+ .g..yP.
+       |
+ -----( )

+ 7 - 0
screens/true14.txt

@@ -0,0 +1,7 @@
+ ybggrpy
+ Ryrgbpg
+ rb.Y.yb
+ g..r..b
+ g..b...
+   |
+ -( )---

+ 11 - 0
screens/true15.txt

@@ -0,0 +1,11 @@
+ prbbbrr
+ rygp.gb
+ pybg.yy
+ Ypbg.yp
+ gpBp..p
+ Rr.....
+ .r.....
+ .b.....
+ .b.....
+    |
+ --( )--

+ 6 - 0
screens/true16.txt

@@ -0,0 +1,6 @@
+ ggB.ygy
+ .PG..p.
+ ..Y..y.
+ .....y.
+   |
+ -( )---

+ 4 - 0
screens/true17.txt

@@ -0,0 +1,4 @@
+ bbrbppb
+ ybBGbrg
+    |
+ --( )--

+ 7 - 0
screens/true18.txt

@@ -0,0 +1,7 @@
+ Bgypybg
+ gbbygrr
+ ypppbpy
+ yG...yr
+ R......
+     |
+ ---(y)-

+ 9 - 0
screens/true19.txt

@@ -0,0 +1,9 @@
+ pyyrgyb
+ pbggpgy
+ gy.bpGg
+ gr.byyb
+ RG.ppyr
+ ....B..
+ ....P..
+       |
+ -----( )

+ 9 - 0
screens/true2.txt

@@ -0,0 +1,9 @@
+ rbybpgg
+ pyrgypy
+ ryrpgry
+ prggbRb
+ ybyyryp
+ bpgpbyg
+ rgbrprg
+    |
+ --( )--

+ 9 - 0
screens/true20.txt

@@ -0,0 +1,9 @@
+ pgrbyry
+ pgpprry
+ rybbggb
+ rrpyrby
+ pBpygpp
+ rGpbbyy
+ ...b...
+ |
+( )-----

+ 5 - 0
screens/true21.txt

@@ -0,0 +1,5 @@
+ grpgpyB
+ ggbb..y
+ .Y.....
+ |
+( )-----

+ 11 - 0
screens/true3.txt

@@ -0,0 +1,11 @@
+ ygyyggr
+ yGbbgby
+ ryyrrgg
+ gppbyrp
+ pbbppry
+ yybpGbg
+ yprrbpg
+ bpgypbr
+ rpgrgrb
+    |
+ --( )--

+ 11 - 0
screens/true4.txt

@@ -0,0 +1,11 @@
+ gYrbbgg
+ ybygpRr
+ rrpybbg
+ rggryyb
+ bpypprg
+ grypbgy
+ Ypyggry
+ pbbgprp
+ .prbbrb
+  |
+ (y)----

+ 9 - 0
screens/true5.txt

@@ -0,0 +1,9 @@
+ byrbpYp
+ ggrgrbg
+ pgBpbbr
+ .rgrrpp
+ ..gprpb
+ ..PY.gb
+ ......g
+     |
+ ---( )-

+ 6 - 0
screens/true6.txt

@@ -0,0 +1,6 @@
+ pbpbpry
+ yr..rrg
+ ....pyy
+ ....rp.
+   |
+ -(R)---

+ 8 - 0
screens/true7.txt

@@ -0,0 +1,8 @@
+ yypbybr
+ ybgbpry
+ prrppgg
+ .pyR.gb
+ .....pp
+ ......p
+    |
+ --(Y)--

+ 8 - 0
screens/true8.txt

@@ -0,0 +1,8 @@
+ ypyygbr
+ bbygbgp
+ ygbpbRy
+ byggygr
+ ppypygr
+ bpbbr.r
+       |
+ -----(p)

+ 6 - 0
screens/true9.txt

@@ -0,0 +1,6 @@
+ rbgrrgP
+ bppgygy
+ rgygbpy
+ pyp.pyb
+ |
+(r)-----

+ 10 - 2
strategy.py

@@ -1,4 +1,6 @@
+import io
 import time
+from contextlib import redirect_stdout
 from itertools import combinations, islice
 from parse import COLUMNS, NOBLOCK, detect_blocks, detect_exa, \
                   detect_held, print_board, is_basic, is_bomb, bomb_to_basic
@@ -45,8 +47,8 @@ class State:
             yield gen_col(col)
 
     @classmethod
-    def detect(cls, board):
-        blocks = [NOBLOCK] * (COLUMNS * 2) + list(detect_blocks(board))
+    def detect(cls, board, pad=2):
+        blocks = [NOBLOCK] * (COLUMNS * pad) + list(detect_blocks(board))
         exa = detect_exa(board)
         held = detect_held(board, exa)
         return cls(blocks, exa, held)
@@ -325,6 +327,12 @@ class State:
     def print(self):
         print_board(self.blocks, self.exa, self.held)
 
+    def tostring(self):
+        stream = io.StringIO()
+        with redirect_stdout(stream):
+            self.print()
+        return stream.getvalue()
+
     def nrows(self):
         return len(self.blocks) // COLUMNS