home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / GRAPH.ZIP / AI / DEMOS / DEMO3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-12  |  3.8 KB  |  229 lines

  1. #include "demo3.h"
  2.  
  3. PUZZLE_::PUZZLE_(PNODE_ *start, PNODE_ *target)
  4.     :DEPTH_GRAPH_(start, target, 4)
  5. {
  6. }
  7.  
  8.  
  9. /*                 PNODE
  10.  
  11.     Initializes a PNODE_ object.
  12.  
  13. */
  14.  
  15. PNODE_::PNODE_(const char *b, int empty_x, int empty_y)
  16. {
  17.     char
  18.         *p = *board;
  19.     int
  20.         i;
  21.  
  22.     for (i = 0; i <= 8; i++)
  23.         *(p + i) = *(b + i);
  24.  
  25.     x = empty_x;
  26.     y = empty_y;
  27. }
  28.  
  29.  
  30.  
  31. /*                       PNODE_
  32.  
  33.     Initializes a new configuration using it's 'parent' configuration.
  34.     First copies the old board and then swaps the two tiles that are
  35.     on old_x, old_y and new_x, new_t respectively.
  36.  
  37. */
  38.  
  39. PNODE_::PNODE_(const char *b, int old_x, int old_y, int new_x, int new_y)
  40. {
  41.     char
  42.         *p = *board;
  43.     int
  44.         i;
  45.  
  46.     for (i = 0; i <= 8; i++)
  47.         *(p + i) = *(b + i); 
  48.  
  49.     board[old_x][old_y] = board[new_x][new_y];
  50.     board[new_x][new_y] = 0;
  51.  
  52.     x = new_x;
  53.     y = new_y;
  54. }
  55.  
  56.  
  57.  
  58. /*                    DISPLAY
  59.  
  60.     Displays a board configuration.
  61.  
  62. */
  63.  
  64. void PNODE_::display() const
  65. {
  66.     int
  67.         row,
  68.         col;
  69.  
  70.     for (row = 0; row < 3; row++)
  71.     {
  72.         for (col = 0; col < 3; col++)
  73.             printf("%d ", board[row][col]);
  74.         putchar('\n');
  75.     }
  76.     putchar('\n');
  77. }
  78.  
  79.  
  80.  
  81. /*                    EQUAL
  82.  
  83.     Determines if two nodes, i.e., two board positions are the same.
  84.     First, the x- and y-coordinates of the empty tile are compared
  85.     and next, if necessary, the two boards themselves.
  86.  
  87. */
  88.  
  89. int PNODE_::equal(const VOBJECT_ &other) const
  90. {
  91.      if (x != ((const PNODE_ &)other).get_x()
  92.           && y != ((const PNODE_ &)other).get_y())
  93.             return(0);
  94.     return(compare_board(((const PNODE_ &)other).get_board()));
  95. }
  96.  
  97.  
  98.  
  99. const char *PNODE_::get_board() const
  100. {
  101.     return(*board);
  102. }
  103.  
  104.  
  105.  
  106. int PNODE_::get_x() const
  107. {
  108.     return(x);
  109. }
  110.  
  111.  
  112.  
  113. int PNODE_::get_y() const
  114. {
  115.     return(y);
  116. }
  117.  
  118.  
  119.  
  120. /*                   COMP_BOARD
  121.  
  122.     Compares the current board configuration with another. Could
  123.     have used memcmp() here.
  124.  
  125. */ 
  126.  
  127. int PNODE_::compare_board(const char *b) const
  128. {
  129.     const char
  130.         *p = *board;
  131.     int
  132.         i;
  133.  
  134.     for (i = 0; i <= 8; i++)
  135.         if (*(p + i) != *(b + i))
  136.             return(0);
  137.  
  138.     return(1);
  139. }
  140.  
  141.  
  142.  
  143. /*                 DO_OPERATOR
  144.  
  145.     Applies operator n to the current configuration, i.e. it moves
  146.     the empty tile (by calling one of the do_..() functions) resulting
  147.     in a new board configuration or NULL if the operator can't be applied.
  148.  
  149. */
  150.  
  151. NODE_ *PNODE_::do_operator(int index) const
  152. {
  153.     switch(index)
  154.     {
  155.         case 0:
  156.             return(do_down());
  157.         case 1:
  158.             return(do_up());
  159.         case 2:
  160.             return(do_right());
  161.     }
  162.     return(do_left());
  163. }
  164.  
  165.  
  166.  
  167. PNODE_ *PNODE_::do_left() const
  168. {
  169.     if (!y)
  170.         return(NULL);
  171.  
  172.     return(new PNODE_(*board, x, y, x, y - 1));
  173. }
  174.  
  175.  
  176.  
  177. PNODE_ *PNODE_::do_right() const
  178. {
  179.     if (y == (2))
  180.         return(NULL);
  181.  
  182.     return(new PNODE_(*board, x, y, x, y + 1));
  183. }
  184.  
  185.  
  186.  
  187. PNODE_ *PNODE_::do_up() const
  188. {
  189.     if (!x)
  190.         return(NULL);
  191.  
  192.     return(new PNODE_(*board, x, y, x - 1, y));
  193. }
  194.  
  195.  
  196.  
  197. PNODE_ *PNODE_::do_down() const
  198. {
  199.     if (x == (2))
  200.         return(NULL);
  201.  
  202.     return(new PNODE_(*board, x, y, x + 1, y));
  203. }
  204.  
  205.  
  206.  
  207. int main()
  208. {
  209.     char
  210.         start[3][3] = {
  211.                         {1, 3, 4},
  212.                         {8, 0, 2},
  213.                         {7, 6, 5},
  214.                       };
  215.  
  216.     char
  217.         goal[3][3] = {
  218.                         {1, 2, 3},
  219.                         {8, 0, 4},
  220.                         {7, 6, 5},
  221.                     };
  222.  
  223.     PUZZLE_
  224.         puzzle(new PNODE_(*start, 1, 1), new PNODE_(*goal, 1, 1));
  225.  
  226.     puzzle.generate();
  227.     return(1);
  228. }
  229.