home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Club Amiga de Montreal - CAM
/
CAM_CD_1.iso
/
files
/
116.lha
/
SmallTalk
/
Sources
/
QUEEN.ST
< prev
next >
Wrap
Text File
|
1986-11-20
|
1KB
|
66 lines
Class Queen Object #row #column #neighbor
Method Queen
setColumn: aNumber neighbor: aQueen
column <- aNumber.
neighbor <- aQueen
|
Method Queen
checkRow: testRow column: testColumn | columnDifference |
columnDifference <- testColumn - column.
(((row = testRow) or:
[ row + columnDifference = testRow]) or:
[ row - columnDifference = testRow])
ifTrue: [ ^ true ].
(neighbor notNil)
ifTrue: [ ^ neighbor checkRow: testRow
column: testColumn ]
ifFalse: [ ^ false ]
|
Method Queen
first
(neighbor notNil)
ifTrue: [ neighbor first ].
row <- 1.
^ self testPosition
|
Method Queen
next
(row = 8)
ifTrue: [ ((neighbor isNil) or: [neighbor next isNil])
ifTrue: [ ^ nil ].
row <- 0 ].
row <- row + 1.
^ self testPosition
|
Method Queen
testPosition
(neighbor isNil) ifTrue: [ ^ self ].
(neighbor checkRow: row column: column)
ifTrue: [ ^ self next ]
ifFalse: [ ^ self ]
|
Method Queen
result
^ ((neighbor isNil)
ifTrue: [ List new ]
ifFalse: [ neighbor result ] )
addLast: row
|
Method Test
queen | lastQueen |
lastQueen <- nil.
(1 to: 8) do: [:i | lastQueen <- Queen new;
setColumn: i neighbor: lastQueen ].
lastQueen first.
(lastQueen result asArray = #(1 5 8 6 3 7 2 4) )
ifTrue: ['8 queens test passed' print]
ifFalse: [smalltalk error: '8queen test failed']
|