Differences From Artifact [29c4bbd4331091b4]:
- File
output.d
- 2012-07-14 07:47:55 - part of checkin [b6daa1efa1] on branch trunk - Modularized version. (user: kinaba) [annotate]
To Artifact [1b458d7cd25e92dc]:
- File
output.d
- 2012-07-14 08:31:35 - part of checkin [a5e6c99b3d] on branch trunk - Guarded. (user: kinaba) [annotate]
- File
src/output.d
- 2012-07-14 09:16:47 - part of checkin [6293256fec] on branch trunk - Preparing for submission... (user: kinaba) [annotate]
2 import game; 2 import game;
3 import core.stdc.signal; 3 import core.stdc.signal;
4 import std.c.stdlib; 4 import std.c.stdlib;
5 5
6 abstract class Output 6 abstract class Output
7 { 7 {
8 void command(char c); 8 void command(char c);
> 9 void flush();
9 } 10 }
10 11
11 class NilOutput : Output 12 class NilOutput : Output
12 { 13 {
13 override void command(char c) {} 14 override void command(char c) {}
> 15 override void flush() {}
14 } 16 }
15 17
16 class StdOutput : Output 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 // Handle SIGINT: force abort and exit. 33 // Handle SIGINT: force abort and exit.
19 static this() 34 static this()
20 { 35 {
21 signal(SIGINT, &sigint); 36 signal(SIGINT, &sigint);
22 } 37 }
> 38
23 extern(C) static void sigint(int) { | 39 extern(C) static void sigint(int)
> 40 {
> 41 if(g_output !is null)
> 42 g_output.flush();
> 43 else {
24 write("A"); | 44 write("A");
25 stdout.flush(); | 45 stdout.flush();
> 46 }
26 exit(0); 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
> 52
> 53 string log;
> 54 long[] score_log;
> 55 long[] ideal_log;
> 56
29 override void command(char c) 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 stdout.flush(); 78 stdout.flush();
34 } 79 }
35 } 80 }