1-- Test file for Kate's Euphoria syntax highlighting/code folding.
2-- BEGIN region marker test
3
4-- code here
5
6-- END region marker test
7
8-- The N Queens Problem:
9-- Place N Queens on an NxN chess board
10-- such that they don't threaten each other.
11constant N = 8 -- try some other sizes
12constant ROW = 1, COLUMN = 2
13constant TRUE = 1, FALSE = 0
14type square(sequence x)
15-- a square on the board
16    return length(x) = 2
17end type
18type row(integer x)
19-- a row on the board
20    return x >= 1 and x <= N
21end type
22
23function threat(square q1, square q2)
24-- do two queens threaten each other?
25    if q1[COLUMN] = q2[COLUMN] then
26 return TRUE
27    elsif q1[ROW] - q1[COLUMN] = q2[ROW] - q2[COLUMN] then
28 return TRUE
29    elsif q1[ROW] + q1[COLUMN] = q2[ROW] + q2[COLUMN] then
30 return TRUE
31    elsif q1[ROW] = q2[ROW] then
32 return TRUE
33    else
34 return FALSE
35    end if
36end function
37
38function conflict(square q, sequence queens)
39-- Would square p cause a conflict with other queens on board so far?
40    for i = 1 to length(queens) do
41 if threat(q, queens[i]) then
42     return TRUE
43 end if
44    end for
45    return FALSE
46end function
47
48integer soln
49soln = 0 -- solution number
50
51procedure print_board(sequence queens)
52-- print a solution, showing the Queens on the board
53    integer k
54    position(1, 1)
55    printf(1, "Solution #%d\n\n  ", soln)
56    for c = 'a' to 'a' + N - 1 do
57 printf(1, "%2s", c)
58    end for
59    puts(1, "\n")
60    for r = 1 to N do
61 printf(1, "%2d ", r)
62 for c = 1 to N do
63     if find({r,c}, queens) then
64  puts(1, "Q ")
65     else
66  puts(1, ". ")
67     end if
68 end for
69 puts(1, "\n")
70    end for
71    puts(1, "\nPress Enter. (q to quit) ")
72    while TRUE do
73 k = get_key()
74 if k = 'q' then
75     abort(0)
76 elsif k != -1 then
77     exit
78 end if
79    end while
80end procedure
81
82procedure place_queen(sequence queens)
83-- place queens on a NxN chess board
84-- (recursive procedure)
85    row r -- only need to consider one row for each queen
86    if length(queens) = N then
87 soln += 1
88 print_board(queens)
89 return
90    end if
91    r = length(queens)+1
92    for c = 1 to N do
93 if not conflict({r,c}, queens) then
94     place_queen(append(queens, {r,c}))
95 end if
96    end for
97end procedure
98