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

  1. #include "demo6.h"
  2.  
  3. PUZZLE_::PUZZLE_(PNODE_ *start, PNODE_ *target)
  4.     :BIDEPTH_GRAPH_(start, target, 4)
  5. {
  6. }
  7.  
  8.  
  9. PNODE_::PNODE_(const char *b, int empty_x, int empty_y)
  10. {
  11.     char
  12.         *p = *board;
  13.     int
  14.         i;
  15.  
  16.     for (i = 0; i <= 8; i++)
  17.         *(p + i) = *(b + i);
  18.  
  19.     x = empty_x;
  20.     y = empty_y;
  21. }
  22.  
  23.  
  24. PNODE_::PNODE_(const char *b, int old_x, int old_y, int new_x, int new_y)
  25. {
  26.     char
  27.         *p = *board;
  28.     int
  29.         i;
  30.  
  31.     for (i = 0; i <= 8; i++)
  32.         *(p + i) = *(b + i);
  33.  
  34.     board[old_x][old_y] = board[new_x][new_y];
  35.     board[new_x][new_y] = 0;
  36.  
  37.     x = new_x;
  38.     y = new_y;
  39. }
  40.  
  41.  
  42. void PNODE_::display() const
  43. {
  44.     int
  45.         row,
  46.         col;
  47.  
  48.     for (row = 0; row < 3; row++)
  49.     {
  50.         for (col = 0; col < 3; col++)
  51.             printf("%d ", board[row][col]);
  52.         putchar('\n');
  53.     }
  54.     putchar('\n');
  55. }
  56.  
  57.  
  58. int PNODE_::equal(const VOBJECT_ &other) const
  59. {
  60.      if (x != ((const PNODE_ &)other).get_x()
  61.           && y != ((const PNODE_ &)other).get_y())
  62.             return(0);
  63.     return(compare_board(((const PNODE_ &)other).get_board()));
  64. }
  65.  
  66.  
  67. int PNODE_::get_x() const
  68. {
  69.     return(x);
  70. }
  71.  
  72.  
  73. int PNODE_::get_y() const
  74. {
  75.     return(y);
  76. }
  77.  
  78.  
  79. const char *PNODE_::get_board() const
  80. {
  81.     return(*board);
  82. }
  83.  
  84.  
  85. int PNODE_::compare_board(const char *b) const
  86. {
  87.     const char
  88.         *p = *board;
  89.     int
  90.         i;
  91.  
  92.     for (i = 0; i <= 8; i++)
  93.         if (*(p + i) != *(b + i))
  94.             return(0);
  95.  
  96.     return(1);
  97. }
  98.  
  99.  
  100. NODE_ *PNODE_::do_operator(int index) const
  101. {
  102.     switch(index)
  103.     {
  104.         case 0:
  105.             return(do_left());
  106.         case 1:
  107.             return(do_right());
  108.         case 2:
  109.             return(do_up());
  110.     }
  111.     return(do_down());
  112. }
  113.  
  114.  
  115. PNODE_ *PNODE_::do_left() const
  116. {
  117.     if (!y)
  118.         return(NULL);
  119.  
  120.     return(new PNODE_(*board, x, y, x, y - 1));
  121. }
  122.  
  123.  
  124. PNODE_ *PNODE_::do_right() const
  125. {
  126.     if (y == (2))
  127.         return(NULL);
  128.  
  129.     return(new PNODE_(*board, x, y, x, y + 1));
  130. }
  131.  
  132.  
  133. PNODE_ *PNODE_::do_up() const
  134. {
  135.     if (!x)
  136.         return(NULL);
  137.  
  138.     return(new PNODE_(*board, x, y, x - 1, y));
  139. }
  140.  
  141.  
  142. PNODE_ *PNODE_::do_down() const
  143. {
  144.     if (x == (2))
  145.         return(NULL);
  146.  
  147.     return(new PNODE_(*board, x, y, x + 1, y));
  148. }
  149.  
  150.  
  151. int main()
  152. {
  153.     char
  154.         start[3][3] = {
  155.                         {1, 3, 4},
  156.                         {8, 0, 2},
  157.                         {7, 6, 5},
  158.                       };
  159.  
  160.     char
  161.         goal[3][3] = {
  162.                         {1, 2, 3},
  163.                         {8, 0, 4},
  164.                         {7, 6, 5},
  165.                     };
  166.  
  167.     PUZZLE_
  168.         puzzle(new PNODE_(*start, 1, 1), new PNODE_(*goal, 1, 1));
  169.  
  170.     puzzle.generate();
  171.     return(1);
  172. }
  173.