Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import itertools
- class Game:
- def __init__(self):
- 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] ]
- self.board = [ [ '*' for i in range(6) ] for j in range(6) ]
- self.color_permutations = list(itertools.permutations(['p', 'r', 'c', 'o', 'y', 'g']))
- #self.possible_positions = [ [1,2,3],[2,3,1],[3,1,2] ]
- #self.board = [ [ '*' for i in range(3) ] for j in range(3) ]
- #self.color_permutations = list(itertools.permutations(['p','r','c']))
- def __str__(self):
- output = ""
- for i in self.board:
- output = output + " ".join(i) + "\n"
- return output.strip()
- def solve(self, n=1):
- if n == len(self.possible_positions[0]) + 1:
- print self
- return True
- for colorcombo in self.color_permutations:
- colorcombo = list(colorcombo)
- if self.check_and_fill_table(colorcombo, n) == True:
- return self.solve(n+1)
- else:
- self.make_stars(n)
- pass
- print "lame."
- def make_stars(self, n):
- for linenum, line in enumerate(self.possible_positions):
- self.board[linenum][line.index(n)] = '*'
- def check_and_fill_table(self, colorcombo, n):
- for linenum, line in enumerate(self.possible_positions):
- color = colorcombo.pop(0)
- if self.color_in_row(color, linenum):
- return False
- if self.color_in_column(color, line.index(n)):
- return False
- self.board[linenum][line.index(n)] = color
- return True
- def color_in_row(self, color, linenum):
- if color in self.board[linenum]:
- return True
- def color_in_column(self, color, column):
- for row in self.board:
- if row[column] == color:
- return True
- a = Game()
- a.solve()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement