Differences From Artifact [62e6040714438eff]:
- File        
src/output.d
- 2012-07-14 14:41:22 - part of checkin [95915ac93f] on branch trunk - n*m limitter. (user: kinaba) [annotate]
 
To Artifact [ef5cfcc2ac51c863]:
- File        
src/output.d
- 2012-07-15 04:44:33 - part of checkin [879099f815] on branch trunk - Moved 'abort' behavior completely out of Game. (user: kinaba) [annotate]
 
   20  }                                                                                     20  }
   21                                                                                        21  
   22  class GuardedOutput : GameObserver                                                    22  class GuardedOutput : GameObserver
   23  {                                                                                     23  {
   24          this(in Game g)                                                               24          this(in Game g)
   25          {                                                                             25          {
   26                  setup_sigint_handling();                                              26                  setup_sigint_handling();
   27                  ideal_log ~= g.score_if_abort_now;                               |    27                  score_log ~= g.score;
   28                  flushed = false;                                                      28                  flushed = false;
   29          }                                                                             29          }
   30                                                                                        30  
   31          override void on_game_changed(char c, in Game g, bool finished)               31          override void on_game_changed(char c, in Game g, bool finished)
   32          {                                                                             32          {
   33                  if(flushed)                                                           33                  if(flushed)
   34                          return;                                                       34                          return;
   35                                                                                        35  
   36                  log ~= c;                                                             36                  log ~= c;
   37                  score_log ~= g.score;                                                 37                  score_log ~= g.score;
   38                  ideal_log ~= g.score_if_abort_now;                               <
   39                  if(finished || log.length+1==g.map.W*g.map.H)                         38                  if(finished || log.length+1==g.map.W*g.map.H)
   40                          flush();                                                      39                          flush();
   41          }                                                                             40          }
   42                                                                                        41  
   43  private:                                                                              42  private:
   44          string log;                                                                   43          string log;
   45          long[] score_log;                                                             44          long[] score_log;
   46          long[] ideal_log;                                                        <
   47          bool   flushed;                                                               45          bool   flushed;
   48                                                                                        46  
   49          void flush()                                                                  47          void flush()
   50          {                                                                             48          {
   51                  Tuple!(long, int, int) cand;                                     |    49                  Tuple!(long, int) cand;
   52                  cand[0] = long.min;                                                   50                  cand[0] = long.min;
   53                                                                                        51  
   54                  for(int i=0; i<score_log.length; ++i)                                 52                  for(int i=0; i<score_log.length; ++i)
   55                          if(cand[0] < score_log[i])                                    53                          if(cand[0] < score_log[i])
   56                                  cand = tuple(score_log[i],i,0);                  |    54                                  cand = tuple(score_log[i],i);
   57                  for(int i=0; i<ideal_log.length; ++i)                            <
   58                          if(cand[0] < ideal_log[i])                               <
   59                                  cand = tuple(ideal_log[i],i,1);                  <
   60                                                                                        55  
   61                  if(cand[2]==0) {                                                 <
   62                          string str = log[0..cand[1]+1];                          <
   63                          std.c.stdio.printf("%.*s\n", str.length, str.ptr);       <
   64                  } else {                                                         <
   65                          string str = log[0..cand[1]];                            <
   66                          std.c.stdio.printf("%.*sA\n", str.length, str.ptr);      |    56                  std.c.stdio.printf("%.*sA\n", cand[1], log.ptr);
   67                  }                                                                <
   68                  std.c.stdio.fflush(std.c.stdio.stdout);                               57                  std.c.stdio.fflush(std.c.stdio.stdout);
   69                  flushed = true;                                                       58                  flushed = true;
   70          }                                                                             59          }
   71                                                                                        60  
   72  private:                                                                              61  private:
   73          static __gshared GuardedOutput g_output;                                      62          static __gshared GuardedOutput g_output;
   74                                                                                        63