[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>&nbsp;</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>&nbsp;<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>&nbsp;</DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN 
class=440511716-30042001><BR>class Grid:<BR>&nbsp;&nbsp; size = 
4<BR>&nbsp;&nbsp; grid = 
[(0,1),(1,2),(2,5),(3,9),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
(4,3),(5,7),(6,6),(7,10),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
(8,0),(9,4),(10,8),(11,11),<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
(12,15),(13,14),(14,13),(15,12)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
]</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN 
class=440511716-30042001>&nbsp;&nbsp; def 
findBlock(self,val):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if val in 
range(self.size**2):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for item in 
self.grid:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if item[1] 
== val:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
return item<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "invalid 
item"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 
self.findBlock(0)<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp; def 
printIt(self):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for i in 
self.grid:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (i[0]+1)%self.size 
!= 0:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print 
i[1],<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else: print 
i[1]</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN 
class=440511716-30042001>&nbsp;&nbsp; def 
isSorted(self):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; limit = 
(self.size**2)-2<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for i in 
range(limit):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
if (self.grid[i+1][1] - self.grid[i][1]) != 
1:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
return 0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN 
class=440511716-30042001>&nbsp;&nbsp; def 
swap(self,pos):<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gap = 
self.findBlock(0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.grid[gap[0]] = 
(gap[0],pos[1])<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.grid[pos[0]] = 
(pos[0],0) 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>g = 
Grid()<BR>g.printIt()<BR>while not g.isSorted():<BR>&nbsp;&nbsp;&nbsp; block = 
input("Item to move?")<BR>&nbsp;&nbsp;&nbsp; item = 
g.findBlock(block)<BR>&nbsp;&nbsp;&nbsp; g.swap(item)<BR>&nbsp;&nbsp;&nbsp; 
g.printIt()<BR>&nbsp;&nbsp;&nbsp; if 
g.isSorted():<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print "Game 
over"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
break<BR></SPAN></FONT></DIV></BODY></HTML>

------_=_NextPart_001_01C0D190.7BB06EF0--