996
|
1 // Rail.cpp
|
|
2
|
|
3 #include "lib.h"
|
|
4 #include "Rail.h"
|
|
5
|
|
6 using namespace std;
|
|
7
|
|
8 namespace Rail
|
|
9 {
|
|
10 struct PrimaryTable
|
|
11 {
|
|
12 PrimaryTable()
|
|
13 {
|
|
14 for (int i = min; i < count; ++i)
|
|
15 {
|
|
16 for (int j = min; j < count; ++j)
|
|
17 {
|
|
18 data[i][j] = false;
|
|
19 }
|
|
20 }
|
|
21
|
|
22 data[horizontal][horizontal] = true;
|
|
23 data[horizontal][ascending] = true;
|
|
24 data[horizontal][descending] = true;
|
|
25 data[horizontal][junction] = true;
|
|
26
|
|
27 data[vertical][vertical] = true;
|
|
28 data[vertical][ascending] = true;
|
|
29 data[vertical][descending] = true;
|
|
30 data[vertical][junction] = true;
|
|
31
|
|
32 data[ascending][horizontal] = true;
|
|
33 data[ascending][vertical] = true;
|
|
34 data[ascending][ascending] = true;
|
|
35 data[ascending][junction] = true;
|
|
36
|
|
37 data[descending][horizontal] = true;
|
|
38 data[descending][vertical] = true;
|
|
39 data[descending][descending] = true;
|
|
40 data[descending][junction] = true;
|
|
41 }
|
|
42
|
|
43 bool data[count][count];
|
|
44 };
|
|
45
|
|
46 bool isPrimary(t source, t dest, Dir::t current)
|
|
47 {
|
|
48 static PrimaryTable result;
|
|
49 check(source, "Rail::isPrimary()");
|
|
50 check(dest, "Rail::isPrimary()");
|
|
51 Dir::check(current, "Rail::isPrimary()");
|
|
52
|
|
53 if (source == junction)
|
|
54 {
|
|
55 source = dirToRail(current);
|
|
56 }
|
|
57 // cerr << "Primary: " << railToString(source) << " -> "
|
|
58 // << railToString(dest) << endl;
|
|
59 return result.data[source][dest];
|
|
60 }
|
|
61
|
|
62 struct LeftTable
|
|
63 {
|
|
64 LeftTable()
|
|
65 {
|
|
66 for (int i = min; i < count; ++i)
|
|
67 {
|
|
68 for (int j = min; j < count; ++j)
|
|
69 {
|
|
70 data[i][j] = false;
|
|
71 }
|
|
72 }
|
|
73
|
|
74 data[horizontal][ascending] = true;
|
|
75 data[ascending][vertical] = true;
|
|
76 data[vertical][descending] = true;
|
|
77 data[descending][horizontal] = true;
|
|
78 }
|
|
79
|
|
80 bool data[count][count];
|
|
81 };
|
|
82
|
|
83 bool isLeft(t source, t dest)
|
|
84 {
|
|
85 static LeftTable result;
|
|
86 check(source, "Rail::isLeft()");
|
|
87 check(dest, "Rail::isLeft()");
|
|
88 // cerr << "Left: " << railToString(source) << " -> "
|
|
89 // << railToString(dest) << endl;
|
|
90 return result.data[source][dest];
|
|
91 }
|
|
92
|
|
93 struct RightTable
|
|
94 {
|
|
95 RightTable()
|
|
96 {
|
|
97 for (int i = min; i < count; ++i)
|
|
98 {
|
|
99 for (int j = min; j < count; ++j)
|
|
100 {
|
|
101 data[i][j] = false;
|
|
102 }
|
|
103 }
|
|
104
|
|
105 data[horizontal][descending] = true;
|
|
106 data[descending][vertical] = true;
|
|
107 data[vertical][ascending] = true;
|
|
108 data[ascending][horizontal] = true;
|
|
109 }
|
|
110
|
|
111 bool data[count][count];
|
|
112 };
|
|
113
|
|
114 bool isRight(t source, t dest)
|
|
115 {
|
|
116 static RightTable result;
|
|
117 check(source, "Rail::isRight()");
|
|
118 check(dest, "Rail::isRight()");
|
|
119 // cerr << "Right: " << railToString(source) << " -> "
|
|
120 // << railToString(dest) << endl;
|
|
121 return result.data[source][dest];
|
|
122 }
|
|
123
|
|
124 Dir::t correctDirection(t current, Dir::t oldDir)
|
|
125 {
|
|
126 static const string functionName = "Rail::correctDirection()";
|
|
127 check(current, functionName);
|
|
128 Dir::check(oldDir, functionName);
|
|
129 Dir::t result = oldDir;
|
|
130 if (current == junction)
|
|
131 {
|
|
132 result = oldDir;
|
|
133 }
|
|
134 else if (current == rubble)
|
|
135 {
|
|
136 throw InternalException(functionName + ": Encountered rubble");
|
|
137 }
|
|
138 else
|
|
139 {
|
|
140 Dir::t forward = railToDirForward(current);
|
|
141 Dir::t backward = railToDirBackward(current);
|
|
142 if (oldDir == forward || oldDir == Dir::left(forward)
|
|
143 || oldDir == Dir::right(forward))
|
|
144 {
|
|
145 result = forward;
|
|
146 }
|
|
147 else if (oldDir == backward || oldDir == Dir::left(backward)
|
|
148 || oldDir == Dir::right(backward))
|
|
149 {
|
|
150 result = backward;
|
|
151 }
|
|
152 else
|
|
153 {
|
|
154 throw InternalException(functionName + ": Perpendicular direction: "
|
|
155 + railToString(current) + " -- "
|
|
156 + Dir::dirToString(oldDir));
|
|
157 }
|
|
158 }
|
|
159 return result;
|
|
160 }
|
|
161
|
|
162 struct CharTable
|
|
163 {
|
|
164 CharTable()
|
|
165 {
|
|
166 for (int i = 0; i < 256; ++i)
|
|
167 {
|
|
168 data[i] = rubble;
|
|
169 }
|
|
170
|
|
171 data['-'] = horizontal;
|
|
172 data['|'] = vertical;
|
|
173 data['/'] = ascending;
|
|
174 data['\\'] = descending;
|
|
175
|
|
176 data['*'] = junction;
|
|
177 data['+'] = junction;
|
|
178 data['x'] = junction;
|
|
179 data['v'] = junction;
|
|
180 data['^'] = junction;
|
|
181 data['<'] = junction;
|
|
182 data['>'] = junction;
|
|
183
|
|
184 data['b'] = junction;
|
|
185 data['e'] = junction;
|
|
186 data['i'] = junction;
|
|
187 data['o'] = junction;
|
|
188 data['u'] = junction;
|
|
189 data['?'] = junction;
|
|
190
|
|
191 data['['] = junction;
|
|
192 data[']'] = junction;
|
|
193 data['('] = junction;
|
|
194 data[')'] = junction;
|
|
195 data['{'] = junction;
|
|
196 data['}'] = junction;
|
|
197
|
|
198 data['a'] = junction;
|
|
199 data['d'] = junction;
|
|
200 data['m'] = junction;
|
|
201 data['r'] = junction;
|
|
202 data['s'] = junction;
|
|
203 data['0'] = junction;
|
|
204 data['1'] = junction;
|
|
205 data['2'] = junction;
|
|
206 data['3'] = junction;
|
|
207 data['4'] = junction;
|
|
208 data['5'] = junction;
|
|
209 data['6'] = junction;
|
|
210 data['7'] = junction;
|
|
211 data['8'] = junction;
|
|
212 data['9'] = junction;
|
|
213 data['0'] = junction;
|
|
214
|
|
215 data['c'] = junction;
|
|
216 data['p'] = junction;
|
|
217 data['z'] = junction;
|
|
218
|
|
219 data['n'] = junction;
|
|
220 data[':'] = junction;
|
|
221 data['~'] = junction;
|
|
222
|
|
223 data['f'] = junction;
|
|
224 data['g'] = junction;
|
|
225 data['q'] = junction;
|
|
226 data['t'] = junction;
|
|
227
|
|
228 data['@'] = junction;
|
|
229 data['#'] = junction;
|
|
230 data['&'] = junction;
|
|
231 data['$'] = junction;
|
|
232 }
|
|
233
|
|
234 t data[256];
|
|
235 };
|
|
236
|
|
237 t charToRail(char current)
|
|
238 {
|
|
239 static CharTable result;
|
|
240 return result.data[static_cast<unsigned char>(current)];
|
|
241 }
|
|
242
|
|
243 t dirToRail(Dir::t current)
|
|
244 {
|
|
245 static t result[] = {vertical, ascending, horizontal, descending,
|
|
246 vertical, ascending, horizontal, descending};
|
|
247 Dir::check(current, "Rail::dirToRail()");
|
|
248 return result[current];
|
|
249 }
|
|
250
|
|
251 Dir::t railToDirForward(t current)
|
|
252 {
|
|
253 static Dir::t result[] = {Dir::east, Dir::north, Dir::northeast,
|
|
254 Dir::southeast};
|
|
255 check(current, "Rail::railToDirForward()");
|
|
256 if (current == junction || current == rubble)
|
|
257 {
|
|
258 throw InternalException(
|
|
259 "Rail::railToDirForward(): No inherent direction: "
|
|
260 + railToString(current));
|
|
261 }
|
|
262 return result[current];
|
|
263 }
|
|
264
|
|
265 Dir::t railToDirBackward(t current)
|
|
266 {
|
|
267 return Dir::back(railToDirForward(current));
|
|
268 }
|
|
269
|
|
270 string railToString(t current)
|
|
271 {
|
|
272 static string result[] = {"horizontal", "vertical", "ascending",
|
|
273 "descending", "junction", "rubble"};
|
|
274 if (current >= min && current <= max)
|
|
275 {
|
|
276 return result[current];
|
|
277 }
|
|
278 else
|
|
279 {
|
|
280 return intToString(current);
|
|
281 }
|
|
282 }
|
|
283
|
|
284 void check(t current, string const & name)
|
|
285 {
|
|
286 if (current < min || current > max)
|
|
287 {
|
|
288 throw InternalException(name + ": Invalid Rail: "
|
|
289 + railToString(current));
|
|
290 }
|
|
291 }
|
|
292 }
|