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

  1. #include "demo1.h"
  2.  
  3. /*                   PUZZLE_
  4.  
  5.     Passes the start node, goal node, and number of operators on
  6.     the DEPTH_GRAPH_'s constructor.
  7.  
  8. */
  9.  
  10. PUZZLE_::PUZZLE_(PNODE_ *start, PNODE_ *goal)
  11.     :DEPTH_GRAPH_(start, goal, 4)
  12. {
  13. }
  14.  
  15.  
  16.  
  17. /*                    PNODE_
  18.  
  19.     Creates a new board configuration.
  20.  
  21. */ 
  22.  
  23. PNODE_::PNODE_(OP_USED_ op_used, int empty_x, int empty_y)
  24. {
  25.     operator_used = op_used;
  26.     x = empty_x;
  27.     y = empty_y;
  28. }
  29.  
  30.  
  31.  
  32. /*                       DISPLAY
  33.  
  34.     Displays the operator applied to get to this state and the
  35.     coordinates of the empty tile of the current state.
  36.  
  37. */ 
  38.  
  39. void PNODE_::display() const
  40. {
  41.     static char
  42.         *move[] = { "start state",
  43.                     "left",
  44.                     "right",
  45.                     "up",
  46.                     "down"
  47.                   };
  48.  
  49.     printf("%s - empty spot at: [%d] [%d]\n", move[operator_used], y, x);
  50. }
  51.  
  52.  
  53.  
  54. /*                       EQUAL.CPP
  55.  
  56.     Determines if two states, i.e., two PNODE_'s are the same by
  57.     comparing the set of coordinates of both objects.
  58.  
  59. */
  60.  
  61. int PNODE_::equal(const VOBJECT_ &other) const
  62. {
  63.     return(x == ((const PNODE_ &)other).get_x()
  64.           && y == ((const PNODE_ &)other).get_y());
  65. }
  66.  
  67.  
  68.  
  69. /*                      GET_X.CPP
  70.  
  71.     Returns the x-coordinate of the position of the empty tile in
  72.     the current configuration.
  73.  
  74. */
  75.  
  76. int PNODE_::get_x() const
  77. {
  78.     return(x);
  79. }
  80.  
  81.  
  82.  
  83. /*                       GET_Y.CPP
  84.  
  85.     Returns the y-coordinate of the position of the empty tile in
  86.     the current configuration.
  87.  
  88. */
  89.  
  90. int PNODE_::get_y() const
  91. {
  92.     return(y);
  93. }
  94.  
  95.  
  96.  
  97. /*                    DO_OPER.CPP
  98.  
  99.     Returns a new PNODE_ object, i.e., a new state, by applying the
  100.     appropriate operator to the current state, or NULL when the
  101.     operator cannot be applied.
  102.  
  103. */
  104.  
  105. NODE_ *PNODE_::do_operator(int index) const
  106. {
  107.     switch(index)
  108.     {
  109.         case 0:
  110.             return(do_left());
  111.         case 1:
  112.             return(do_right());
  113.         case 2:
  114.             return(do_up());
  115.     }
  116.     return(do_down());
  117. }
  118.  
  119.  
  120.  
  121. /*                   DO_LEFT
  122.  
  123.     Creates a new state/node representing the configuration that
  124.     arises when the empty tile is moved left one position.
  125.  
  126. */
  127.  
  128. PNODE_ *PNODE_::do_left() const
  129. {
  130.     if (!x)
  131.         return(NULL);    // can't go left any further
  132.  
  133.     return(new PNODE_(left_used, x - 1, y));
  134. }
  135.  
  136.  
  137.  
  138. /*                   DO_RIGHT
  139.  
  140.     Creates a new state/node representing the configuration that
  141.     arises when the empty tile is moved right one position.
  142.  
  143. */ 
  144.  
  145. PNODE_ *PNODE_::do_right() const
  146. {
  147.     if (x == (4 - 1))
  148.         return(NULL);
  149.  
  150.     return(new PNODE_(right_used, x + 1, y));
  151. }
  152.  
  153.  
  154.  
  155. /*                        DO_UP
  156.  
  157.  
  158.     Creates a new state/node representing the configuration that
  159.     arises when the empty tile is moved up one position.
  160.  
  161. */
  162.  
  163. PNODE_ *PNODE_::do_up() const
  164. {
  165.     if (!y)
  166.         return(NULL);       // can't go up any further
  167.  
  168.     return(new PNODE_(up_used, x, y - 1));
  169. }
  170.  
  171.  
  172.  
  173. /*                    DO_DOWN
  174.  
  175.     Creates a new state/node representing the configuration that
  176.     arises when the empty tile is moved down one position.
  177.  
  178. */ 
  179.  
  180. PNODE_ *PNODE_::do_down() const
  181. {
  182.     if (y == (4 - 1))          // can't go down any further
  183.         return(NULL);
  184.  
  185.     return(new PNODE_(down_used, x, y + 1));
  186. }
  187.  
  188.  
  189. int main()
  190. {
  191.     PUZZLE_
  192.         puzzle(new PNODE_(none_used, 3, 3), new PNODE_(none_used, 0, 0));
  193.  
  194.     puzzle.generate();           // start search process
  195.  
  196.     return(1);
  197. }
  198.