[Tutor] 15 puzzle
alan.gauld@bt.com
alan.gauld@bt.com
Mon, 30 Apr 2001 17:13:24 +0100
------_=_NextPart_001_01C0D190.7BB06EF0
Content-type: text/plain; charset="iso-8859-1"
This may well have been answered already - I'm catching up from a week's
vacation...
It appears not yet...
Here is a skeleton for a 3x3 game and lacks any input validation
- ie it's easily broken but it should form a basis for something...
Here it is in full 4x4 size with minimal error detection...
class Grid:
size = 4
grid = [(0,1),(1,2),(2,5),(3,9),
(4,3),(5,7),(6,6),(7,10),
(8,0),(9,4),(10,8),(11,11),
(12,15),(13,14),(14,13),(15,12)
]
def findBlock(self,val):
if val in range(self.size**2):
for item in self.grid:
if item[1] == val:
return item
else:
print "invalid item"
return self.findBlock(0)
def printIt(self):
for i in self.grid:
if (i[0]+1)%self.size != 0:
print i[1],
else: print i[1]
def isSorted(self):
limit = (self.size**2)-2
for i in range(limit):
if (self.grid[i+1][1] - self.grid[i][1]) != 1:
return 0
return 1
def swap(self,pos):
gap = self.findBlock(0)
self.grid[gap[0]] = (gap[0],pos[1])
self.grid[pos[0]] = (pos[0],0)
g = Grid()
g.printIt()
while not g.isSorted():
block = input("Item to move?")
item = g.findBlock(block)
g.swap(item)
g.printIt()
if g.isSorted():
print "Game over"
break
------_=_NextPart_001_01C0D190.7BB06EF0
Content-type: text/html; charset="iso-8859-1"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META content="MSHTML 5.00.3013.2600" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<BLOCKQUOTE
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=060143314-30042001>This
may well have been answered already - I'm catching up from a week's
vacation...</SPAN></FONT></DIV></BLOCKQUOTE>
<DIV><SPAN class=060143314-30042001></SPAN><SPAN class=440511716-30042001><FONT
color=#0000ff face=Arial size=2>It appears not yet...</FONT></SPAN> </DIV>
<BLOCKQUOTE
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=060143314-30042001>Here
is a skeleton for a 3x3 game and lacks any input validation
</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN class=060143314-30042001>- ie
it's easily broken but it should form a basis for
something...</SPAN></FONT></DIV></BLOCKQUOTE>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=060143314-30042001></SPAN></FONT> <FONT color=#0000ff face=Arial
size=2><SPAN class=440511716-30042001>Here it is in full 4x4 size with minimal
error detection...</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=440511716-30042001></SPAN></FONT> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=440511716-30042001><BR>class Grid:<BR> size =
4<BR> grid =
[(0,1),(1,2),(2,5),(3,9),<BR>
(4,3),(5,7),(6,6),(7,10),<BR>
(8,0),(9,4),(10,8),(11,11),<BR>
(12,15),(13,14),(14,13),(15,12)<BR>
]</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=440511716-30042001> def
findBlock(self,val):<BR> if val in
range(self.size**2):<BR> for item in
self.grid:<BR> if item[1]
== val:<BR>
return item<BR>
else:<BR> print "invalid
item"<BR> return
self.findBlock(0)<BR> <BR> def
printIt(self):<BR> for i in
self.grid:<BR> if (i[0]+1)%self.size
!= 0:<BR> print
i[1],<BR> else: print
i[1]</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=440511716-30042001> def
isSorted(self):<BR> limit =
(self.size**2)-2<BR> for i in
range(limit):<BR>
if (self.grid[i+1][1] - self.grid[i][1]) !=
1:<BR>
return 0<BR> return 1</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN
class=440511716-30042001> def
swap(self,pos):<BR> gap =
self.findBlock(0)<BR> self.grid[gap[0]] =
(gap[0],pos[1])<BR> self.grid[pos[0]] =
(pos[0],0)
<BR> <BR>g =
Grid()<BR>g.printIt()<BR>while not g.isSorted():<BR> block =
input("Item to move?")<BR> item =
g.findBlock(block)<BR> g.swap(item)<BR>
g.printIt()<BR> if
g.isSorted():<BR> print "Game
over"<BR>
break<BR></SPAN></FONT></DIV></BODY></HTML>
------_=_NextPart_001_01C0D190.7BB06EF0--