home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 231_01 / 8queen.st < prev    next >
Text File  |  1987-06-17  |  2KB  |  57 lines

  1. Class Queen
  2. | myrow mycolumn neighbor boardsize |
  3. [
  4.         build: aQueen col: aNumber size: brdmax
  5.  
  6.                 neighbor <- aQueen.
  7.                 mycolumn <- aNumber.
  8.                 myrow <- 1.
  9.                 boardsize <- brdmax.
  10.                 neighbor first.
  11.                 ^ self
  12.  
  13. |       checkCol: colNumber row: rowNumber      | cd |
  14.                 (rowNumber = myrow) ifTrue: [ ^ false ].
  15.                 cd <- colNumber - mycolumn.
  16.                 ((myrow + cd) = rowNumber) ifTrue: [ ^ false ].
  17.                 ((myrow - cd) = rowNumber) ifTrue: [ ^ false ].
  18.                 (neighbor isNil) ifFalse:
  19.                         [ ^ neighbor checkCol: colNumber row: rowNumber ].
  20.                 ^ true
  21.  
  22. |       first
  23.                 myrow <- 1.
  24.                 ^ self checkrow
  25.  
  26. |       next
  27.                 myrow <- myrow + 1.
  28.                 ^ self checkrow
  29.  
  30. |       checkrow
  31.                 (neighbor isNil) ifTrue: [^ myrow].
  32.                 [myrow <= boardsize] whileTrue:
  33.                         [(neighbor checkCol: mycolumn row: myrow)
  34.                                 ifTrue: [^ myrow]
  35.                                 ifFalse: [myrow <- myrow + 1] ].
  36.                 ((neighbor next) isNil) ifTrue: [^ nil].
  37.                 ^ self first
  38.  
  39. |       printboard
  40.                 (neighbor isNil) ifFalse: [ neighbor printboard].
  41.                 ('Col ', mycolumn asString , ' Row ' ,
  42.                     myrow asString) print
  43. ]
  44.  
  45. Class Main
  46. | lastq |
  47. [
  48.         main | size |
  49.  
  50.                 size <- 8.
  51.                 lastq <- nil.
  52.                 (1 to: size) do: [:x |
  53.                          lastq <- Queen new build: lastq col: x size: size ].
  54.                 lastq first.
  55.                 lastq printboard
  56. ]
  57.