Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. bool GameBoard::KT_Heuristic(unsigned row, unsigned column)
  2. {
  3.     // only iterate if a tour hasn't been found yet
  4.     if( !success )
  5.     {
  6.         int position = Convert2D(row, column);  // find 1D position
  7.  
  8.         PlaceKnight(row, column);               // place the knight
  9.  
  10.         // if a tour wasn't found
  11.         if( !success )
  12.         {
  13.             // find all possible moves from current position
  14.             std::vector<int> possible_moves = FindPossibleMoves(position);
  15.             // list of possible moves sorted into a linear fashion
  16.             std::list<int> sorted_moves;
  17.  
  18.             // if there are possible moves
  19.             if( possible_moves.size() )
  20.             {
  21.                 // for each possible move
  22.                 for( unsigned i = 0; i < possible_moves.size(); ++i )
  23.                 {
  24.                     // update the heuristics table
  25.                     --hTable_[possible_moves[i]];
  26.  
  27.                     // if there are already items in the sorted moves list
  28.                     if( sorted_moves.size() )
  29.                     {
  30.                         unsigned size = sorted_moves.size();    // check size (to see if it changes)
  31.                         // for each item in the sorted list
  32.                         for( std::list<int>::iterator iter = sorted_moves.begin(); iter != sorted_moves.end(); ++iter )
  33.                         {
  34.                             // check to see where it should be inserted
  35.                             if( !CompareHTable((*iter), possible_moves[i]) )
  36.                             {
  37.                                 // insert the move into the list where it belongs
  38.                                 sorted_moves.insert(iter, possible_moves[i]);
  39.                                 break; // cut out
  40.                             }
  41.                         }
  42.                         // if the size didn't change, then no insert; it belongs at the end
  43.                         if( size == sorted_moves.size() )
  44.                             sorted_moves.push_back(possible_moves[i]);  // add it to the end
  45.                     }
  46.                     else    // otherwise if the list is empty
  47.                         sorted_moves.push_back(possible_moves[i]);  // start the list
  48.                 }
  49.  
  50.                 // for each possible sorted move
  51.                 for( std::list<int>::iterator iter = sorted_moves.begin(); iter != sorted_moves.end(); ++iter )
  52.                 {
  53.                     int n_row, n_column;
  54.                     ConvertBack((*iter), n_row, n_column);  // find the 2D point
  55.  
  56.                     // if the possible move is empty
  57.                     if( board_[(*iter)] == BLANK )
  58.                         success = KT_Heuristic(n_row, n_column);    // follow recursion through the new spot
  59.                 }
  60.             }
  61.             else    // otherwise
  62.             {
  63.                 success = false;    // the tour is not successful
  64.             }
  65.  
  66.             if( !success )  // if the path was not successful
  67.                 RemoveKnight(row,column);   // backtrack
  68.         }
  69.     }
  70.  
  71.     return success; // return if the tour was successful or not
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement