Problem with nested lists as arrays

B

benjamin.cordes

Hello,

For a class modeling the block puzzle I use nested lists as arrays.
Within this class there is a method for swaping elements. I have lots
of trouble with that method but can't figure it out. It has probably to
do with my unuseful approach to nested lists, but I don't want to write
the code again. As I am a python newbie any other comments on the code
will be appreciated. Thanks for any help.

# Puzzle.py
# class for a sliding block puzzle

# an starting state of a 8-puzzle could look like the following:
# ------------
# | 7 2 4 |
# | |
# | 5 X 6 |
# | |
# | 8 3 1 |
# ------------

# the goal is to reach this state:
# ------------
# | X 1 2 |
# | |
# | 3 4 5 |
# | |
# | 6 7 8 |
# ------------

import copy

class Puzzle:

def __init__(self, dim):
self.dim = dim
self.elements = [[0 for column in range(dim)] for row in
range(dim) ]


def getEmptySlot(self):
i = 0
j = 0
while i <= self.dim-1:
while j <= self.dim-1:
if self.elements[j] == -1:
return [j, i]
j = j+1
j = 0
i = i + 1

def performMove(self, direction):
slot = self.getEmptySlot()

if (direction == "up"):
self.swapElements(slot[1], slot[0], slot[1]+1, slot[0])

elif (direction == "down"):
self.swapElements(slot[1], slot[0], slot[1]-1, slot[0])

elif direction == "left":
self.swapElements(slot[1], slot[0], slot[1], slot[0]-1)
elif (direction == "right"):
self.swapElements(slot[1], slot[0], slot[1], slot[0]+1)

def swapElements(self, fromx, fromy, tox, toy):
dummy = self.elements[toy][tox]

self.elements[toy][tox] = self.elements[fromy][fromx]
self.elements[fromy][fromx] = dummy

def getPossibleMoves(self):
emptySlot = self.getEmptySlot()
y = emptySlot[1]
x = emptySlot[0]
north = (y == 0)
south = (y == (self.dim-1))
west = (x == 0)
east = (x == (self.dim-1))

middle = not(north or south or west or east)

northwest = north and west
northeast = north and east
southwest = south and west
southeast = south and east
# orientation has to be distinct
# save original values
orignorth = north
origsouth = south
# original north or south
orignors = north or south

north = north and not (west or east)
south = south and not (west or east)
west = west and not (orignors)
east = east and not (orignors)

if middle:
return ["up", "down", "left", "right"]
elif north:
return ["up", "left", "right"]
elif south:
return ["down", "left", "right"]
elif west:
return ["up", "down", "left"]
elif east:
return ["up", "down", "right"]
elif northwest:
return ["up", "left"]
elif northeast:
return ["up", "right"]
elif southwest:
return ["down", "left"]
elif southeast:
return ["down", "right"]

# ~Puzzle.py
 
D

Diez B. Roggisch

Some general remarks:
def getEmptySlot(self):
i = 0
j = 0
while i <= self.dim-1:
while j <= self.dim-1:
if self.elements[j] == -1:
return [j, i]
j = j+1
j = 0
i = i + 1


make this:

def getEmptySlot(self):
for i in xrange(self.dim):
for j in xrange(self.dim):
if self.elements[j] == -1:
return (i,j)
def getPossibleMoves(self):
emptySlot = self.getEmptySlot()
y = emptySlot[1]
x = emptySlot[0]

make this:

x,y = self.getEmptySlot()
 
T

Terry Reedy

'Having trouble' is too vague to figure out. However, I would delete this:
def getEmptySlot(self):
i = 0
j = 0
while i <= self.dim-1:
while j <= self.dim-1:
if self.elements[j] == -1:
return [j, i]
j = j+1
j = 0
i = i + 1


and maintain an .empty attribute, which is trivially updated in
def swapElements(self, fromx, fromy, tox, toy):
dummy = self.elements[toy][tox]

self.elements[toy][tox] = self.elements[fromy][fromx]
self.elements[fromy][fromx] = dummy

as (fromy,fromx). Note that there is no need to pass tox, toy to this
routine. Indeed, I would include the last two lines in your move routine.
Since dummy is always the same object, I would also just keep it as
self._dummy instead of looking it up each time.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top