alpha-beta剪枝搜尋

來源:互聯網
上載者:User

   // basic Alpha-Beta search;

     move bestmove;


    int alphabeta(int depth, int alpha, int beta, bool isMyTurn) ...{

     if (gameOver || depth == 0) ...{
      return evaluation();
     }

       generateLegalMoves();

        // you may  change the search order to make the search faster

     for (each move m) ...{
      do move m;
      score = -alphabeta(depth - 1, -beta, -alpha, ! isMyTurn );
      if (score > alpha) ...{

             alpha = score;

             if(at the Level 1)     

              bestmove = m;
      }
      undo move-m;
      if (alpha >= beta) ...{
       break;
      }
     }
     return alpha;
    }

Initial call  :  alphabeta(depth,  - infinite,  infinite,  isMyTurn);

不理解上面的代碼的話,看看下面的極小極大搜尋

極小極大搜尋

//極大搜尋

int Max(int depth)
...{
 int best = -INFINITY;
 if (depth == 0)     
   ...{
  return evaluation();
 }
 generateLegalMoves();
 for (each move m) ...{
   ...{
  do move m;
  val = Min(depth - 1);       // call the Min search
      undo move m;
  if (val > best)
     ...{
   best = val;
  }
 }
 return best;
}
 
int Min(int depth) // Min search
...{
 int best = INFINITY;
 if (depth <= 0)
   ...{
  return evaluation();
 }
 GenerateLegalMoves();
 for (each move m) ...{
  do move m;
  val = Max(depth - 1);    // call the Max search
       undo move m;
  if (val < best)
     ...{
   best = val;
  }
 }
 return best;
}

負極大搜尋

極小極大搜尋可以統一用負極大搜尋代替

int NegaMax(int depth)
...{
 int best = -INFINITY;
 if (depth == 0)
   ...{
  return evaluation();
 }
 GenerateLegalMoves();
   for (each move m) ...{
  do move m; 
  val = - NegaMax(depth - 1);
  undo move m;
  if (val > best)
     ...{
   best = val;
  }
 }
 return best;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.