Tue Jun 21, 2016 7:06 pm
# an approach to the 8 queens problem in fig
# public domain
# the idea is to set up the arrays of strings, as they are here:
# to get an array of say, row 8... now = r : mid 8 1 : split now " "
# that sets now to the python array ['a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8']
# so if you place a queen on that row, you can:
# * iterate through the other row items:
# * string find each of those items in columns and diags
# * remove those diags and columns and row items from board array "a"
# # technically, a is a string.
# now split a " " # now its an array named "now"
# a now # if you want to copy the array back to a, this did
# now join now " " # now its a string copied to now
# a now # now its a string copied to a (weve copied string to string a)
# #### how to string replace a substring:
# now = split a "a7" join now "x" # replace all instances of a7 in string a with "x" in string now
# a now # copy string now to string a
r "" arr times 8
r arrset 1 "a8 b8 c8 d8 e8 f8 g8 h8"
r arrset 2 "a7 b7 c7 d7 e7 f7 g7 h7"
r arrset 3 "a6 b6 c6 d6 e6 f6 g6 h6"
r arrset 4 "a5 b5 c5 d5 e5 f5 g5 h5"
r arrset 5 "a4 b4 c4 d4 e4 f4 g4 h4"
r arrset 6 "a3 b3 c3 d3 e3 f3 g3 h3"
r arrset 7 "a2 b2 c2 d2 e2 f2 g2 h2"
r arrset 8 "a1 b1 c1 d1 e1 f1 g1 h1"
c "" arr times 8
c arrset 1 "a8 a7 a6 a5 a4 a3 a2 a1"
c arrset 2 "b8 b7 b6 b5 b4 b3 b2 b1"
c arrset 3 "c8 c7 c6 c5 c4 c3 c2 c1"
c arrset 4 "d8 d7 d6 d5 d4 d3 d2 d1"
c arrset 5 "e8 e7 e6 e5 e4 e3 e2 e1"
c arrset 6 "f8 f7 f6 f5 f4 f3 f2 f1"
c arrset 7 "g8 g7 g6 g5 g4 g3 g2 g1"
c arrset 8 "h8 h7 h6 h5 h4 h3 h2 h1"
d "" arr times 30
d arrset 1 "a1"
d arrset 2 "a2 b1"
d arrset 3 "a3 b2 c1"
d arrset 4 "a4 b3 c2 d1"
d arrset 5 "a5 b4 c3 d2 e1"
d arrset 6 "a6 b5 c4 d3 e2 f1"
d arrset 7 "a7 b6 c5 d4 e3 f2 g1"
d arrset 8 "a8 b7 c6 d5 e4 f3 g2 h1"
d arrset 9 "b8 c7 d6 e5 f4 g3 h2"
d arrset 10 "c8 d7 e6 f5 g4 h3"
d arrset 11 "d8 e7 f6 g5 h4"
d arrset 12 "e8 f7 g6 h5"
d arrset 13 "f8 g7 h6"
d arrset 14 "g8 h7"
d arrset 15 "h8"
d arrset 16 "a8"
d arrset 17 "a7 b8"
d arrset 18 "a6 b7 c8"
d arrset 19 "a5 b6 c7 d8"
d arrset 20 "a4 b5 c6 d7 e8"
d arrset 21 "a3 b4 c5 d6 e7 f8"
d arrset 22 "a2 b3 c4 d5 e6 f7 g8"
d arrset 23 "a1 b2 c3 d4 e5 f6 g7 h8"
d arrset 24 "b1 c2 d3 e4 f5 g6 h7"
d arrset 25 "c1 d2 e3 f4 g5 h6"
d arrset 26 "d1 e2 f3 g4 h5"
d arrset 27 "e1 f2 g3 h4"
d arrset 28 "f1 g2 h3"
d arrset 29 "g1 h2"
d arrset 30 "h1"
a = join r " "
# abcdefgh
# 8
# 7
# 6
# 5
# 4
# 3
# 2
# 1
# r 8 rows, 1 row per space-delim string
# c 8 cols, 1 col per space-delim string
# a 64 positions, space-delim single string
# d 1-15 diags, 1 diag per space-delim string
# d 16-30 diags, 1 diag per space-delim string
# to get row 8 as a space-delimited string, convert to array of 8 position names:
# now = r : mid 8 1 : split now " " # ['a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8']
copy a split copy " "
#function aset ar po
function disp ar
now "" print
c 0
for y 1 8 1
for x 1 8 1
inc c plus 1 swap inc c
now ar mid c 1 plus " " left 3 prints
next
now "" print
next
fig
function xrows ar pose
now return ar
fig
function xcols ar pose
now return ar
fig
function xdgs ar pose
now return ar
fig
for pose 1 4 1
ag arrget copy pose
ifequal ag "x"
pass
else
ifequal ag "q"
pass
else
copy arrset pose "q"
copy xrows copy pose
copy xcols copy pose
copy xdgs copy pose
fig
fig
now disp copy
next
Tue Jun 21, 2016 11:59 pm
# an approach to the 8 queens problem in fig
# public domain
# the idea is to set up the arrays of strings, as they are here:
# to get an array of say, row 8... now = r : mid 8 1 : split now " "
# that sets now to the python array ['a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8']
# so if you place a queen on that row, you can:
# * iterate through the other row items:
# * string find each of those items in columns and diags
# * remove those diags and columns and row items from board array "a"
# # technically, a is a string.
# now split a " " # now its an array named "now"
# a now # if you want to copy the array back to a, this did
# now join now " " # now its a string copied to now
# a now # now its a string copied to a (weve copied string to string a)
# #### how to string replace a substring:
# now = split a "a7" join now "x" # replace all instances of a7 in string a with "x" in string now
# a now # copy string now to string a
r "" arr times 8
r arrset 1 "a8 b8 c8 d8 e8 f8 g8 h8"
r arrset 2 "a7 b7 c7 d7 e7 f7 g7 h7"
r arrset 3 "a6 b6 c6 d6 e6 f6 g6 h6"
r arrset 4 "a5 b5 c5 d5 e5 f5 g5 h5"
r arrset 5 "a4 b4 c4 d4 e4 f4 g4 h4"
r arrset 6 "a3 b3 c3 d3 e3 f3 g3 h3"
r arrset 7 "a2 b2 c2 d2 e2 f2 g2 h2"
r arrset 8 "a1 b1 c1 d1 e1 f1 g1 h1"
c "" arr times 8
c arrset 1 "a8 a7 a6 a5 a4 a3 a2 a1"
c arrset 2 "b8 b7 b6 b5 b4 b3 b2 b1"
c arrset 3 "c8 c7 c6 c5 c4 c3 c2 c1"
c arrset 4 "d8 d7 d6 d5 d4 d3 d2 d1"
c arrset 5 "e8 e7 e6 e5 e4 e3 e2 e1"
c arrset 6 "f8 f7 f6 f5 f4 f3 f2 f1"
c arrset 7 "g8 g7 g6 g5 g4 g3 g2 g1"
c arrset 8 "h8 h7 h6 h5 h4 h3 h2 h1"
d "" arr times 30
d arrset 1 "a1"
d arrset 2 "a2 b1"
d arrset 3 "a3 b2 c1"
d arrset 4 "a4 b3 c2 d1"
d arrset 5 "a5 b4 c3 d2 e1"
d arrset 6 "a6 b5 c4 d3 e2 f1"
d arrset 7 "a7 b6 c5 d4 e3 f2 g1"
d arrset 8 "a8 b7 c6 d5 e4 f3 g2 h1"
d arrset 9 "b8 c7 d6 e5 f4 g3 h2"
d arrset 10 "c8 d7 e6 f5 g4 h3"
d arrset 11 "d8 e7 f6 g5 h4"
d arrset 12 "e8 f7 g6 h5"
d arrset 13 "f8 g7 h6"
d arrset 14 "g8 h7"
d arrset 15 "h8"
d arrset 16 "a8"
d arrset 17 "a7 b8"
d arrset 18 "a6 b7 c8"
d arrset 19 "a5 b6 c7 d8"
d arrset 20 "a4 b5 c6 d7 e8"
d arrset 21 "a3 b4 c5 d6 e7 f8"
d arrset 22 "a2 b3 c4 d5 e6 f7 g8"
d arrset 23 "a1 b2 c3 d4 e5 f6 g7 h8"
d arrset 24 "b1 c2 d3 e4 f5 g6 h7"
d arrset 25 "c1 d2 e3 f4 g5 h6"
d arrset 26 "d1 e2 f3 g4 h5"
d arrset 27 "e1 f2 g3 h4"
d arrset 28 "f1 g2 h3"
d arrset 29 "g1 h2"
d arrset 30 "h1"
a = join r " "
# abcdefgh
# 8
# 7
# 6
# 5
# 4
# 3
# 2
# 1
# r 8 rows, 1 row per space-delim string
# c 8 cols, 1 col per space-delim string
# a 64 positions, space-delim single string
# d 1-15 diags, 1 diag per space-delim string
# d 16-30 diags, 1 diag per space-delim string
# to get row 8 as a space-delimited string, convert to array of 8 position names:
# now = r : mid 8 1 : split now " " # ['a8', 'b8', 'c8', 'd8', 'e8', 'f8', 'g8', 'h8']
copy a split copy " "
#function aset ar po
function disp ar title
arlen ar len
xlen 8
ifmore arlen 64
xlen 16
fig
now "" print
now title print
c 0
for y 1 8 1
for x 1 xlen 1
inc c plus 1 swap inc c
now ar mid c 1 plus " " left 3 prints
next
now "" print
next
fig
function xorq ar x q pose
#### if position is not xd then place a q at position, or:
#### if position is not qd then place a x at position
ag arrget ar pose
ifequal ag x
pass
else
ifequal ag q
pass
else
ar arrset pose q
fig
fig
now return ar
fig
function ror ar ch
#### check and x each row / col / diag that just gained a queen
arlen ar len
now "ror called" print
for arl 1 arlen 1
now arrget ar arl
innow instr now ch
iftrue innow
splitnow split now " "
nowlen splitnow len
for nl 1 nowlen 1
splitnow xorq splitnow "q" "x" nl
next
now join splitnow " "
fig
ar arrset arl now
next
now return ar
fig
rcopy r
ccopy c
dcopy d
function arck ar ch
#### check if position ch is in ar
total join ar " "
ck instr total ch
now return ck
fig
#### loop through each place on 8x8 "copy"
for pose 1 64 1
ch arrget copy pose
#### if rows and cols and diags dont have an x at position ch,
#### place a q at pose in board model
#### ror will then be called to update (disqualify) copy of each r/c/d for next pose
rck arck rcopy ch
iftrue rck
cck arck ccopy ch
iftrue cck
dck arck dcopy ch
iftrue dck
now xorq copy "x" "q" pose
rcopy ror rcopy ch
ccopy ror ccopy ch
dcopy ror dcopy ch
now disp copy "a:"
fig
fig
fig
next
now "" print
now
now join rcopy " " split now " " disp now "r copy:"
now join ccopy " " split now " " disp now "c copy:"
now join dcopy " " split now " " disp now "d copy:"
now disp copy "a:"
ror called
ror called
ror called
a:
q b8 c8 d8 e8 f8 g8 h8
a7 b7 c7 d7 e7 f7 g7 h7
a6 b6 c6 d6 e6 f6 g6 h6
a5 b5 c5 d5 e5 f5 g5 h5
a4 b4 c4 d4 e4 f4 g4 h4
a3 b3 c3 d3 e3 f3 g3 h3
a2 b2 c2 d2 e2 f2 g2 h2
a1 b1 c1 d1 e1 f1 g1 h1
ror called
ror called
ror called
a:
q b8 c8 d8 e8 f8 g8 h8
a7 q c7 d7 e7 f7 g7 h7
a6 b6 c6 d6 e6 f6 g6 h6
a5 b5 c5 d5 e5 f5 g5 h5
a4 b4 c4 d4 e4 f4 g4 h4
a3 b3 c3 d3 e3 f3 g3 h3
a2 b2 c2 d2 e2 f2 g2 h2
a1 b1 c1 d1 e1 f1 g1 h1
ror called
ror called
ror called
a:
q b8 c8 d8 e8 f8 g8 h8
a7 q c7 d7 e7 f7 g7 h7
a6 b6 q d6 e6 f6 g6 h6
a5 b5 c5 d5 e5 f5 g5 h5
a4 b4 c4 d4 e4 f4 g4 h4
a3 b3 c3 d3 e3 f3 g3 h3
a2 b2 c2 d2 e2 f2 g2 h2
a1 b1 c1 d1 e1 f1 g1 h1
ror called
ror called
ror called
a:
q b8 c8 d8 e8 f8 g8 h8
a7 q c7 d7 e7 f7 g7 h7
a6 b6 q d6 e6 f6 g6 h6
a5 b5 c5 q e5 f5 g5 h5
a4 b4 c4 d4 e4 f4 g4 h4
a3 b3 c3 d3 e3 f3 g3 h3
a2 b2 c2 d2 e2 f2 g2 h2
a1 b1 c1 d1 e1 f1 g1 h1
ror called
ror called
ror called
a:
q b8 c8 d8 e8 f8 g8 h8
a7 q c7 d7 e7 f7 g7 h7
a6 b6 q d6 e6 f6 g6 h6
a5 b5 c5 q e5 f5 g5 h5
a4 b4 c4 d4 q f4 g4 h4
a3 b3 c3 d3 e3 f3 g3 h3
a2 b2 c2 d2 e2 f2 g2 h2
a1 b1 c1 d1 e1 f1 g1 h1
ror called
ror called
ror called
a:
q b8 c8 d8 e8 f8 g8 h8
a7 q c7 d7 e7 f7 g7 h7
a6 b6 q d6 e6 f6 g6 h6
a5 b5 c5 q e5 f5 g5 h5
a4 b4 c4 d4 q f4 g4 h4
a3 b3 c3 d3 e3 q g3 h3
a2 b2 c2 d2 e2 f2 g2 h2
a1 b1 c1 d1 e1 f1 g1 h1
ror called
ror called
ror called
a:
q b8 c8 d8 e8 f8 g8 h8
a7 q c7 d7 e7 f7 g7 h7
a6 b6 q d6 e6 f6 g6 h6
a5 b5 c5 q e5 f5 g5 h5
a4 b4 c4 d4 q f4 g4 h4
a3 b3 c3 d3 e3 q g3 h3
a2 b2 c2 d2 e2 f2 q h2
a1 b1 c1 d1 e1 f1 g1 h1
ror called
ror called
ror called
a:
q b8 c8 d8 e8 f8 g8 h8
a7 q c7 d7 e7 f7 g7 h7
a6 b6 q d6 e6 f6 g6 h6
a5 b5 c5 q e5 f5 g5 h5
a4 b4 c4 d4 q f4 g4 h4
a3 b3 c3 d3 e3 q g3 h3
a2 b2 c2 d2 e2 f2 q h2
a1 b1 c1 d1 e1 f1 g1 q
r copy:
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
c copy:
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
d copy:
a1 a2 b1 a3 b2 c1 a4 b3 c2 d1 a5 b4 c3 d2 e1 a6
b5 c4 d3 e2 f1 a7 b6 c5 d4 e3 f2 g1 x x x x
x x x x b8 c7 d6 e5 f4 g3 h2 c8 d7 e6 f5 g4
h3 d8 e7 f6 g5 h4 e8 f7 g6 h5 f8 g7 h6 g8 h7 h8
x a7 b8 x x x a5 b6 c7 d8 x x x x x a3
b4 c5 d6 e7 f8 x x x x x x x a1 b2 c3 d4
e5 f6 g7 h8 x x x x x x x c1 d2 e3 f4 g5
h6 x x x x x e1 f2 g3 h4 x x x g1 h2 x
a:
q b8 c8 d8 e8 f8 g8 h8
a7 q c7 d7 e7 f7 g7 h7
a6 b6 q d6 e6 f6 g6 h6
a5 b5 c5 q e5 f5 g5 h5
a4 b4 c4 d4 q f4 g4 h4
a3 b3 c3 d3 e3 q g3 h3
a2 b2 c2 d2 e2 f2 q h2
a1 b1 c1 d1 e1 f1 g1 q