code-archive/android-Shot-solver/solve.py

158 lines
4.5 KiB
Python

#!/usr/bin/python
# Solves puzzles for Android game Shot (http://www.androidzoom.com/android_games/brain_puzzle/shot-puzzle_xina.html)
# Populate matrix "puzzle" with 1s to represent ball locations
import copy
class wizard:
def __init__(this,puzzle,trail):
# print "----------------------------------------------"
# print "New spawn started with this puzzle:"
# for i in puzzle:
# print i
balls = 0
for x in puzzle:
for y in x:
if y == 1:
balls += 1
# print "Total balls: " + str(balls)
if balls == 1:
columns = len(trail[0])
rowcount = 0
step = 0
# print "ORIGINAL PUZZLE"
for i in trail:
rowcount += 1
print i
if rowcount == columns:
rowcount = 0
step += 1
# print "STEP #" + str(step)
# print "END TRAIL"
exit("Win!")
countx = county = 0
for x in puzzle: # for each row
for y in x: # for each column within each row
if y == 1: # if it found a ball
this.shootball(puzzle,countx,county,trail)
county += 1
county = 0
countx += 1
# print "FAILED...death."
# print "Another wizard died"
# print "----------------------"
def shootball(this,puzzle,ballx,bally,trail):
this.tryright(puzzle,ballx,bally,trail)
this.tryleft(puzzle,ballx,bally,trail)
this.tryup(puzzle,ballx,bally,trail)
this.trydown(puzzle,ballx,bally,trail)
def tryright(this,puzzle,ballx,bally,trail):
workingpuzzle = copy.deepcopy(puzzle)
probex = ballx
probey = bally
numcolumns = len(workingpuzzle[probex])
while (probey < numcolumns - 1):
probey += 1
if workingpuzzle[probex][probey] == 1:
if probey - 1 == bally:
return # if ball is adjacent, can't hit it
trailadd = "(" + str(ballx) + "," + str(bally) + ") RIGHT"
workingtrail = copy.deepcopy(trail)
workingtrail.append(trailadd)
workingpuzzle[ballx][bally] = 0 # remove original ball
while probey < numcolumns:
if workingpuzzle[ballx][probey] == 1:
workingpuzzle[ballx][probey - 1] = 1
workingpuzzle[ballx][probey] = 0
probey += 1
nextwizard = wizard(workingpuzzle,workingtrail)
def tryleft(this,puzzle,ballx,bally,trail):
workingpuzzle = copy.deepcopy(puzzle)
probex = copy.deepcopy(ballx)
probey = copy.deepcopy(bally)
numcolumns = len(workingpuzzle[probex])
while (probey > 0):
probey -= 1
if workingpuzzle[probex][probey] == 1:
if probey + 1 == bally: # if ball is adjacent, can't hit it
return
trailadd = "(" + str(ballx) + "," + str(bally) + ") LEFT"
workingtrail = copy.deepcopy(trail)
workingtrail.append(trailadd)
workingpuzzle[ballx][bally] = 0 # remove original ball
while probey >= 0:
if workingpuzzle[ballx][probey] == 1:
workingpuzzle[ballx][probey + 1] = 1
workingpuzzle[ballx][probey] = 0
probey -= 1
nextwizard = wizard(workingpuzzle,workingtrail)
def trydown(this,puzzle,ballx,bally,trail):
workingpuzzle = copy.deepcopy(puzzle)
probex = ballx
probey = bally
numrows = len(workingpuzzle)
while (probex < numrows - 1):
probex += 1
if workingpuzzle[probex][probey] == 1:
if probex - 1 == ballx:
return # if ball is adjacent, can't hit it
trailadd = "(" + str(ballx) + "," + str(bally) + ") DOWN"
workingtrail = copy.deepcopy(trail)
workingtrail.append(trailadd)
workingpuzzle[ballx][bally] = 0 # remove original ball
while probex < numrows:
if workingpuzzle[probex][bally] == 1:
workingpuzzle[probex -1][bally] = 1
workingpuzzle[probex][bally] = 0
probex += 1
nextwizard = wizard(workingpuzzle,workingtrail)
def tryup(this,puzzle,ballx,bally,trail):
workingpuzzle = copy.deepcopy(puzzle)
probex = copy.deepcopy(ballx)
probey = copy.deepcopy(bally)
numrows = len(workingpuzzle)
while (probex > 0):
probex -= 1
if workingpuzzle[probex][probey] == 1:
if probex + 1 == ballx: # if ball is adjacent, can't hit it
return
trailadd = "(" + str(ballx) + "," + str(bally) + ") UP"
workingtrail = copy.deepcopy(trail)
workingtrail.append(trailadd)
workingpuzzle[ballx][bally] = 0 # remove original ball
while probex >= 0:
if workingpuzzle[probex][bally] == 1:
workingpuzzle[probex + 1][bally] = 1
workingpuzzle[probex][bally] = 0
probex -= 1
nextwizard = wizard(workingpuzzle,workingtrail)
puzzle = [ [0,0,0,0,0,0,0] ,
[0,0,0,0,0,0,0] ,
[0,0,0,0,0,0,0] ,
[0,0,0,0,0,0,0] ,
[0,0,0,0,0,0,0] ,
[0,0,0,0,0,0,0] ,
[0,0,0,0,0,0,0] ]
firstwizard = wizard(puzzle,["Solution:"])
#print "firstwizard died"