Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWalter Bender <walter@walter-laptop.(none)>2009-10-09 06:10:52 (GMT)
committer Walter Bender <walter@walter-laptop.(none)>2009-10-09 06:10:52 (GMT)
commitf4e9be649585977bda66bcdafda9f25fb894c38d (patch)
tree8d8aabf8c340e3ddd1a25b2fc8b65122e8f4cd8a
parentd692af30c7dfa822bd9b049a01d27ac706254f0a (diff)
speed up of solver code
-rw-r--r--card.py11
-rwxr-xr-xcardsort.py17
-rw-r--r--orientation.py27
3 files changed, 32 insertions, 23 deletions
diff --git a/card.py b/card.py
index 22ed848..0f43b02 100644
--- a/card.py
+++ b/card.py
@@ -57,11 +57,11 @@ class Card:
'.svg'), \
int(wh), int(wh))
- def set_orientation(self,r):
+ def set_orientation(self,r,rotate_spr=True):
while r != self.orientation:
- self.rotate_ccw()
+ self.rotate_ccw(rotate_spr)
- def rotate_ccw(self):
+ def rotate_ccw(self,rotate_spr=True):
# print "rotating card " + str(self.spr.label)
tmp = self.north
self.north = self.east
@@ -71,8 +71,9 @@ class Card:
self.orientation += 90
if self.orientation > 359:
self.orientation -= 360
- tmp = self.spr.image.rotate_simple(90)
- self.spr.image = tmp
+ if rotate_spr is True:
+ tmp = self.spr.image.rotate_simple(90)
+ self.spr.image = tmp
def print_card(self):
print "(" + str(self.north) + "," + str(self.east) + \
diff --git a/cardsort.py b/cardsort.py
index 553e2f8..2084cad 100755
--- a/cardsort.py
+++ b/cardsort.py
@@ -41,9 +41,9 @@ class CardSortMain:
self.win = gtk.Window(gtk.WINDOW_TOPLEVEL)
# for some reason, full screen width/height doesn't work
self.win.set_size_request(
- int(gtk.gdk.screen_width()-32), \
+ int(gtk.gdk.screen_width()-32),
int(gtk.gdk.screen_height()-32))
- self.win.set_title(_("CardSort") + ": " + \
+ self.win.set_title(_("CardSort") + ": " +
_("click to rotate; drag to swap"))
self.win.connect("delete_event", lambda w,e: gtk.main_quit())
@@ -105,6 +105,7 @@ class CardSortMain:
def _solve_cb(self, widget):
rotation_sets = get_rotation_sets()
+ counter = 0
for i in range(9):
for j in range(9):
@@ -122,16 +123,20 @@ class CardSortMain:
for b in range(9):
if b in [a,x,y,z,i,j,k]: continue
for c in range(9):
- if c in [a,b,x,y,z,i,j,k]:
- continue
+ if c in [a,b,x,y,z,i,j,k]: continue
self.tw.grid.grid = [i,j,k,x,y,z,a,b,c]
+ counter += 1
+ if (counter/1000)*1000 == counter:
+ print counter
for o in range(64):
for j in range(9):
- self.tw.grid.card_table[self.tw.grid.grid.index(j)].set_orientation(rotation_sets[o][j])
+ self.tw.grid.card_table[self.tw.grid.grid.index(j)].set_orientation(rotation_sets[o][j],False)
if self.tw.grid.test() is True:
+ self.tw.grid.card_table[self.tw.grid.grid.index(j)].set_orientation(rotation_sets[o][j],True)
sprites.redrawsprites(self.tw)
print self.tw.grid
- print o
+ print rotation_sets[o]
+ self.tw.win.set_title(_("CardSort") + ": " + _("You solved the puzzle."))
return True
print "no solution found :("
return True
diff --git a/orientation.py b/orientation.py
index d31579c..1e994e1 100644
--- a/orientation.py
+++ b/orientation.py
@@ -4,11 +4,11 @@
# initial position
#
# Since the gender of a neighboring edge has to be opposite, the following
-# constraints apply:
+# constraints apply (top,left):
#
-ORIENTATION_CONSTRAINTS = [(-1,-1),(0,-1),(1,-1), \
- (-1,0), (3,1), (4,2), \
- (-1,3), (6,4), (7,5)]
+# ORIENTATION_CONSTRAINTS = [(-1,-1),(-1,0),(-1,1), \
+# (0,-1), (1,3), (2,4), \
+# (3,-1), (4,6), (5,7)]
# where the pairs are (left dependency, top dependency)
# and -1 indicates an edge, i.e., no dpendency
#
@@ -19,11 +19,11 @@ ROTS = (0,90,180,270)
# constraint tables
LEFT = {0:(0,270),90:(90,180),180:(90,180),270:(0,270)}
-TOP = {0:(0,90),90:(0,90),180:(180,270),270:(180,270)}
+TOP = {0:(0,90),90:(0,90),180:(180,270),270:(180,270)}
TOP_LEFT = [{0:0,90:90,180:90,270:0}, \
- {0:0,90:90,180:90,270:0}, \
- {0:270,90:180,180:180,270:270}, \
- {0:270,90:180,180:180,270:270}]
+ {0:0,90:90,180:90,270:0}, \
+ {0:270,90:180,180:180,270:270}, \
+ {0:270,90:180,180:180,270:270}]
def get_rotation_sets():
@@ -52,8 +52,8 @@ def get_rotation_sets():
# fourth tile is constrained to the top only (16 groups of 2x2
for i in range(16):
for j in range(2):
- rot_sets[i*4+j].append(TOP[rot_sets[i*4+j][1]][0])
- rot_sets[i*4+j+2].append(TOP[rot_sets[i*4+j+2][1]][1])
+ rot_sets[i*4+j].append(TOP[rot_sets[i*4+j][0]][0])
+ rot_sets[i*4+j+2].append(TOP[rot_sets[i*4+j+2][0]][1])
# fifth tile is constrained by top and left
# sixth tile is constrained by top and left
@@ -63,8 +63,8 @@ def get_rotation_sets():
# seventh tile is constrained to the top only (32 groups of 1x2)
for i in range(32):
- rot_sets[i*2].append(TOP[rot_sets[i*2][4]][0])
- rot_sets[i*2+1].append(TOP[rot_sets[i*2+1][4]][1])
+ rot_sets[i*2].append(TOP[rot_sets[i*2][3]][0])
+ rot_sets[i*2+1].append(TOP[rot_sets[i*2+1][3]][1])
# eigth tile is constrained by top and left
# ninth tile is constrained by top and left
@@ -72,4 +72,7 @@ def get_rotation_sets():
rot_sets[i].append(TOP_LEFT[ROTS.index(rot_sets[i][4])][rot_sets[i][6]])
rot_sets[i].append(TOP_LEFT[ROTS.index(rot_sets[i][5])][rot_sets[i][7]])
+ # for i in range(64):
+ # print str(i) + ": " + str(rot_sets[i])
+
return rot_sets