-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumbreroMain.py
148 lines (112 loc) · 5.55 KB
/
sumbreroMain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 22 22:25:33 2012
@author: Juice
"""
from PySide import QtGui, QtCore
from constraints import *
from structure import *
from time import *
from math import *
from constraints import *
from gui.puzzlemodel.puzzletreemodel import *
from gui.ui import mainWindowUi
from structure.puzzle import Puzzle
from solver.Solver import Solver
from structure.cell import PositionedCell
from gui.windows.basicmainwindow import BasicMainWindow
from gui.puzzlerepresentation.puzzlepiece import PuzzlePieceProxy
from gui.puzzlerepresentation.valuehexagon import ValueHexagon
from utility.puzzlefactory import PuzzleFactory
class MainWindow(BasicMainWindow):
def initializePuzzleRepresentation(self):
self.scene = QtGui.QGraphicsScene(self)
# self.view.setScene(self.scene)
self.puzzlePieces = {}
self.hexSide = 100
#self.blockPadding = 3
self.selectedItems = set()
for c in self.puzzle.getGrid().getCells():
if( c.getPosition ):
# see for an explanation of the rotated version (i.e. horizontal iso vertical):
# http://www.gamedev.net/page/resources/_/technical/game-programming/coordinates-in-hexagon-based-tile-maps-r1800
tileR = self.hexSide/2 * cos(pi/6)
hexH = self.hexSide/2 * sin(pi/6)
#tileW = 2 * tileR
#tileH = self.hexSide/2 + 2 * hexH
pos = c.getPosition()
if( pos.x() % 2 ):
hexPos = QtCore.QPointF(
pos.x() * (hexH + self.hexSide/2) ,
pos.y() * 2 * tileR + tileR
)
else:
hexPos = QtCore.QPointF(
pos.x() * (hexH + self.hexSide/2) ,
pos.y() * 2 * tileR
)
rect = ValueHexagon(c, self.hexSide, hexPos)
self.scene.addItem(rect)
c.valueChanged.connect(self.cellValueChanged)
c.possibleValuesChanged.connect(self.cellPossibleValuesChanged)
self.puzzlePieces[c] = rect
proxy = PuzzlePieceProxy(rect)
self.puzzlePieceProxies[c] = proxy
for cg in self.puzzle.getConstraintGroups():
for constraint in cg.getConstraints():
if isinstance(constraint, TotalSumValueConstraint):
# TODO: refactor to utililty class, make much more general
# find the position and direction that is most likely:
if(cg.name.startswith('Column')):
cell = cg.getCells()[0]
pos = QtCore.QPoint(cell.position.x(), -1)
else:
pos = QtCore.QPoint(-1, cell.position.y())
if( pos.x() % 2 ):
hexPos = QtCore.QPointF(
pos.x() * (hexH + self.hexSide/2) ,
pos.y() * 2 * tileR + tileR
)
else:
hexPos = QtCore.QPointF(
pos.x() * (hexH + self.hexSide/2) ,
pos.y() * 2 * tileR
)
hex = ValueHexagon(constraint, self.hexSide, hexPos, edgeColor=QtCore.Qt.transparent)
self.puzzlePieces[constraint] = hex
self.scene.addItem(hex)
@staticmethod
def parsePuzzle(filename):
# Set the initial data to work with
myFile = open("./simpleSumbrero.txt")
values = range(1,10)
stringArray = myFile.readlines()
y = 0
for line in stringArray:
line = line.strip() # strip off whitespace
if(line.startswith("range")):
min, max = [int(x) for x in line[line.find("=")+1:].split(",")]
puzzle = Puzzle("Test-sumbrero", range(min, max+1))
if(line.startswith("cell")):
x,y = [float(x) for x in line[line.find("(")+1:line.find(')')].split(",")]
puzzle.getGrid().addCell(QtCore.QPoint(x,y))
if(line.startswith("column")):
column, value = line.split('=')
column = int(column.split('(')[1].split(')')[0])
# create a sum constraint
cg = puzzle.addConstraintGroup("Column sum " + str(column))
tsvc = cg.addConstraint(TotalSumValueConstraint)
uvc = cg.addConstraint(UniqueValueConstraint)
for cell in puzzle.grid.cells:
if(cell.position.x() == column):
cg.addCell(cell)
tsvc.setTotalValue(int(value))
# TODO: deal with the two different kinds of row sums
solver = Solver(puzzle)
return puzzle, solver
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.loadPuzzle("simpleSumbrero.txt")
mainWin.show()
sys.exit(app.exec_())