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

  1. #include "nodes.h"
  2.  
  3. /*            NODE_
  4.  
  5.     The constructor initializes the node by setting all pointers to NULL.
  6.  
  7. */
  8.  
  9. NODE_::NODE_()
  10. {
  11.     parent = NULL;
  12.     next = NULL;
  13. }
  14.  
  15.  
  16.  
  17. /*            EXPAND
  18.  
  19.     This function creates a linked list of NODE_'s by calling do_operator()
  20.     and linking the nodes do_operator() returns together. We make expand()
  21.     virtual because the user may want to implement his own version of
  22.     expand().
  23.  
  24. */
  25.  
  26. NODE_ *NODE_::expand(int op) const
  27. {
  28.     NODE_
  29.     *start = NULL,         // important, marks end of list
  30.     *tmp;
  31.  
  32.     for (op--; op >= 0; op--)    // apply all of the operators one by one
  33.     {
  34.     tmp = do_operator(op);
  35.     if (tmp == NULL)         // operator not applicable 
  36.         continue;
  37.  
  38.     tmp->setnext(start);     // builds a linked list
  39.     start = tmp;
  40.     }
  41.     return(start);
  42. }
  43.  
  44.  
  45.  
  46. /*            SETPARENT
  47.  
  48.     Sets parent of node.
  49.  
  50. */
  51.  
  52. void NODE_::setparent(NODE_ *par)
  53. {
  54.     parent = par;
  55. }
  56.  
  57.  
  58.  
  59. /*                      EVAL
  60.  
  61.     We don't make this function pure virtual because it's only needed in
  62.     a best first and uniform cost search. Still, we must implement it
  63.     because it is called in solve() for every node.
  64.  
  65. */ 
  66.  
  67. int NODE_::eval(const SVOBJECT_ &) const
  68. {
  69.     return(1);
  70. }
  71.  
  72.  
  73.  
  74. /*            GETPARENT
  75.  
  76.     Returns parent of node.
  77.  
  78. */
  79.  
  80. NODE_ *NODE_::getparent() const
  81. {
  82.         return(parent);
  83. }
  84.  
  85.  
  86.  
  87. /*            DO_OPER
  88.  
  89.     We can't make this function pure virtual because the user may
  90.     implement either do_operator() or expand().
  91.  
  92. */
  93.  
  94. NODE_ *NODE_::do_operator(int ) const
  95. {
  96.     puts("Wrong do_operator() function called!");
  97.     return(NULL);
  98. }
  99.  
  100.  
  101.  
  102. /*            GETNEXT
  103.  
  104.     Returns the next node in the list.
  105.  
  106. */
  107.  
  108. NODE_ *NODE_::getnext() const
  109. {
  110.     return(next);
  111. }
  112.  
  113.  
  114.  
  115. /*            SETNEXT
  116.  
  117.     Sets next field of node.
  118.  
  119. */
  120.  
  121. void NODE_::setnext(NODE_ *node)
  122. {
  123.     next = node;
  124. }
  125.