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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
|
#!/usr/bin/python
"""
__version__ = "$Revision: 1.3 $"
__date__ = "$Date: 2006/01/14 14:27:33 $"
"""
import os, sys
import wx
from wx.html import HtmlEasyPrinting
from PythonCard import configuration, dialog, model, helpful
import copy
COLOR = { 'a': (255,200,200), 'f': (255,255,255), 'p': (255,255,255), 'u': (255,255,255), 'z': (255,0,0),
'i': (200,255,200), 'o': (200,200,255), }
VERSION = "0.2"
def textToHtml(txt):
# the wxHTML classes don't require valid HTML
# so this is enough
html = txt.replace('\n\n', '<P>')
html = html.replace('\n', '<BR>')
return html
class square:
def __init__(self, x,y, comp):
self.x = x
self.y = y
self.comp = comp
self.state = None
self.values = [1,2,3,4,5,6,7,8,9]
self.color = (255,255,255)
class undosquare:
def __init__(self, val, state):
self.state = state
self.values = val
class MyBackground(model.Background):
def on_initialize(self, event):
# if you have any initialization
# including sizer setup, do it here
self.readme = None
self.hideUndecided = False
self.hideChoices = False
self.squares = {}
for i in range(9):
for j in range(9):
self.squares[i,j] = square(i,j, self.components["B%d%d" % (i,j)])
self.peer = {}
for i in range(3):
for j in range(3):
self.peer[3*i+j] = []
for x in range(3):
for y in range(3):
self.peer[3*i+j].append( (3*i+x, 3*j+y) )
self.sizerLayout()
self.startTitle = self.title
self.newFile()
self.components.solveIt.SetFocus()
def sizerLayout(self):
# Need this nonsense because we are using list components in an unusual way
# We want them to be entirely visible - and Mac OSX doesn't record an adequate
# minimum size (GetBestSize()) for list controls
if wx.Platform == '__WXMSW__':
minListSize = (45,44)
elif wx.Platform <> '__WXMAC__':
minListSize = (66,66)
else:
minListSize = (66,66)
for i in range(9):
for j in range(9):
self.components["B%d%d" % (i,j)].SetMinSize( minListSize )
sizer1 = wx.BoxSizer(wx.VERTICAL)
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
## There is no sizer 3 !!
sizer4 = wx.GridSizer(9, 9, 2, 2)
sizer5 = wx.BoxSizer(wx.HORIZONTAL)
stcSizerAttrs = wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL
fldSizerAttrs = wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL
vertFlags = wx.LEFT | wx.TOP | wx.ALIGN_LEFT
chkSizerAttrs = wx.RIGHT | wx.ALIGN_CENTER_VERTICAL
C = self.components
sizer2.Add(C.solveIt, flag=fldSizerAttrs)
sizer2.Add((5, 5), 0)
sizer2.Add(C.Singles, flag=fldSizerAttrs)
sizer2.Add((5, 5), 0)
sizer2.Add(C.Soles, flag=fldSizerAttrs)
sizer2.Add((45, 5), 0)
sizer2.Add(C.Blank, flag=fldSizerAttrs)
sizer2.Add((25, 5), 0)
sizer2.Add(C.Undo, flag=fldSizerAttrs)
sizer2.Add((5, 5), 0)
sizer2.Add(C.Redo, flag=fldSizerAttrs)
for i in range(9):
for j in range(9):
sizer4.Add(C["B"+str(i)+str(j)])
sizer5.Add(C.hideUndecided, flag=fldSizerAttrs)
sizer5.Add((5, 5), 0)
sizer5.Add(C.hideChoices, flag=fldSizerAttrs)
sizer1.Add(sizer2, 0, vertFlags)
sizer1.Add((5, 15), 0) # spacer
sizer1.Add(sizer4, 0, vertFlags)
sizer1.Add((5, 15), 0) # spacer
sizer1.Add(sizer5, 0, vertFlags)
sizer1.Fit(self)
sizer1.SetSizeHints(self)
self.panel.SetSizer(sizer1)
self.panel.SetAutoLayout(1)
self.panel.Layout()
self.visible = True
def reset(self):
for i in range(9):
for j in range(9):
self.squares[i,j].values = [1,2,3,4,5,6,7,8,9]
self.squares[i,j].color = (255,255,255)
self.squares[i,j].state = 'u'
self.undo = []
self.redo = []
def loadConfig(self):
pass
def saveConfig(self):
pass
def saveChanges(self):
# save configuration info in the app directory
#filename = os.path.basename(self.documentPath)
if self.documentPath is None:
filename = "Untitled"
else:
filename = self.documentPath
msg = "The text in the %s file has changed.\n\nDo you want to save the changes?" % filename
result = dialog.messageDialog(self, msg, 'textEditor', wx.ICON_EXCLAMATION | wx.YES_NO | wx.CANCEL)
return result.returnedString
def doExit(self):
return 1
def on_close(self, event):
if self.doExit():
# self.saveConfig()
self.fileHistory = None
self.printer = None
event.skip()
def on_menuFileSave_select(self, event):
if self.documentPath is None:
# this a "new" document and needs to go through Save As...
self.on_menuFileSaveAs_select(None)
else:
self.saveFile(self.documentPath)
def on_menuFileSaveAs_select(self, event):
wildcard = "Text files (*.txt)|*.TXT;*.txt|All files (*.*)|*.*"
if self.documentPath is None:
dir = ''
filename = '*.txt'
else:
dir = os.path.dirname(self.documentPath)
filename = os.path.basename(self.documentPath)
result = dialog.saveFileDialog(None, "Save As", dir, filename, wildcard)
if result.accepted:
path = result.paths[0]
self.saveFile(path)
return True
else:
return False
def newFile(self):
self.documentPath = None
self.documentChanged = 0
self.title = 'Untitled - ' + self.startTitle
self.statusBar.text = 'Untitled'
self.reset()
for i in range(9):
for j in range(9):
self.updateList(self.squares[i,j])
def setUpPuzzle(self, definition):
self.reset()
y = 0
for line in definition:
if line.strip() == "": continue
if y == 9:
# must be the optional title
self.title = line
self.statusBar.text = line
break
x = 0
for c in line.strip():
sqr = self.squares[x,y]
if c == " ": continue
if c == "x":
sqr.state = 'u'
else:
sqr.state = 'p'
sqr.values = [int(c),]
self.updateList(sqr)
x += 1
y += 1
def on_builtInPuzzle_command(self, event):
self.documentPath = None
self.documentChanged = 0
self.title = 'Untitled - ' + self.startTitle
self.statusBar.text = 'Untitled'
tname = event.target.name.replace("menuFilePuzzle", "P")
self.setUpPuzzle(self.components[tname].userdata.split("\n"))
def openFile(self, path):
# change the code below for
# opening an existing document
# the commented lines are from the textEditor tool
# may be overridden if file includesthe optinoal title on last line
self.title = os.path.split(path)[-1] + ' - ' + self.startTitle
f = open(path)
self.setUpPuzzle(f)
f.close()
self.documentPath = path
self.documentChanged = 0
self.statusBar.text = path
def saveFile(self, path):
# change the code below for
# saving an existing document
f = open(path, 'w')
for y in range(9):
s = []
for x in range(9):
tList = self.squares[x,y].values
if len(tList) == 1:
s.append(str(tList[0]))
else:
s.append('x')
if x == 2 or x == 5: s.append(' ')
s.append("\n")
f.write(''.join(s))
if y == 2 or y == 5: f.write("\n")
f.close()
self.documentPath = path
self.documentChanged = False
self.title = os.path.split(path)[-1] + ' - ' + self.startTitle
self.statusBar.text = path
def on_menuFileNew_select(self, event):
if self.documentChanged:
save = self.saveChanges()
if save == "Cancel":
# don't do anything, just go back to editing
pass
elif save == "No":
# any changes will be lost
self.newFile()
else:
if self.documentPath is None:
if self.on_menuFileSaveAs_select(None):
self.newFile()
else:
self.saveFile(self.documentPath)
self.newFile()
else:
# don't need to save
self.newFile()
def on_menuFileOpen_select(self, event):
# split this method into several pieces to make it more flexible
wildcard = "Text files (*.txt)|*.txt;*.TXT|All files (*.*)|*.*"
result = dialog.openFileDialog(wildcard=wildcard)
if result.accepted:
path = result.paths[0]
# an error will probably occur here if the text is too large
# to fit in the wxTextCtrl (TextArea) or the file is actually
# binary. Not sure what happens with CR/LF versus CR versus LF
# line endings either
self.openFile(path)
def on_menuFilePrint_select(self, event):
# put your code here for print
# the commented code below is from the textEditor tool
# and is simply an example
#source = textToHtml(self.components.fldDocument.text)
#self.printer.PrintText(source)
pass
def on_menuFilePrintPreview_select(self, event):
# put your code here for print preview
# the commented code below is from the textEditor tool
# and is simply an example
#source = textToHtml(self.components.fldDocument.text)
#self.printer.PreviewText(source)
pass
def on_menuFilePageSetup_select(self, event):
self.printer.PageSetup()
def saveUndoPosition(self):
st = {}
for i in range(9):
for j in range(9):
sqr = self.squares[i,j]
st[i,j] = undosquare(copy.copy(sqr.values), sqr.state)
self.undo.append(st)
self.redo = []
def on_Blank_mouseClick(self, event):
self.on_menuFileNew_select(event)
def on_Undo_command(self, event):
# if we are at the end - i.e. no redo info yet, then we should take a snapshot
# right now so that we can get back to this point
if len(self.redo) == 0:
self.saveUndoPosition()
st = self.undo[-1]
self.redo.append(st)
del self.undo[-1]
## print "undo", len(self.undo), len(self.redo)
if len(self.undo) == 0:
result = dialog.alertDialog(self, 'No more to Undo', 'Undo Alert')
return
st = self.undo[-1]
## for k,v in self.undo[-1].iteritems():
## print k, v.state, v.values
for i in range(9):
for j in range(9):
sqr = self.squares[i,j]
sqr.values = copy.copy(st[i,j].values)
sqr.state = st[i,j].state
self.updateList(sqr)
self.redo.append(st)
del self.undo[-1]
t = self.count()
self.statusBar.text = "Possible choices left %d" % t
## print "undo", len(self.undo), len(self.redo)
def on_Redo_command(self, event):
## print "redo", len(self.undo), len(self.redo)
if len(self.redo) == 0:
result = dialog.alertDialog(self, 'No more to Redo', 'Redo Alert')
return
st = self.redo[-1]
for i in range(9):
for j in range(9):
sqr = self.squares[i,j]
sqr.values = copy.copy(st[i,j].values)
sqr.state = st[i,j].state
self.updateList(sqr)
self.undo.append(st)
del self.redo[-1]
t = self.count()
self.statusBar.text = "Possible choices left %d" % t
## print "redo", len(self.undo), len(self.redo)
def on_hideUndecided_mouseClick(self, event):
self.hideUndecided = self.components.hideUndecided.checked
for i in range(9):
for j in range(9):
self.updateList(self.squares[i,j])
def on_hideChoices_mouseClick(self, event):
self.hideChoices = self.components.hideChoices.checked
def on_doHelpAbout_command(self, event):
dialog.messageDialog(self,
"Sudoku Solver" + "\n\n" + \
"Version %s" % VERSION + "\n\n" + \
"(c) 2005 Alex Tweedly",
"About Sudoku Solver",
wx.ICON_INFORMATION | wx.OK)
pass
def on_doHelpHelp_command(self, event):
if not self.readme:
self.readme = open('readme.txt').read()
dialog.scrolledMessageDialog(self, self.readme, 'Help')
pass
def singles(self):
self.saveUndoPosition()
for i in range(9):
for j in range(9):
sqr = self.squares[i,j]
if len(sqr.values) == 1 and (sqr.state == 'p' or sqr.state == 'a' or sqr.state == 'f'):
sqr.state = 'i'
self.fillin(sqr, i,j)
def soles(self):
self.saveUndoPosition()
for i in range(9):
# check the col
used = {1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0}
last = {1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0}
for j in range(9):
sqr = self.squares[i,j]
for c in sqr.values:
used[c] += 1
last[c] = j
for c,val in used.iteritems():
if val == 1:
ss = self.squares[i, last[c]]
if len(ss.values) <> 1:
ss.values = [c,]
ss.state = 'o'
#rint "col", i, last[c], c
self.fillin(ss,i,last[c])
# check the row
used = {1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0}
last = {1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0}
for j in range(9):
sqr = self.squares[j,i]
for c in sqr.values:
used[c] += 1
last[c] = j
for c,val in used.iteritems():
if val == 1:
ss = self.squares[last[c], i]
if len(ss.values) <> 1:
ss.values = [c,]
ss.state = 'o'
#rint "row", last[c], i, c
self.fillin(ss,last[c],i)
# check the square
used = {1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0}
last = {1:0,2:0,3:0,4:0,5:0,6:0,7:0,8:0,9:0}
for j in range(9):
sqr = self.squares[self.peer[i][j]]
#rint i,j,self.peer[i][j]
for c in sqr.values:
used[c] += 1
last[c] = self.peer[i][j]
for c,val in used.iteritems():
if val == 1:
x,y = last[c]
ss = self.squares[x,y]
#rint i,c,val,ss.values
if len(ss.values) <> 1:
ss.values = [c,]
ss.state = 'o'
#rint "3x3", x, y, c
self.fillin(ss,x,y)
def fillin(self, sqr, i, j):
if len(sqr.values) <> 1:
print "help - bad values list", i,j, ss
value = sqr.values[0]
for d in range(9):
if d == j: continue
ss = self.squares[i,d]
if value in ss.values:
self.delValue(ss, value)
ss.state = 'f'
self.updateList(ss)
#if len(ss.values) == 1: self.fillin(ss,i,d)
for d in range(9):
if d == i: continue
ss = self.squares[d,j]
if value in ss.values:
self.delValue(ss, value)
#if len(ss.values) == 1: self.fillin(ss,d,j)
for d,e in self.peer[3*(i/3) + j/3]:
if d == i and e == j: continue
ss = self.squares[d,e]
if value in ss.values:
self.delValue(ss, value)
ss.state = 'f'
#if len(ss.values) == 1: self.fillin(ss,d,e)
self.updateList(sqr)
t = self.count()
self.statusBar.text = "Possible choices left %d" % t
def count(self):
t = 1
for i in range(9):
for j in range(9):
t *= len(self.squares[i,j].values)
if t == 0:
return 0
return t
def on_Singles_mouseClick(self, event):
self.singles()
def on_Soles_mouseClick(self, event):
self.soles()
def on_solveIt_mouseClick(self, event):
last = -1
t = self.count()
while last <> t and t > 1:
last = t
self.singles()
t = self.count()
if t == last:
self.soles()
t = self.count()
def updateList(self, sqr):
if len(sqr.values) == 0:
items = [" ", " ", " "]
sqr.state = 'z'
elif len(sqr.values) == 1:
items = [" ", " "+str(sqr.values[0])+" ", " "]
else:
s = []
for c in xrange(1,10):
if c in sqr.values:
s.append(str(c))
else:
s.append(" ")
all = " ".join(s)
items = [all[:5], all[6:11], all[12:]]
if self.hideUndecided and (sqr.state == 'u' or sqr.state == 'f'): items = [' ', ' ', ' ']
sqr.comp.items = items
sqr.comp.backgroundColor = COLOR[sqr.state]
def delValue(self, sqr, value):
if value in sqr.values:
del sqr.values[sqr.values.index(value)]
self.updateList(sqr)
def on_popup_command(self, event):
comp = event.target
selected = 0
x = int(comp.name[1])
y = int(comp.name[2])
sqr = self.squares[x,y]
if len(sqr.values) == 1:
if self.hideChoices and (sqr.state == 'u' or sqr.state == 'f'):
items = ['1','2','3','4','5','6','7','8','9']
else:
selected = sqr.values[0]
else:
# make a menu
self.menu = wx.Menu()
# add the items
items = []
if self.hideChoices:
items = ['1','2','3','4','5','6','7','8','9']
else:
for c in sqr.values:
items.append(str(c))
if selected == 0:
# Popup the menu.
selected = helpful.popUpMenu(self, items, comp.position)
if selected:
selected = int(selected)
else:
selected = 0
if selected > 0:
self.saveUndoPosition()
sqr.values = [selected,]
sqr.state = 'a'
self.updateList(sqr)
comp.backgroundColor = (200,100,100)
self.fillin(sqr, int(comp.name[1]), int(comp.name[2]))
if __name__ == '__main__':
app = model.Application(MyBackground)
app.MainLoop()
|