How to do it...

We will add a new function to the BoardAI class as follows:

  1. Create the ABNegamax function:
public static float ABNegamax( 
        Board board, 
        int player, 
        int maxDepth, 
        int currentDepth, 
        ref Move bestMove, 
        float alpha, 
        float beta) 
{ 
    // next steps here 
} 
  1. Validate the base case:
if (board.IsGameOver() || currentDepth == maxDepth) 
    return board.Evaluate(player); 
  1. Set the initial values:
bestMove = null; 
float bestScore = Mathf.NegativeInfinity; 
  1. Loop through every available move and return the best score:
foreach (Move m in board.GetMoves()) 
{ 
    // next steps here 
} 
return bestScore; 
  1. Create a new game state from the current move:
Board b = board.MakeMove(m); 
  1. Set the values for calling the recursion:
float recursedScore; Move ...

Get Unity 2018 Artificial Intelligence Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.