Skip to content

Instantly share code, notes, and snippets.

@dcrystalj
Created March 8, 2014 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcrystalj/9430858 to your computer and use it in GitHub Desktop.
Save dcrystalj/9430858 to your computer and use it in GitHub Desktop.
glass crush
import copy
# G
# P
# V
# P R
# P P
# G R
# V V G P P R
solution = []
stage = [[0 for x in xrange(12)] for x in xrange(12)]
#here you define positions of blocks
def initStage():
stage = [[0 for x in xrange(12)] for x in xrange(12)]
stage[1][2] = 1
stage[1][3] = 1
stage[1][4] = 2
stage[1][5] = 3
stage[1][6] = 3
stage[1][7] = 4
stage[2][4] = 2
stage[2][6] = 4
stage[4][4] = 3
stage[4][6] = 3
stage[5][4] = 3
stage[5][6] = 4
stage[6][4] = 1
stage[7][4] = 3
stage[8][4] = 2
popAll(stage)
moveAllDown(stage)
return stage
def clearJ(i, j, stage):
j_new = j+1
i_new = i+1
#clear x axis
while( stage[i][j_new] == stage[i][j] and stage[i][j_new] != 0):
stage[i][j_new] = 0
j_new += 1
def clearI(i, j, stage):
j_new = j+1
i_new = i+1
#clear y axis
while( stage[i_new][j] == stage[i][j] and stage[i_new][j] != 0):
stage[i_new][j] = 0
i_new += 1
#pop all blocks on stage which lenght is at least 3
def popAll(stage):
flag = False
for i in xrange(0,10):
for j in xrange(0,10):
if stage[i][j] == 0:
continue
i_new = i+1
j_new = j+1
#clear x/y axis
while( stage[i][j_new] == stage[i][j] and stage[i][j_new] != 0):
j_new+=1
if j_new - j >= 3:
clearJ(i, j, stage)
while( stage[i_new][j] == stage[i][j] and stage[i_new][j] != 0):
i_new+=1
if i_new - i >= 3:
clearI(i, j, stage)
if i_new - i >= 3 or j_new - j >= 3:
stage[i][j] = 0
flag = True
return flag
def moveLineDown(i, j, stage):
while stage[i][j] == 0 and i < 10:
k = i+1
while stage[k][j] == 0:
k += 1
if k > 10: return
stage[i][j] = stage[k][j]
stage[k][j] = 0
i+=1
#set gravity to blocks
def moveAllDown(stage):
for i in xrange(9, -1, -1):
for j in xrange(0, 9):
if stage[i][j] == 0 and stage[i+1][j] != 0:
moveLineDown(i, j, stage);
#for easier testing
def drawStage(stage):
for i in xrange(10, -1, -1):
for j in xrange(0, 10):
if stage[i][j] == 0:
print " ", " ",
else:
print stage[i][j], " ",
print " "
print " "
def stageCleared(stage):
for i in xrange(10, -1, -1):
for j in xrange(0, 10):
if stage[i][j] != 0:
return False
return True
def switchLeft(i, j, stage):
tmp = stage[i][j-1]
stage[i][j-1] = stage[i][j]
stage[i][j] = tmp
def switchRight(i, j, stage):
tmp = stage[i][j+1]
stage[i][j+1] = stage[i][j]
stage[i][j] = tmp
def switchTop(i, j, stage):
tmp = stage[i+1][j]
stage[i+1][j] = stage[i][j]
stage[i][j] = tmp
def switchBottom(i, j, stage):
tmp = stage[i-1][j]
stage[i-1][j] = stage[i][j]
stage[i][j] = tmp
def move(moved, moves, stage):
global solution
#kill recursion
if solution:
return
while(popAll(stage)):
moveAllDown(stage)
if moves == 0:
if stageCleared(stage):
print "solution is:", (moved)
solution = moved
return
oldstage = copy.deepcopy(stage)
for i in xrange(10, -1, -1):
for j in xrange(-1, 10):
if stage[i][j] == 0:
continue
if stage[i][j-1] != stage[i][j]:
switchLeft(i, j, stage)
move(moved+["switch_left " + str(i) + " " + str(j)], moves - 1, stage)
stage = copy.deepcopy(oldstage)
if stage[i][j+1] != stage[i][j]:
switchRight(i, j, stage)
move(moved + ["switch_right " + str(i) + " " + str(j)], moves - 1, stage)
stage = copy.deepcopy(oldstage)
if stage[i+1][j] != 0 and stage[i+1][j] != stage[i][j]:
switchTop(i, j, stage)
move(moved + ["switch_top " + str(i) + " " + str(j)], moves - 1, stage)
stage = copy.deepcopy(oldstage)
if stage[i-1][j] != 0 and stage[i-1][j] != stage[i][j]:
switchBottom(i, j, stage)
move(moved + ["switch_bottom " + str(i) + " " + str(j)], moves - 1, stage)
stage = copy.deepcopy(oldstage)
def drawProcedure(procedure):
stage = initStage()
print "moves are:"
for i in procedure:
print i
if i[0:-4] == "switch_left":
switchLeft(int(i[-3]), int(i[-1]), stage)
if i[0:-4] == "switch_right":
switchRight(int(i[-3]), int(i[-1]), stage)
if i[0:-4] == "switch_top":
switchTop(int(i[-3]), int(i[-1]), stage)
if i[0:-4] == "switch_bottom":
switchBottom(int(i[-3]), int(i[-1]), stage)
for x in xrange(1,10):
popAll(stage)
moveAllDown(stage)
drawStage(stage)
print "solved"
stage = initStage()
#define number of given moves
drawStage(stage)
move([], 3, stage)
#copy of console output here to se move by move
drawProcedure(solution)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment