# Untitled

a guest
Jan 15th, 2012
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #!/usr/bin/python
2. import itertools
3.
4. class Cube36:
5.     def __init__(self, board):
6.         self.possible_positions = board
7.         size = len(self.possible_positions[0])
8.         print "The board: "
9.         print print_matrix(self.possible_positions)
10.         self.board = [ [ '*' for i in range(size) ] for j in range(size) ]
11.         colors = ['p','r','c','o','y','g','m','t','b'][:size]
12.         self.color_permutations = list(itertools.permutations(colors))
13.
14.     def __str__(self):
15.         return print_matrix(self.board)
16.
17.     def solve(self, n=1):
18.         if n == len(self.possible_positions[0]) + 1:
19.             print "Solution: "
20.             print self
21.             return True
22.         for colorcombo in self.color_permutations:
23.             colorcombo = list(colorcombo)
24.             if self.check_and_fill_table(colorcombo, n) == True:
25.                 self.solve(n+1)
26.             else:
27.                 self.make_stars(n)
28.
29.     def make_stars(self, n):
30.         for linenum, line in enumerate(self.possible_positions):
31.             for colnum, value in enumerate(line):
32.                 if value == n:
33.                     self.board[linenum][colnum] = '*'
34.
35.
36.     def check_and_fill_table(self, colorcombo, n):
37.         for rownum, row in enumerate(self.possible_positions):
38.             for colnum, value in enumerate(row):
39.                 if value == n:
40.                     color = colorcombo.pop(0)
41.                     if self.color_in_row(color, rownum):
42.                         return False
43.                     if self.color_in_column(color, colnum):
44.                         return False
45.                     self.board[rownum][colnum] = color
46.         return True
47.
48.     def color_in_row(self, color, linenum):
49.         return color in self.board[linenum]
50.
51.     def color_in_column(self, color, colnum):
52.         for row in self.board:
53.             if row[colnum] == color:
54.                 return True
55.
56. def print_matrix(matrix):
57.     output = ""
58.     for row in matrix:
59.         output += "|"
60.         output += " ".join([str(col).center(2) for col in row])
61.         output += "|\n"
62.     return output.strip()
63.
64.
65. # The board, as viewed by casual observer. (36 officers)
66. #gameboard = [ [1,2,5,4,6,3],
67. #              [5,3,6,1,4,2],
68. #              [4,6,3,5,2,1],
69. #              [2,1,4,3,5,6],
70. #              [3,5,2,6,1,4],
71. #              [6,4,1,2,3,5] ]
72.
73. # The board in reality.
74. gameboard = [ [5,3,2,1,4,6],
75.               [4,1,5,2,5,3],
76.               [6,5,3,4,1,2],
77.               [1,2,6,3,6,4],
78.               [2,4,1,6,3,5],
79.               [3,6,4,5,2,1] ]
80.
81. # A 3x3 arrangement of officers. (test)
82. #gameboard = [ [1, 2, 3],
83. #              [2, 3, 1],
84. #              [3, 1, 2] ]
85.
86. game = Cube36(gameboard)
87. game.solve()