Newsqueak2
'Sudoku'
class SudokuGame usingPlatform: platform hopscotchExtensions: hopscotchExtensions = NewspeakObject (
"Copyright 2010-2011 Vadim Tsushko.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the ''Software''), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ''AS IS'', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"
|
Presenter = hopscotchExtensions PresenterExt.
Subject = platform hopscotch core Subject.
protected Time = platform Time.
protected Random = platform Random.
Color = platform Color.
Row = platform brazil containers Row.
Grid = platform brazil containers Grid.
Set = platform collections Set.
boardPresenter
HopscotchWindow = hopscotchExtensions HopscotchWindowExt.
SubmenuItem = platform brazil menus SubmenuItem.
MenuItem = platform brazil menus MenuItem.
Menu = platform brazil menus Menu.
SeparatorItem = platform brazil menus SeparatorItem.
currentLanguage::='English'.
Dictionary = platform collections Dictionary.
i18nEngine = I18n new.
showConflictMode::=false.
|
)
(
class BoardGridPresenter onSubject: s = Presenter onSubject: s (
)
('as yet unclassified'
cellForRow: r column: c bigRow: bigRow bigColumn: bigColumn = (
|column row|
column:: ((bigColumn-1)* 3) + c.
row:: ((bigRow-1)* 3) + r.
^cellPresenter: (subject model atCol: column row: row)
)
cellPresenter: cell = (
^(CellSubject onModel: cell) presenter
)
definition = (
|grid|
grid:: gridWithRows: 3 columns: 3 cellFragmentBlock: [:r :c| grid3x3AtRow: r column: c].
^(padded: grid with: {4. 4. 4. 4.}) color: Color black.
)
fragmentForRow: r column: c = (
^cellPresenter: (subject model atCol: c row: r)
)
grid3x3AtRow: bigRow column: bigColumn = (
|grid|
grid:: gridWithRows: 3 columns: 3 cellFragmentBlock: [:r :c| cellForRow: r column: c bigRow: bigRow bigColumn: bigColumn] copy fixTemps.
^(padded: grid with: {2. 2. 2. 2.}) color: Color black.
)
)
class BoardSubject onModel: m = Subject onModel: m(
|
startedAt = Time now.
level
|
)
('as yet unclassified'
createPresenter = (
^BoardPresenter onSubject: self.
)
) : (
'as yet unclassified'
newValidBoardWithDifficulty: level = (
|board|
board:: Board new generateValidBoard.
board setDifficultyLevel: level.
^BoardSubject onModel: board.
)
randomBoard = (
^BoardSubject onModel: Board new generateRandomBoard.
)
)
class Cell withValue: v col: c row: r = (
"Describe the class in this comment."
|
value
currentValue
row
column
region
enabled
inConflict
revealed
availableValues
|
value:: v.
column:: c.
row:: r.
region:: regionForCol: c row: r.
enabled:: true.
revealed:: false.
inConflict: false.
currentValue:: 0.
)
('as yet unclassified'
displayValue = (
|res|
res:: currentValue = 0
ifTrue: ['...']
ifFalse: [currentValue printString] .
^res
)
isPreopened = (
^enabled not and: [revealed not]
)
open = (
currentValue:: value.
enabled: false.
)
printOn: aStream = (
aStream nextPutAll: 'SudokuGame::Cell(value:'.
value printOn:aStream.
aStream nextPutAll:', currentValue: ' .
currentValue printOn: aStream.
aStream nextPutAll:', row: ' .
row printOn: aStream.
aStream nextPutAll:', column: ' .
column printOn: aStream.
aStream nextPutAll: ')')
regionForCol: col row: row = (
(col between: 1 and: 3) &
(row between: 1 and: 3) ifTrue: [^1].
(col between: 4 and: 6) &
(row between: 1 and: 3) ifTrue: [^2].
(col between: 7 and: 9) &
(row between: 1 and: 3) ifTrue: [^3].
(col between: 1 and: 3) &
(row between: 4 and: 6) ifTrue: [^4].
(col between: 4 and: 6) &
(row between: 4 and: 6) ifTrue: [^5].
(col between: 7 and: 9) &
(row between: 4 and: 6) ifTrue: [^6].
(col between: 1 and: 3) &
(row between: 7 and: 9) ifTrue: [^7].
(col between: 4 and: 6) &
(row between: 7 and: 9) ifTrue: [^8].
(col between: 7 and: 9) &
(row between: 7 and: 9) ifTrue: [^9].
error: ''.
)
reveal = (
open.
revealed: true.
)
)
class SudokuWindow = HopscotchWindow(
)
('as yet unclassified'
addMenuBarItemsTo: menu