ixxra

Simple 15puzzle repr

Nov 21st, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. #usr/bin/env python
  2.  
  3. from sys import argv
  4.  
  5.  
  6. class DimensionMismatchException(BaseException):
  7.     pass
  8.  
  9. class Blank(object):
  10.     def __repr__(self):
  11.         return 'B'
  12.        
  13.  
  14. class Puzzle:
  15.     rows = 0
  16.     cols = 0
  17.     array = None
  18.     blank = None
  19.  
  20.     def __init__(self, rows, cols, *data):
  21.         if len(data) != rows*cols - 1:
  22.             raise DimensionMismatchException('not enough data')
  23.  
  24.         self.rows = rows
  25.         self.cols = cols
  26.         self.array = list(data)
  27.         self.blank = Blank()
  28.         self.array.append(self.blank)
  29.  
  30.     def __repr__(self):
  31.         puzzle = ''
  32.        
  33.         for i in range(self.rows):
  34.             if i > 0:
  35.                 puzzle = puzzle + '\n'
  36.        
  37.             puzzle = puzzle + str(self.array[i*self.rows:i*self.rows + self.cols])
  38.        
  39.         return puzzle
  40.  
  41.     def up(self):
  42.         pos = self.array.index(self.blank)
  43.         if pos < self.cols:
  44.             return
  45.         else:
  46.             upper = pos - self.cols
  47.             d = self.array[upper]
  48.             self.array[pos] = d
  49.             self.array[upper] = self.blank
  50.            
  51.     def down(self):
  52.         pos = self.array.index(self.blank)
  53.         if pos >= (self.rows - 1) * self.cols:
  54.             return
  55.         else:
  56.             downer = pos + self.cols
  57.             d = self.array[downer]
  58.             self.array[pos] = d
  59.             self.array[downer] = self.blank
  60.            
  61.     def right(self):
  62.         pos = self.array.index(self.blank)
  63.         if (pos + 1) % self.cols == 0:
  64.             return
  65.         else:
  66.             righty = pos + 1
  67.             d = self.array[righty]
  68.             self.array[pos] = d
  69.             self.array[righty] = self.blank
  70.    
  71.     def left(self):
  72.         pos = self.array.index(self.blank)
  73.         if pos % self.cols == 0:
  74.             return
  75.         else:
  76.             lefty = pos - 1
  77.             d = self.array[lefty]
  78.             self.array[pos] = d
  79.             self.array[lefty] = self.blank
  80.        
  81. if __name__ == '__main__':
  82.     rows = int(argv[1])
  83.     cols = int(argv[2])
  84.  
  85.     data = [int(i) for i in  argv[3:]]
  86.  
  87.     puzzle = Puzzle(rows, cols, *data)
  88.     print('tablero en posicion 0:')
  89.     print(puzzle)
  90.     puzzle.up()
  91.     print('un movimiento hacia arriba:')
  92.     print(puzzle)
  93.     puzzle.left()
  94.     print('y ahora hacia la izquierda')
  95.     print(puzzle)
Advertisement
Add Comment
Please, Sign In to add comment