Changeset 22 for trunk


Ignore:
Timestamp:
Dec 19, 2005, 9:24:02 AM (14 years ago)
Author:
jtv
Message:

Saved-game size estimation

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/c_abi.cxx

    r5 r22  
    5454{
    5555  delete castback(f);
     56}
     57
     58
     59int mines_savesize(const Minefield *f)
     60{
     61  return castback(f)->savesize();
    5662}
    5763
  • trunk/c_abi.h

    r16 r22  
    3838Minefield *mines_load(const char buffer[]);
    3939
    40 /** @brief Save minefield to buffer (at least rows*cols+100 bytes large).
     40/** @brief Maximum number of bytes of storage required to save game
     41 */
     42int mines_savesize(const Minefield *);
     43
     44/** @brief Save minefield to buffer (at least mines_savesize() large)
    4145 * @return Number of bytes of buffer space used (not including terminating zero)
    4246 */
  • trunk/gamelogic.cxx

    r21 r22  
    192192 * the "superset" one that doesn't border on the "subset" patch is either all
    193193 * clear or all mined, respectively.
     194 *
     195 * This logic comes into play at intelligence level 3.
    194196 */
    195197class SuperSet
     
    331333{
    332334  delete [] m_patches;
     335}
     336
     337
     338int Lake::savesize() const throw ()
     339{
     340  return m_rows * ((m_cols+patchesperchar-1)/patchesperchar+3) + 100;
    333341}
    334342
  • trunk/gamelogic.hxx

    r15 r22  
    100100  const int cols() const throw () { return m_cols; }
    101101
     102  /// Maximum number of bytes required to save this game
     103  int savesize() const throw ();
     104
    102105  /// Write game state (in ASCII) to memory buffer
    103106  /** The available buffer space must be at least rows*cols+100 bytes large.
  • trunk/ui_cli.cxx

    r14 r22  
    4848void save_game(const Lake &L)
    4949{
    50   char *const buf = new char[L.rows()*L.cols()+100];
     50  assert(L.savesize() == L.savesize());
     51  char *const buf = new char[L.savesize()];
    5152  int fd = -1;
    5253  try
    5354  {
    5455    int bytes = L.save(buf);
     56    assert(bytes < L.savesize());
    5557    fd = creat("mines.savedgame", 0744);
    5658    if (fd == -1) throw runtime_error(strerror(errno));
     
    6062    // See if we can restore this game, save that, and get the same data back
    6163    const Lake L2(buf);
    62     char *const check = new char[L.rows()*L.cols()+100];
    63     bytes = L2.save(check);
     64    char *const check = new char[L2.savesize()];
     65    const int bytes2 = L2.save(check);
     66    assert(bytes2 == bytes);
    6467    assert(strcmp(buf,check) == 0);
    6568    delete [] check;
  • trunk/ui_web.c

    r20 r22  
    2222  "</head>"
    2323  "<body>\n",
     24toolarge[] = "<p><em>Playing field too large.  Try something smaller!</em></p>",
    2425youwin[] =
    2526  "<h1>You win.  Congratulations!</h1>",
     
    123124  if (id[0])
    124125  {
    125     char saved[maxsize+100+1];
     126    char saved[maxsize+1000];
    126127    int fd = -1;
    127128    ssize_t bytes = 0, bytesread = 0;
     
    156157    cols = mines_cols(F);
    157158  }
    158   else if (rows && cols && mines && rows*cols<=maxsize)
     159  else if (rows && cols && mines)
    159160  {
    160161    size_t idbytes = 0;
     162
     163    if (rows*cols > maxsize)
     164    {
     165      puts(toolarge);
     166      puts(footer);
     167      exit(0);
     168    }
    161169
    162170    /* We have parameters.  Create new game. */
     
    170178    F = mines_init(rows,cols,mines);
    171179    if (!F) exit(1);
     180    if (mines_savesize(F) > maxsize)
     181    {
     182      puts(toolarge);
     183      mines_close(F);
     184      F = NULL;
     185    }
    172186    mines_set_intelligence(F, intelligence);
    173187  }
     
    183197    int done=0;
    184198    const char *scriptname = getenv("SCRIPT_NAME");
    185     char saved[maxsize+100+1];
     199    char saved[maxsize+100];
    186200    int fd;
    187201    size_t bytes;
Note: See TracChangeset for help on using the changeset viewer.