We will create the base class for handling all of our main algorithms and implement the Minimax function as follows:
- Create the BoardAI class:
using UnityEngine; using System.Collections; public class BoardAI { }
- Declare the Minimax function:
public static float Minimax( Board board, int player, int maxDepth, int currentDepth, ref Move bestMove) { // next steps here }
- Consider the base case:
if (board.IsGameOver() || currentDepth == maxDepth) return board.Evaluate(player);
- Set the initial values depending on the player:
bestMove = null; float bestScore = Mathf.Infinity; if (board.GetCurrentPlayer() == player) bestScore = Mathf.NegativeInfinity;
- Loop through all the possible moves and return the best score: