98 lines
3.4 KiB
Plaintext
98 lines
3.4 KiB
Plaintext
-- Test file for Kate's Euphoria syntax highlighting/code folding.
|
|
<beginfold id='1'>-- BEGIN region marker test</beginfold id='1'>
|
|
|
|
-- code here
|
|
|
|
<endfold id='1'>-- END region marker test</endfold id='1'>
|
|
|
|
-- The N Queens Problem:
|
|
-- Place N Queens on an NxN chess board
|
|
-- such that they don't threaten each other.
|
|
constant N = 8 -- try some other sizes
|
|
constant ROW = 1, COLUMN = 2
|
|
constant TRUE = 1, FALSE = 0
|
|
<beginfold id='2'>type</beginfold id='2'> square(sequence x)
|
|
-- a square on the board
|
|
return length(x) = 2
|
|
<endfold id='2'>end type</endfold id='2'>
|
|
<beginfold id='2'>type</beginfold id='2'> row(integer x)
|
|
-- a row on the board
|
|
return x >= 1 and x <= N
|
|
<endfold id='2'>end type</endfold id='2'>
|
|
|
|
<beginfold id='3'>function</beginfold id='3'> threat(square q1, square q2)
|
|
-- do two queens threaten each other?
|
|
<beginfold id='4'>if</beginfold id='4'> q1[COLUMN] = q2[COLUMN] then
|
|
return TRUE
|
|
elsif q1[ROW] - q1[COLUMN] = q2[ROW] - q2[COLUMN] then
|
|
return TRUE
|
|
elsif q1[ROW] + q1[COLUMN] = q2[ROW] + q2[COLUMN] then
|
|
return TRUE
|
|
elsif q1[ROW] = q2[ROW] then
|
|
return TRUE
|
|
else
|
|
return FALSE
|
|
<endfold id='4'>end if</endfold id='4'>
|
|
<endfold id='3'>end function</endfold id='3'>
|
|
|
|
<beginfold id='3'>function</beginfold id='3'> conflict(square q, sequence queens)
|
|
-- Would square p cause a conflict with other queens on board so far?
|
|
<beginfold id='5'>for</beginfold id='5'> i = 1 to length(queens) do
|
|
<beginfold id='4'>if</beginfold id='4'> threat(q, queens[i]) then
|
|
return TRUE
|
|
<endfold id='4'>end if</endfold id='4'>
|
|
<endfold id='5'>end for</endfold id='5'>
|
|
return FALSE
|
|
<endfold id='3'>end function</endfold id='3'>
|
|
|
|
integer soln
|
|
soln = 0 -- solution number
|
|
|
|
<beginfold id='6'>procedure</beginfold id='6'> print_board(sequence queens)
|
|
-- print a solution, showing the Queens on the board
|
|
integer k
|
|
position(1, 1)
|
|
printf(1, "Solution #%d\n\n ", soln)
|
|
<beginfold id='5'>for</beginfold id='5'> c = 'a' to 'a' + N - 1 do
|
|
printf(1, "%2s", c)
|
|
<endfold id='5'>end for</endfold id='5'>
|
|
puts(1, "\n")
|
|
<beginfold id='5'>for</beginfold id='5'> r = 1 to N do
|
|
printf(1, "%2d ", r)
|
|
<beginfold id='5'>for</beginfold id='5'> c = 1 to N do
|
|
<beginfold id='4'>if</beginfold id='4'> find({r,c}, queens) then
|
|
puts(1, "Q ")
|
|
else
|
|
puts(1, ". ")
|
|
<endfold id='4'>end if</endfold id='4'>
|
|
<endfold id='5'>end for</endfold id='5'>
|
|
puts(1, "\n")
|
|
<endfold id='5'>end for</endfold id='5'>
|
|
puts(1, "\nPress Enter. (q to quit) ")
|
|
<beginfold id='7'>while</beginfold id='7'> TRUE do
|
|
k = get_key()
|
|
<beginfold id='4'>if</beginfold id='4'> k = 'q' then
|
|
abort(0)
|
|
elsif k != -1 then
|
|
exit
|
|
<endfold id='4'>end if</endfold id='4'>
|
|
<endfold id='7'>end while</endfold id='7'>
|
|
<endfold id='6'>end procedure</endfold id='6'>
|
|
|
|
<beginfold id='6'>procedure</beginfold id='6'> place_queen(sequence queens)
|
|
-- place queens on a NxN chess board
|
|
-- (recursive procedure)
|
|
row r -- only need to consider one row for each queen
|
|
<beginfold id='4'>if</beginfold id='4'> length(queens) = N then
|
|
soln += 1
|
|
print_board(queens)
|
|
return
|
|
<endfold id='4'>end if</endfold id='4'>
|
|
r = length(queens)+1
|
|
<beginfold id='5'>for</beginfold id='5'> c = 1 to N do
|
|
<beginfold id='4'>if</beginfold id='4'> not conflict({r,c}, queens) then
|
|
place_queen(append(queens, {r,c}))
|
|
<endfold id='4'>end if</endfold id='4'>
|
|
<endfold id='5'>end for</endfold id='5'>
|
|
<endfold id='6'>end procedure</endfold id='6'>
|