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

  1. #include "demo2.h"
  2.  
  3. KNIGHT_::KNIGHT_(KNODE_ *start, KNODE_ *target)
  4.     : BREADTH_GRAPH_(start, target, 8)
  5. {
  6. }
  7.  
  8.  
  9. KNODE_::KNODE_(int pos1, int pos2)
  10. {
  11.     x = pos1;
  12.     y = pos2;
  13. }
  14.  
  15.  
  16. void KNODE_::display() const
  17. {
  18.     printf("%d/%d\n", x, y);   // display x- and y-coordinates of position
  19. }
  20.  
  21.  
  22. int KNODE_::equal(const VOBJECT_ &other) const
  23. {
  24.     return(x == ((const KNODE_ &)other).get_x()
  25.            && y == ((const KNODE_ &)other).get_y());
  26. }
  27.  
  28.  
  29. int KNODE_::get_x() const
  30. {
  31.     return(x);
  32. }
  33.  
  34.  
  35. int KNODE_::get_y() const
  36. {
  37.     return(y);
  38. }
  39.  
  40.  
  41. NODE_ *KNODE_::do_operator(int op) const
  42. {
  43.     int
  44.         new_x, new_y;
  45.  
  46.     switch(op)        // we keep it simple here...
  47.     {
  48.         case 0:
  49.             new_x = x + 2;
  50.             new_y = y + 1;
  51.             break;
  52.         case 1:
  53.             new_x = x + 2;
  54.             new_y = y - 1;
  55.             break;
  56.         case 2:
  57.             new_x = x - 2;
  58.             new_y = y + 1;
  59.             break;
  60.         case 3:
  61.             new_x = x - 2;
  62.             new_y = y - 1;
  63.             break;
  64.         case 4:
  65.             new_x = x + 1;
  66.             new_y = y + 2;
  67.             break;
  68.         case 5:
  69.             new_x = x - 1;
  70.             new_y = y + 2;
  71.         case 6:
  72.             new_x = x + 1;
  73.             new_y = y - 2;
  74.         default:
  75.             new_x = x - 1;
  76.             new_y = y - 2;
  77.     }
  78.     return(( new_x > 0 && new_x < 9 && new_y > 0 && new_y < 9) ?
  79.             new KNODE_(new_x, new_y) : NULL);
  80. }
  81.  
  82.  
  83. int main()
  84. {
  85.     KNIGHT_
  86.         knight(new KNODE_(1, 1), new KNODE_(3, 3));
  87.  
  88.     knight.generate();
  89.     return(1);
  90. }
  91.  
  92.