From a384cebb646380b1a2dd88c487ef3f7cb0b19d50 Mon Sep 17 00:00:00 2001 From: diemheych <100515426+diemheych@users.noreply.github.com> Date: Mon, 2 Oct 2023 11:24:08 +1100 Subject: [PATCH] N-Queens in FORTH Example N-Queens for 8x8 board in FORTH for Prime FORTH --- nqueens.fth | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 nqueens.fth diff --git a/nqueens.fth b/nqueens.fth new file mode 100644 index 0000000..a97a249 --- /dev/null +++ b/nqueens.fth @@ -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 +;