home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / games / tictacai.zip / DUMPMEM.C next >
C/C++ Source or Header  |  1990-06-17  |  6KB  |  220 lines

  1.  
  2. /*
  3.  * DUMPMEM.C
  4.  *
  5.  * Copyright (C) 1990 by Aaron L. Brenner
  6.  *
  7.  * Purpose:
  8.  *        Produce a formatted dump of TICTAC.MEM.
  9.  *
  10.  * Revision history:
  11.  * 1.00        06/17/90    ALB        Created.
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include <malloc.h>
  16.  
  17.  
  18. #define    mem_err(x)        err_exit("memory allocation", x)
  19. #define    eof_err(x)        err_exit("unexpected EOF", x)
  20.  
  21.  
  22. char    memory_name[] = "tictac.mem";        /* File name for "memory"        */
  23.  
  24.  
  25. /*
  26.  * Define the structure of each recorded move
  27.  */
  28. typedef    struct    playlist {
  29.     unsigned    char    pl_move;            /* Move this corresponds to        */
  30.                 char    pl_weight;            /* "Weight" for this move        */
  31.     struct    playlist    *pl_next;            /* Next one in the list            */
  32. } PLAYLIST;
  33.  
  34.  
  35. /*
  36.  * Define the structure of each "remembered" board
  37.  */
  38. typedef    struct    boardlist {
  39.     unsigned    int        bl_board;            /* Encoded board                */
  40.     unsigned    char    bl_count;            /* Count of play list elements    */
  41.     PLAYLIST            *bl_plays;            /* List of plays made here        */
  42.     struct    boardlist    *bl_next;            /* Next board in the list        */
  43. } BOARDLIST;
  44.  
  45.  
  46. BOARDLIST    *board_memory = NULL;            /* Head of list of boards        */
  47.  
  48. unsigned    powers[9] = {
  49.     3, 9, 27, 81, 243, 729, 2187, 6561, 19683
  50. };
  51.  
  52.  
  53. int        board_count;
  54.  
  55.  
  56. /*
  57.  * Error exit routine
  58.  */
  59. void    err_exit(char *msg, char *locus)
  60. {
  61.     fputs("Fatal ", stderr);                /* Tell them it's a fatal error    */
  62.     fputs(msg, stderr);                        /* Spit out the message            */
  63.     fputs(" error in/about ", stderr);        /* Say about where it is        */
  64.     fputs(locus, stderr);
  65.     fputc('\n', stderr);                    /* New line                        */
  66.     exit(1);                                /* Get the hell out of Dodge    */
  67. }
  68.  
  69.  
  70. /*
  71.  * Load the memory file
  72.  *
  73.  * The memory file is organized as follows:
  74.  *        word        count of boards
  75.  *        <boards>
  76.  *        <move/weight pairs>
  77.  * The count of pairs for each board is stored with the board.
  78.  */
  79. void    load_boards()
  80. {
  81.     FILE        *memfile;
  82.     unsigned    bcount,
  83.                 pcount,
  84.                 count2;
  85.     BOARDLIST    *tboards,                    /* Ptr for allocating            */
  86.                 *tbdtail;                    /* Keeps track of the end of    */
  87.                                             /*  the board list so we don't    */
  88.                                             /*  have to chain through the    */
  89.                                             /*  entire list each time        */
  90.     PLAYLIST    *tplays,                    /* Ptr for allocating            */
  91.                 *tplaystail;                /* Same purpose as tbdtail, but    */
  92.                                             /*  for the move list            */
  93.  
  94.     /*
  95.      * Try to open the memory file. If it fails, no big deal, just no memory
  96.      */
  97.     if ((memfile = fopen(memory_name, "rb")) != NULL) {
  98.  
  99.         /*
  100.          * Make the file buffer bigger than 1 sector so that the I/O goes
  101.          * faster
  102.          */
  103.         setvbuf(memfile, NULL, _IOFBF, 8192);
  104.  
  105.         /*
  106.          * Read in the count of boards. If that fails, die.
  107.          */
  108.         if ((board_count = (unsigned)getw(memfile)) == EOF)
  109.             eof_err("load_boards[board count]");
  110.         tbdtail = NULL;                        /* Set up the tail ptr            */
  111.  
  112.         /*
  113.          * Load in each board in the file
  114.          */
  115.         for (count2 = 0; count2 < board_count; count2++) {
  116.  
  117.             /*
  118.              * Allocate a structure for this board. If it fails, die.
  119.              */
  120.             if ((tboards = malloc(sizeof(BOARDLIST))) == NULL)
  121.                 mem_err("load_boards[board list]");
  122.             if (tbdtail == NULL)            /* If none yet,                    */
  123.                 board_memory = tboards;        /* Set the list head            */
  124.             else                            /* But if we already have some,    */
  125.                 tbdtail->bl_next = tboards;    /*  set our tail ptr's ptr        */
  126.             tbdtail = tboards;                /* Point to the last one        */
  127.             tboards->bl_plays = NULL;        /* Set up ptr to play list        */
  128.             tboards->bl_next = NULL;        /* Keep list terminated            */
  129.             /*
  130.              * Read in the board itself. If it fails, die.
  131.              */
  132.             if (fread(&(tboards->bl_board), 1, sizeof(tboards->bl_board) +
  133.                         sizeof(char), memfile) != (sizeof(tboards->bl_board) +
  134.                                                             sizeof(char)))
  135.                 eof_err("load_boards[board data]");
  136.         }
  137.  
  138.         /*
  139.          * We've read in all the board configurations. Now, we have to go
  140.          * through each board, reading in the plays for that board.
  141.          */
  142.         for (tboards = board_memory; tboards != NULL;
  143.                                                 tboards = tboards->bl_next) {
  144.             tplaystail = NULL;                /* Set up list tail ptr            */
  145.             pcount = tboards->bl_count;        /* Get play list count            */
  146.             for (count2 = 0; count2 < pcount; count2++) {
  147.                 if ((tplays = malloc(sizeof(PLAYLIST))) == NULL)
  148.                     mem_err("load_boards[play list]");
  149.                 if (tplaystail == NULL)        /* If none yet,                    */
  150.                     tboards->bl_plays = tplays;    /* Set ptr in board entry    */
  151.                 else                        /* Otherwise keep list right    */
  152.                     tplaystail->pl_next = tplays;
  153.                 tplaystail = tplays;
  154.                 tplays->pl_next = NULL;        /* Keep list terminated            */
  155.  
  156.                 /*
  157.                  * Read in a move/weight pair. If it fails, die.
  158.                  */
  159.                 if (fread(&(tplays->pl_move), 1, sizeof(unsigned char) +
  160.                             sizeof(char), memfile) < (sizeof(unsigned char) +
  161.                                                                 sizeof(char)))
  162.                     eof_err("load_boards[play list]");
  163.             }
  164.         }
  165.         fclose(memfile);                    /* Close it                        */
  166.     }
  167. }
  168.  
  169.  
  170. /*
  171.  * Dump the memory file.
  172.  */
  173. void    dump_memory()
  174. {
  175.     FILE        *mfile;
  176.     BOARDLIST    *tb;
  177.     PLAYLIST    *tp;
  178.     int            i,
  179.                 j;
  180.     unsigned    coded_board;
  181.     char        t2[9][6];
  182.     unsigned    char    dboard[9];
  183.  
  184.     if ((mfile = fopen("tmdump.txt", "w")) == NULL)
  185.         err_exit("file creation", "dump_memory[create dump file]");
  186.  
  187.     setvbuf(mfile, NULL, _IOFBF, 8192);
  188.     fputs("Dump of TICTAC.MEM\n==================\n\n", mfile);
  189.     fprintf(mfile, "Contains %d boards\n\n", board_count);
  190.     for (i = 1, tb = board_memory; tb != NULL; i++, tb = tb->bl_next) {
  191.         coded_board = tb->bl_board;
  192.         for (j = 8; j >= 0; j--) {
  193.             dboard[j] = (coded_board / powers[j]);
  194.             coded_board %= powers[j];
  195.         }
  196.         for (j = 0; j < 9; j++)
  197.             if (dboard[j] == 1)
  198.                 strcpy(t2[j], "**X**");
  199.             else if (dboard[j] == 2)
  200.                 strcpy(t2[j], "**O**");
  201.             else
  202.                 t2[j][0] = '\0';
  203.         for (tp = tb->bl_plays; tp != NULL; tp = tp->pl_next)
  204.             sprintf(t2[tp->pl_move], "%5d", tp->pl_weight);
  205.         fprintf(mfile, "Board %-8d%5s|%5s|%s\n%31s\n",
  206.                                 i, t2[6], t2[7], t2[8], "-----+-----+-----");
  207.         fprintf(mfile, "%14s%5s|%5s|%s\n%31s\n",
  208.                                 "", t2[3], t2[4], t2[5], "-----+-----+-----");
  209.         fprintf(mfile, "%14s%5s|%5s|%s\n\n", "", t2[0], t2[1], t2[2]);
  210.     }
  211.     fclose(mfile);
  212. }
  213.  
  214.  
  215. main()
  216. {
  217.     load_boards();                            /* Load in the memory file        */
  218.     dump_memory();                            /* Dump it                        */
  219. }
  220.