[Tutor] making a number slide game

Rich Krauter rmkrauter at yahoo.com
Sat Aug 14 04:37:59 CEST 2004


Dragonfirebane at aol.com wrote:
> Hello all,
> 
> I'm trying to make a non-GUI (for now) Python representation of the handheld 
> tile-sliding game where there are numbered tiles from 1 to 15 in a 4 x 4 
> square with one empty square, the point of the game being to arrange them in order 
> from 1 to 15. I remember a while ago (June 2004) there was a post regarding 
> using sentinels to define the border of a minesweeper field 
> (http://mail.python.org/pipermail/tutor/2004-June/029990.html), but i didn't quite understand it 
> at the time and the archives only show it as a text file. I'd be grateful if 
> anyone'd be willing to help me out by explaining sentinels to define the edge of 
> a field (so that the program can check where in the field the empty space is, 
> and if the collected user move is next to the space w/o getting an 
> IndexError). The following is my code, which works up to the first move, as i have not 
> yet programmed the rest and don't know where to start.
> 

Hi Dfb,

The link you posted contains good advice; that said, I tried the 
following (more or less ignoring the good advice):


<code>
import random

blank_char = ' '
size = 16
numbers = range(1,size)
ranum = random.sample(numbers, size-1)
ranum.append(blank_char)

# no diagonal moves allowed
neighbors = [[1,4],    [0,2,5],     [1,3,6],     [2,7],
              [0,5,8],  [1,4,6,9],   [2,5,7,10],  [3,6,11],
              [4,9,12], [5,8,10,13], [6,9,11,14], [7,10,15],
              [8,13],   [9,12,14],   [10,13,15],  [11,14]]


field = """
-------------------------------------
|        |        |        |        |
|   %s   |   %s   |   %s   |   %s   |
|        |        |        |        |
-------------------------------------
|        |        |        |        |
|   %s   |   %s   |   %s   |   %s   |
|        |        |        |        |
-------------------------------------
|        |        |        |        |
|   %s   |   %s   |   %s   |   %s   |
|        |        |        |        |
-------------------------------------
|        |        |        |        |
|   %s   |   %s   |   %s   |   %s   |
|        |        |        |        |
-------------------------------------
"""


play = True

while play:
     print field%(tuple(['%2s'%n for n in ranum]))
     move = int(raw_input("Enter number of tile you wish to move: "))

     	
     if move >= size:
         print 'Invalid choice'
         continue
     elif move <= 0:
         break

     move_index = ranum.index(move)
     blank_index = ranum.index(blank_char)

     if blank_index in neighbors[move_index]:
	# valid move, so swap the values
         ranum[move_index] = blank_char
         ranum[blank_index] = move
     else:
         print 'Invalid move'


</code>

I tried to separate the board creation from the game-playing; but did 
very little error checking. e.g. if the user passes in a letter the 
program will choke, etc., etc.

I used a 1-d list of known size (your 'ranum' list) to create the board, 
so it was easy to check for boundary cases - i.e.  I didn't need 
'sentinels'. Also, I cheated by hard-wiring the allowable moves in the 
'neighbors' list, which is ok for this size board, but anything larger 
would be a pain to hardwire. The 'neighbors' list could probably be 
generated; in the meantime, I figured I'd send this along.

Good luck.

Rich






More information about the Tutor mailing list