Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/knights-tour.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / knights-tour.wiki`
149 lines ยท 2827 bytes Raw Blame History
1 =={{header|Omegese}}==
2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text minus "-"
9 text tstart "Warnsdorff tour from a1; each square shows its move number, rank 8 at the top\n"
10 global board ulong 64
11 global mdx long 8
12 global mdy long 8
13 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
14 fn emit a view uchar * to void
15 o = ulong 0
16 while lt o len a bound 4096
17 k = write 1 a o sub len a o
18 if le k 0
19 exit 1
20 end
21 o = add o ulong k
22 end
23 end
24 note emitu writes n in decimal; 20 digits hold every ulong
25 fn emitu n ulong to void
26 d = new uchar 20 0
27 i = ulong 20
28 v = n
29 while true bound 20
30 i = sub i 1
31 put d i uchar add rem v 10 48
32 v = div v 10
33 if eq v 0
34 break
35 end
36 end
37 emit part d i sub 20 i
38 end
39 note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut)
40 fn emitw n ulong w ulong to void
41 d = new uchar 20 32
42 i = ulong 20
43 v = n
44 while true bound 20
45 i = sub i 1
46 put d i uchar add rem v 10 48
47 v = div v 10
48 if eq v 0
49 break
50 end
51 end
52 if gt w sub 20 i
53 i = sub 20 w
54 end
55 emit part d i sub 20 i
56 end
57 note target is the square a knight reaches from sq with move m, or 64 when it leaves the board
58 fn target sq ulong m ulong to ulong
59 f = add long rem sq 8 get mdx m
60 r = add long div sq 8 get mdy m
61 if or or lt f 0 gt f 7 or lt r 0 gt r 7
62 ret 64
63 end
64 ret ulong add mul r 8 f
65 end
66 note degree counts the unvisited squares reachable from sq
67 fn degree sq ulong to ulong
68 d = ulong 0
69 for m ulong 8
70 t = target sq m
71 if lt t 64
72 if eq get board t 0
73 d = add d 1
74 end
75 end
76 end
77 ret d
78 end
79 note Warnsdorff's rule: always move to the unvisited square with the fewest onward moves; ties go to
80 note the first in the move order (2,1) (1,2) (-1,2) (-2,1) (-2,-1) (-1,-2) (1,-2) (2,-1)
81 fn tour to bool
82 sq = ulong 0
83 put board 0 1
84 for k ulong 63
85 b = ulong 64
86 bd = ulong 9
87 for m ulong 8
88 t = target sq m
89 if lt t 64
90 if eq get board t 0
91 d = degree t
92 if lt d bd
93 b = t
94 bd = d
95 end
96 end
97 end
98 end
99 if eq b 64
100 ret false
101 end
102 put board b add k 2
103 sq = b
104 end
105 ret true
106 end
107 fn main to int
108 put mdx 0 2
109 put mdy 0 1
110 put mdx 1 1
111 put mdy 1 2
112 put mdx 2 -1
113 put mdy 2 2
114 put mdx 3 -2
115 put mdy 3 1
116 put mdx 4 -2
117 put mdy 4 -1
118 put mdx 5 -1
119 put mdy 5 -2
120 put mdx 6 1
121 put mdy 6 -2
122 put mdx 7 2
123 put mdy 7 -1
124 if not tour
125 exit 1
126 end
127 emit tstart
128 for x ulong 8
129 r = sub 7 x
130 for f ulong 8
131 emitw get board add mul r 8 f 3
132 end
133 emit nl
134 end
135 ret 0
136 end
137
138 {{out}}
139
140 Warnsdorff tour from a1; each square shows its move number, rank 8 at the top
141 16 31 12 51 26 29 10 63
142 13 50 15 30 11 64 25 28
143 32 17 52 47 54 27 62 9
144 49 14 55 36 61 38 45 24
145 18 33 48 53 46 59 8 39
146 3 56 35 60 37 42 23 44
147 34 19 2 5 58 21 40 7
148 1 4 57 20 41 6 43 22
149