Differences From Artifact [ede3d182d99f2de5]:
- File
src/solver.d
- 2012-07-15 04:44:33 - part of checkin [879099f815] on branch trunk - Moved 'abort' behavior completely out of Game. (user: kinaba) [annotate]
To Artifact [dca9b6f10b63ba61]:
- File
src/solver.d
- 2012-07-15 05:56:41 - part of checkin [45b72fc54e] on branch trunk - Last 10 step optimization. (user: kinaba) [annotate]
293 293 }
294 294 return [];
295 295 }
296 296 return (danger_ok ? [] : tryA()) ~ tryB() ~ tryC();
297 297 }
298 298 }
299 299
300 -alias Solver_1 MainSolver;
300 +class Solver_2(Solver)
301 +{
302 + string plan;
303 +
304 + Game g;
305 + this(in Game g)
306 + {
307 + this.g = g.clone();
308 + make_plan(g);
309 + }
310 +
311 + Tuple!(Solver,string) run_sub_solver(in Game g)
312 + {
313 + string log;
314 + auto s = new Solver(g);
315 + while(!g.cleared && !g.dead && plan.length<=g.map.H*g.map.W) {
316 + char c = s.single_step();
317 + if( c == 'A' )
318 + break;
319 + log ~= c;
320 + }
321 + while(log.length>0 && log[$-1]=='W')
322 + log.length--;
323 + return tuple(s, log);
324 + }
325 +
326 + void make_plan(in Game g) {
327 + Tuple!(Solver,string) x = run_sub_solver(g);
328 + plan = x[1];
329 + if(x[0].g.cleared)
330 + return;
331 + modify_plan(g, x[0].g.score);
332 + }
333 +
334 + void modify_plan(in Game ini, long unmod)
335 + {
336 + int bp = max(0, (cast(int)plan.length)-10);
337 + Game g = ini.clone();
338 + for(int i=0; i<bp; ++i) g.command(plan[i]);
339 +
340 + Tuple!(string,long) cand = tuple(plan, unmod);
341 + for(int i=bp; i<plan.length; ++i) {
342 + foreach(char c; "UDLR")
343 + if(c != plan[i]) {
344 + Tuple!(string,long) zz = try_plan(c, g);
345 + if(cand[1]<zz[1])
346 + cand = tuple(plan[0..i]~c~zz[0], zz[1]);
347 + }
348 + g.command(plan[i]);
349 + }
350 + plan = cand[0];
351 + }
352 +
353 + Tuple!(string,long) try_plan(char c, in Game g)
354 + {
355 + Game gg = g.clone();
356 + gg.command(c);
357 + Tuple!(Solver, string) x = run_sub_solver(gg);
358 + return tuple(x[1], x[0].g.score);
359 + }
360 +
361 + char single_step() {
362 + if(plan.empty)
363 + return 'A';
364 + char c = plan[0];
365 + plan = plan[1..$];
366 + g.command(c);
367 + return c;
368 + }
369 +
370 + void force(char c) {
371 + g.command(c);
372 + make_plan(g);
373 + }
374 +}
375 +
376 +alias Solver_2!(Solver_1) MainSolver;