Commit a296ca9c authored by Taddeüs Kroes's avatar Taddeüs Kroes

Add detection test suite

parent 05436333
__pycache__
*.swp
screens/bitmap*.png
screens/all.png
#!/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))
ybyyryp
bpgpbyg
rgbrprg
|
--( )--
ybgpbpr
ggybrrp
yGgbgrb
bbggY..
bpb....
py.....
p......
|
----( )
rgrypbr
gr.pbpb
gr.Pbp.
yg..rg.
rr..rg.
p....g.
|
----( )
rgrggrb
yGbbrpg
bgggyyr
prbbypp
rbrypyp
gbbgpgg
yppPyGr
...R...
|
----( )
pbgrpbg
yb.bpgy
yr..byY
yb..ryp
pr..grp
bb..yyB
gg..rr.
.g..yP.
|
-----( )
ybggrpy
Ryrgbpg
rb.Y.yb
g..r..b
g..b...
|
-( )---
prbbbrr
rygp.gb
pybg.yy
Ypbg.yp
gpBp..p
Rr.....
.r.....
.b.....
.b.....
|
--( )--
ggB.ygy
.PG..p.
..Y..y.
.....y.
|
-( )---
bbrbppb
ybBGbrg
|
--( )--
Bgypybg
gbbygrr
ypppbpy
yG...yr
R......
|
---(y)-
pyyrgyb
pbggpgy
gy.bpGg
gr.byyb
RG.ppyr
....B..
....P..
|
-----( )
rbybpgg
pyrgypy
ryrpgry
prggbRb
ybyyryp
bpgpbyg
rgbrprg
|
--( )--
pgrbyry
pgpprry
rybbggb
rrpyrby
pBpygpp
rGpbbyy
...b...
|
( )-----
grpgpyB
ggbb..y
.Y.....
|
( )-----
ygyyggr
yGbbgby
ryyrrgg
gppbyrp
pbbppry
yybpGbg
yprrbpg
bpgypbr
rpgrgrb
|
--( )--
gYrbbgg
ybygpRr
rrpybbg
rggryyb
bpypprg
grypbgy
Ypyggry
pbbgprp
.prbbrb
|
(y)----
byrbpYp
ggrgrbg
pgBpbbr
.rgrrpp
..gprpb
..PY.gb
......g
|
---( )-
pbpbpry
yr..rrg
....pyy
....rp.
|
-(R)---
yypbybr
ybgbpry
prrppgg
.pyR.gb
.....pp
......p
|
--(Y)--
ypyygbr
bbygbgp
ygbpbRy
byggygr
ppypygr
bpbbr.r
|
-----(p)
rbgrrgP
bppgygy
rgygbpy
pyp.pyb
|
(r)-----
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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment