* * Little Smalltalk, * 8 Queen puzzle (modified from Tim Budd's 'queen.st' ) * Written by David H. Lorenz, Technion, May 1993 * * This file is almost identical to Budd's "queen.st". * To use, first file in this file, then create an new object of * class Board and send it the message firstSolution. * * '8queen.st' r * Board new firstSolution * Class Board Object lastQueen Methods Board new lastQueen <- QueensListTerminator new. (1 to: 8) do: [:i | lastQueen <- QueensList new; setColumn: i neighbor: lastQueen ] | firstSolution lastQueen firstSolution. ^ lastQueen result | nextSolution | result | lastQueen nextSolution. ^ lastQueen result ] Class QueensListTerminator Object Methods QueensListTerminator firstSolution ^ true | nextSolution ^ true | canAttack: row x: column " we can't attack anything " ^ false | result ^ List new ] Class QueensList Object row column neighbor Methods QueensList setColumn: aColumn neighbor: aQueen column <- aColumn. neighbor <- aQueen | canAttack: testRow x: testColumn | columnDifference | columnDifference <- testColumn - column. (((row = testRow) or: [ row + columnDifference = testRow]) or: [ row - columnDifference = testRow]) ifTrue: [ ^ true ]. ^ neighbor canAttack: testRow x: testColumn | advance (row = 8) ifTrue: [ (neighbor nextSolution) ifFalse: [^ false]. row <- 0 ]. row <- row + 1. ^ true | moveToNextNonAttackedRow [neighbor canAttack: row x: column] whileTrue: [ (self advance) ifFalse: [ ^ false ]]. ^ true | firstSolution neighbor firstSolution. row <- 1. ^ self moveToNextNonAttackedRow | nextSolution ^ (self advance) and: [ self moveToNextNonAttackedRow] | result ^ neighbor result addLast: row ]