|
|
@@ -11,10 +11,10 @@ GET = ((GRAB,), (SWAP, GRAB), (GRAB, SWAP, DROP, SWAP, GRAB))
|
|
|
PUT = ((DROP,), (DROP, SWAP), (DROP, SWAP, GRAB, SWAP, DROP))
|
|
|
MIN_BASIC_GROUP_SIZE = 4
|
|
|
MIN_BOMB_GROUP_SIZE = 2
|
|
|
-FIND_GROUPS_DEPTH = 4
|
|
|
-FRAG_DEPTH = 4
|
|
|
+FIND_GROUPS_DEPTH = 3
|
|
|
+FRAG_DEPTH = 3
|
|
|
COLSIZE_PRIO = 5
|
|
|
-COLSIZE_PRIO_HIGH = 7
|
|
|
+COLSIZE_PANIC = 7
|
|
|
COLSIZE_MAX = 8
|
|
|
BOMB_POINTS = 2
|
|
|
MIN_ROWS = 2
|
|
|
@@ -31,8 +31,6 @@ class State:
|
|
|
skip = self.colskip(self.exa)
|
|
|
i = (skip + 1) * COLUMNS + self.exa
|
|
|
return i < len(self.blocks) and self.blocks[i] == NOBLOCK
|
|
|
- #return any(len(col) > 1 and col[1] == NOBLOCK
|
|
|
- # for col in map(tuple, self.iter_columns()))
|
|
|
|
|
|
def iter_columns(self):
|
|
|
nrows = self.nrows()
|
|
|
@@ -60,11 +58,6 @@ class State:
|
|
|
for col in range(COLUMNS):
|
|
|
yield self.nrows() - self.colskip(col)
|
|
|
|
|
|
- #def highest_column(self):
|
|
|
- # for i, block in enumerate(self.blocks):
|
|
|
- # if block != NOBLOCK:
|
|
|
- # return self.nrows() - i // COLUMNS
|
|
|
-
|
|
|
def empty_column_score(self):
|
|
|
skip = 0
|
|
|
for i, block in enumerate(self.blocks):
|
|
|
@@ -82,23 +75,13 @@ class State:
|
|
|
return score
|
|
|
|
|
|
def score(self, points, moves, prev):
|
|
|
- #colsizes = list(self.colsizes())
|
|
|
- #mincol = min(colsizes)
|
|
|
- #maxcol = max(colsizes)
|
|
|
- #colsize_score = maxcol, colsizes.count(maxcol) #, -mincol
|
|
|
- #colsize_score = tuple(sorted(colsizes, reverse=True))
|
|
|
- #if prev.nrows() >= 6:
|
|
|
- # return colsize_score, -points, frag, len(moves)
|
|
|
-
|
|
|
- #colsize_score = maxcol, self.empty_column_score()
|
|
|
-
|
|
|
frag = self.fragmentation()
|
|
|
colsize_score = self.empty_column_score()
|
|
|
#return -points, frag + colsize_score, len(moves)
|
|
|
|
|
|
frag += colsize_score
|
|
|
prev_colsize = max(prev.colsizes())
|
|
|
- if prev_colsize >= COLSIZE_PRIO_HIGH:
|
|
|
+ if prev_colsize >= COLSIZE_PANIC:
|
|
|
return colsize_score, len(moves), -points, frag
|
|
|
elif prev_colsize >= COLSIZE_PRIO:
|
|
|
return -points, colsize_score, frag, len(moves)
|
|
|
@@ -106,10 +89,6 @@ class State:
|
|
|
return -points, frag, colsize_score, len(moves)
|
|
|
|
|
|
def score_moves(self):
|
|
|
- # clear exploding blocks before computing colsize
|
|
|
- #prev = self.copy()
|
|
|
- #prev.score_points()
|
|
|
-
|
|
|
for moves in self.gen_moves():
|
|
|
try:
|
|
|
points, newstate = self.simulate(moves)
|
|
|
@@ -147,9 +126,6 @@ class State:
|
|
|
s = self.copy()
|
|
|
points = 0
|
|
|
|
|
|
- #if not moves:
|
|
|
- # return s.score_points(), s
|
|
|
-
|
|
|
# avoid swapping/grabbing currently exploding items
|
|
|
#unmoveable = s.find_unmovable_blocks()
|
|
|
|
|
|
@@ -228,15 +204,18 @@ class State:
|
|
|
|
|
|
def fragmentation(self, depth=FRAG_DEPTH):
|
|
|
"""
|
|
|
- Minimize the sum of dist(i,j) for all blocks i,j of the same color.
|
|
|
- Prioritize horitontal distance to avoid column stacking.
|
|
|
+ Minimize the sum of dist(i,j) between all blocks i,j of the same color.
|
|
|
+ Magnify vertical distances to avoid column stacking.
|
|
|
"""
|
|
|
def dist(i, j):
|
|
|
yi, xi = divmod(i, COLUMNS)
|
|
|
yj, xj = divmod(j, COLUMNS)
|
|
|
+
|
|
|
+ # for blocks in the same group, only count vertical distance so that
|
|
|
+ # groups are spread out horizontally
|
|
|
if groups[i] == groups[j]:
|
|
|
return abs(yj - yi)
|
|
|
- #return abs(xj - xi) * 2 + abs(yj - yi) - 1
|
|
|
+
|
|
|
return abs(xj - xi) + abs(yj - yi) * 2 - 1
|
|
|
|
|
|
colors = {}
|
|
|
@@ -345,7 +324,6 @@ def moves_to_keys(moves):
|
|
|
if __name__ == '__main__':
|
|
|
import sys
|
|
|
from PIL import Image
|
|
|
- #from pprint import pprint
|
|
|
|
|
|
board = Image.open('screens/board%d.png' % int(sys.argv[1])).convert('HSV')
|
|
|
state = State.detect(board)
|
|
|
@@ -371,7 +349,3 @@ if __name__ == '__main__':
|
|
|
|
|
|
for score, moves in sorted(state.score_moves()):
|
|
|
print('move %18s:' % moves_to_keys(moves), score)
|
|
|
- #print('moves:', moves_to_keys(moves), moves)
|
|
|
- #print('score:', score)
|
|
|
-
|
|
|
- #print('\nmoves:', moves_to_keys(state.solve()))
|