SHOW:
|
|
- or go back to the newest paste.
1 | #!/usr/bin/python | |
2 | import itertools | |
3 | ||
4 | - | class Game: |
4 | + | class Cube36: |
5 | - | def __init__(self): |
5 | + | def __init__(self, board): |
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] ] |
6 | + | self.possible_positions = board |
7 | - | self.board = [ [ '*' for i in range(6) ] for j in range(6) ] |
7 | + | size = len(self.possible_positions[0]) |
8 | - | self.color_permutations = list(itertools.permutations(['p', 'r', 'c', 'o', 'y', 'g'])) |
8 | + | print "The board: " |
9 | - | #self.possible_positions = [ [1,2,3],[2,3,1],[3,1,2] ] |
9 | + | print print_matrix(self.possible_positions) |
10 | - | #self.board = [ [ '*' for i in range(3) ] for j in range(3) ] |
10 | + | self.board = [ [ '*' for i in range(size) ] for j in range(size) ] |
11 | - | #self.color_permutations = list(itertools.permutations(['p','r','c'])) |
11 | + | colors = ['p','r','c','o','y','g','m','t','b'][:size] |
12 | self.color_permutations = list(itertools.permutations(colors)) | |
13 | ||
14 | - | output = "" |
14 | + | |
15 | - | for i in self.board: |
15 | + | return print_matrix(self.board) |
16 | - | output = output + " ".join(i) + "\n" |
16 | + | |
17 | - | return output.strip() |
17 | + | |
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 | - | return self.solve(n+1) |
26 | + | |
27 | self.make_stars(n) | |
28 | ||
29 | - | pass |
29 | + | |
30 | - | print "lame." |
30 | + | |
31 | for colnum, value in enumerate(line): | |
32 | if value == n: | |
33 | self.board[linenum][colnum] = '*' | |
34 | - | self.board[linenum][line.index(n)] = '*' |
34 | + | |
35 | ||
36 | def check_and_fill_table(self, colorcombo, n): | |
37 | for rownum, row in enumerate(self.possible_positions): | |
38 | - | color = colorcombo.pop(0) |
38 | + | for colnum, value in enumerate(row): |
39 | - | if self.color_in_row(color, linenum): |
39 | + | if value == n: |
40 | - | return False |
40 | + | color = colorcombo.pop(0) |
41 | - | if self.color_in_column(color, line.index(n)): |
41 | + | if self.color_in_row(color, rownum): |
42 | - | return False |
42 | + | return False |
43 | - | self.board[linenum][line.index(n)] = color |
43 | + | if self.color_in_column(color, colnum): |
44 | return False | |
45 | self.board[rownum][colnum] = color | |
46 | return True | |
47 | - | if color in self.board[linenum]: |
47 | + | |
48 | def color_in_row(self, color, linenum): | |
49 | return color in self.board[linenum] | |
50 | - | def color_in_column(self, color, column): |
50 | + | |
51 | def color_in_column(self, color, colnum): | |
52 | - | if row[column] == color: |
52 | + | |
53 | if row[colnum] == color: | |
54 | return True | |
55 | ||
56 | - | a = Game() |
56 | + | def print_matrix(matrix): |
57 | - | a.solve() |
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() |