home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / MotifApp / ch5 / MoveGenerator.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  1.5 KB  |  53 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //////////////////////////////////////////////////////////////////////////////
  3. //         This example code is from the book:
  4. //
  5. //           Object-Oriented Programming with C++ and OSF/Motif
  6. //         by
  7. //           Douglas Young
  8. //           Prentice Hall, 1992
  9. //           ISBN 0-13-630252-1    
  10. //
  11. //         Copyright 1991 by Prentice Hall
  12. //         All Rights Reserved
  13. //
  14. //  Permission to use, copy, modify, and distribute this software for 
  15. //  any purpose except publication and without fee is hereby granted, provided 
  16. //  that the above copyright notice appear in all copies of the software.
  17. ///////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. ////////////////////////////////////////////////////
  22. // MoveGenerator.C
  23. //////////////////////////////////////////////////////
  24. #include "MoveGenerator.h"
  25. #include "Board.h"
  26. #include "unistd.h"
  27. #include "math.h"
  28. #include "rand48.h"
  29.  
  30.  
  31. MoveGenerator::MoveGenerator()
  32. {
  33.     srand48( (long) getpid() );
  34. }
  35.  
  36. int MoveGenerator::getNextMove ( Board *board )
  37. {
  38.     int randomIndex, movesLeft;
  39.     
  40.     // Get the list of free squares on the Board
  41.     
  42.     int * const freeSquares = board->freeSquares ( movesLeft );
  43.     
  44.     if ( movesLeft == 0 )
  45.     return -1;
  46.     
  47.     // Pick one of the free squares at random and return it
  48.     
  49.     randomIndex = (int) ( movesLeft * drand48() );
  50.     
  51.     return freeSquares[randomIndex];
  52. }
  53.