mirror of
https://github.com/diemheych/PrimeFORTH
synced 2024-12-25 09:58:33 +01:00
N-Queens in FORTH
Example N-Queens for 8x8 board in FORTH for Prime FORTH
This commit is contained in:
parent
8b08f501b2
commit
a384cebb64
1 changed files with 48 additions and 0 deletions
48
nqueens.fth
Normal file
48
nqueens.fth
Normal file
|
@ -0,0 +1,48 @@
|
|||
8 constant rr
|
||||
variable ii
|
||||
variable ss
|
||||
variable xx
|
||||
variable yy
|
||||
create aa rr 1 + allot
|
||||
: rclaa @ aa + @ ;
|
||||
: stoaa @ aa + ! ;
|
||||
|
||||
: NQPRINT
|
||||
1 II !
|
||||
BEGIN
|
||||
II RCLAA .
|
||||
1 II +!
|
||||
II @ XX @ 1 + = until
|
||||
;
|
||||
|
||||
: NQCORE
|
||||
0 SS !
|
||||
0 XX !
|
||||
BEGIN
|
||||
1 XX +!
|
||||
RR XX STOAA
|
||||
BEGIN
|
||||
1 SS +!
|
||||
XX @ YY !
|
||||
BEGIN YY @ DUP II ! 1 > IF
|
||||
-1 YY +!
|
||||
XX RCLAA YY RCLAA - DUP
|
||||
0 = SWAP ABS XX @ YY @ - = OR IF
|
||||
0 YY !
|
||||
BEGIN XX RCLAA 1 - DUP XX STOAA DUP 0 = IF
|
||||
-1 XX +!
|
||||
then
|
||||
0 <>
|
||||
UNTIL
|
||||
THEN
|
||||
THEN
|
||||
II @ 2 <
|
||||
UNTIL
|
||||
YY @ 1 = UNTIL
|
||||
RR XX @ = UNTIL
|
||||
;
|
||||
|
||||
: NQUEENS
|
||||
NQCORE
|
||||
NQPRINT
|
||||
;
|
Loading…
Reference in a new issue