Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. //--------------minimax pradzia----------------------
  2. void minimax(vector<Planet> planets, int depth, int &sim)
  3. {
  4.     int bestMoveScore = 1000;
  5.     Move bestMove;
  6.    
  7.     for(int i = 0; i < planets.size(); i++){
  8.         if(planets[i].canAssign){
  9.             planets[i].myUnits += 5;
  10.             planets[i].myTolerance--;
  11.             sim++;
  12.             cerr << i << " - ";
  13.            
  14.             int tempMoveScore = maxSearch(planets, depth+1, sim);
  15.            
  16.             planets[i].myUnits -= 5;
  17.             for(int j = 0; j < planets[i].near.size(); j++){
  18.                 int x = planets[i].near[j];
  19.                 planets[x].myUnits++;
  20.             }
  21.            
  22.             int tempMoveScoreSpread = maxSearch(planets, depth+1, sim);
  23.            
  24.             cerr << tempMoveScore << " " << tempMoveScoreSpread << endl;
  25.            
  26.             if(tempMoveScore <= bestMoveScore){
  27.                 bestMoveScore = tempMoveScore;
  28.                 bestMove.target = i;
  29.             }
  30.            
  31.             if(tempMoveScoreSpread >= bestMoveScore){
  32.                 bestMoveScore = tempMoveScoreSpread;
  33.                 bestMove.target = i;
  34.                 bestMove.spread = to_string(i);
  35.             }
  36.            
  37.             planets[i].myTolerance++;
  38.             for(int j = 0; j < planets[i].near.size(); j++){
  39.                 int x = planets[i].near[j];
  40.                 planets[x].myUnits--;
  41.             }
  42.         }
  43.     }
  44.    
  45.     for(int i = 0; i < 5; i++){
  46.         cout << bestMove.target << endl;
  47.     }
  48.     cout << bestMove.spread << endl;
  49.    
  50. }
  51.  
  52. //----------minimax max reiksme----------------------
  53. int maxSearch(vector<Planet> planets, int depth, int &sim)
  54. {
  55.     if((clock()-t0)/1000 > 40 || depth >= Depth) return score(planets);
  56.     Move bestMove;
  57.    
  58.     int bestMoveScore = -1000;
  59.     for(int i = 0; i < planets.size(); i++){
  60.         if(enemyAssign(planets, i)){
  61.             planets[i].otherUnits += 5;
  62.             planets[i].otherTolerance--;
  63.             sim++;
  64.             //cerr << " E " << i ;
  65.            
  66.             calculateMove(planets);
  67.            
  68.             int tempMoveScore = minSearch(planets, depth+1, sim);
  69.             if(tempMoveScore >= bestMoveScore){
  70.                 bestMoveScore = tempMoveScore;
  71.                 bestMove.target = i;
  72.             }
  73.             planets[i].otherUnits -= 5;
  74.             planets[i].otherTolerance++;
  75.         }
  76.     }
  77.     return bestMoveScore;
  78. }
  79.  
  80. //------------minimax min reiksme----------------------
  81. int minSearch(vector<Planet> planets, int depth, int &sim)
  82. {
  83.     if((clock()-t0)/1000 > 40 || depth >= Depth) return score(planets);
  84.     Move bestMove;
  85.    
  86.     int bestMoveScore = 1000;
  87.     for(int i = 0; i < planets.size(); i++){
  88.         if(myAssign(planets,i)){
  89.             planets[i].myUnits += 5;
  90.             planets[i].myTolerance--;
  91.             sim++;
  92.            
  93.             //cerr << " M " << i;
  94.            
  95.             int tempMoveScore = maxSearch(planets, depth+1, sim);
  96.             if(tempMoveScore <= bestMoveScore){
  97.                 bestMoveScore = tempMoveScore;
  98.                 bestMove.target = i;
  99.             }
  100.             planets[i].myUnits -= 5;
  101.             planets[i].myTolerance++;
  102.         }
  103.     }
  104.     return bestMoveScore;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement