Diff
Not logged in

Differences From Artifact [29c4bbd4331091b4]:

To Artifact [1b458d7cd25e92dc]:


2 2 import game; 3 3 import core.stdc.signal; 4 4 import std.c.stdlib; 5 5 6 6 abstract class Output 7 7 { 8 8 void command(char c); 9 + void flush(); 9 10 } 10 11 11 12 class NilOutput : Output 12 13 { 13 14 override void command(char c) {} 15 + override void flush() {} 14 16 } 15 17 16 18 class StdOutput : Output 17 19 { 20 + override void command(char c) 21 + { 22 + write(c); 23 + stdout.flush(); 24 + } 25 + override void flush() {} 26 +} 27 + 28 +// TODO: clean it up. 29 +__gshared Output g_output; 30 + 31 +class GuardedOutput : StdOutput 32 +{ 18 33 // Handle SIGINT: force abort and exit. 19 34 static this() 20 35 { 21 36 signal(SIGINT, &sigint); 22 37 } 23 - extern(C) static void sigint(int) { 24 - write("A"); 25 - stdout.flush(); 38 + 39 + extern(C) static void sigint(int) 40 + { 41 + if(g_output !is null) 42 + g_output.flush(); 43 + else { 44 + write("A"); 45 + stdout.flush(); 46 + } 26 47 exit(0); 27 48 } 28 49 50 + Game g; 51 + this(Game ini) { this.g = ini.clone(); ideal_log ~= g.score_if_abort_now; g_output = this; } 52 + 53 + string log; 54 + long[] score_log; 55 + long[] ideal_log; 56 + 29 57 override void command(char c) 30 58 { 31 - // TODO: optimize redundancy. 32 - write(c); 59 + g.command(c); 60 + log ~= c; 61 + score_log ~= g.score; 62 + ideal_log ~= g.score_if_abort_now; 63 + } 64 + override void flush() 65 + { 66 + Tuple!(long, int, int) cand; 67 + cand[0] = long.min; 68 + foreach(int i, long s; score_log) 69 + if(cand[0] < s) 70 + cand = tuple(s,i,0); 71 + foreach(int i, long s; ideal_log) 72 + if(cand[0] < s) 73 + cand = tuple(s,i,1); 74 + if(cand[2]==0) 75 + writeln(log[0..cand[1]+1]); 76 + else 77 + writeln(log[0..cand[1]]~"A"); 33 78 stdout.flush(); 34 79 } 35 80 }