Guest User

Untitled

a guest
Jan 14th, 2012
45
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import itertools
  3.  
  4. class Game:
  5.     def __init__(self):
  6.         self.possible_positions = [ [1,2,5,4,6,3], [5,3,6,1,4,2], [4,6,3,5,2,1], [2,1,4,3,5,6], [3,5,2,6,1,4], [6,4,1,2,3,5] ]
  7.         self.board = [ [ '*' for i in range(6) ] for j in range(6) ]
  8.         self.color_permutations = list(itertools.permutations(['p', 'r', 'c', 'o', 'y', 'g']))
  9.         #self.possible_positions = [ [1,2,3],[2,3,1],[3,1,2] ]
  10.         #self.board = [ [ '*' for i in range(3) ] for j in range(3) ]
  11.         #self.color_permutations = list(itertools.permutations(['p','r','c']))
  12.  
  13.     def __str__(self):
  14.         output = ""
  15.         for i in self.board:
  16.             output = output + " ".join(i) + "\n"
  17.         return output.strip()
  18.  
  19.     def solve(self, n=1):
  20.         if n == len(self.possible_positions[0]) + 1:
  21.             print self
  22.             return True
  23.         for colorcombo in self.color_permutations:
  24.             colorcombo = list(colorcombo)
  25.             if self.check_and_fill_table(colorcombo, n) == True:
  26.                 return self.solve(n+1)
  27.             else:
  28.                 self.make_stars(n)
  29.                 pass
  30.         print "lame."
  31.  
  32.     def make_stars(self, n):
  33.         for linenum, line in enumerate(self.possible_positions):
  34.             self.board[linenum][line.index(n)] = '*'
  35.  
  36.     def check_and_fill_table(self, colorcombo, n):
  37.         for linenum, line in enumerate(self.possible_positions):
  38.             color = colorcombo.pop(0)
  39.             if self.color_in_row(color, linenum):
  40.                 return False
  41.             if self.color_in_column(color, line.index(n)):
  42.                 return False
  43.             self.board[linenum][line.index(n)] = color
  44.         return True
  45.  
  46.     def color_in_row(self, color, linenum):
  47.         if color in self.board[linenum]:
  48.             return True
  49.  
  50.     def color_in_column(self, color, column):
  51.         for row in self.board:
  52.             if row[column] == color:
  53.                 return True
  54.  
  55.  
  56. a = Game()
  57. a.solve()
RAW Paste Data