private void PlanMove() { Debug.Log("Planning Move"); var watch = System.Diagnostics.Stopwatch.StartNew(); Move bestMove = null; int bestValue = -9999; List <Move> possibleMoves = BoardStateUtils.EnumerateAvailableMoves(_moves); List <Move> shuffledMoves = Shuffle.FisherYatesCardDeckShuffle <Move>(possibleMoves); foreach (Move move in shuffledMoves) { List <Move> moves = new List <Move>(_moves); moves.Add(move); int value = MiniMax.minimax(minimaxDepth, moves, -1000, 1000, false); if (value > bestValue) { bestMove = move; bestValue = value; } } watch.Stop(); Debug.Log("Time Taken: " + watch.ElapsedMilliseconds); if (watch.Elapsed.Milliseconds < 1000) { int delay = 1000 - watch.Elapsed.Milliseconds; Thread.Sleep(delay); } plannedMove = bestMove; planning = false; }
private void PlanRandomMove() { Debug.Log("Planning Random Move"); var watch = System.Diagnostics.Stopwatch.StartNew(); List <Move> possibleMoves = BoardStateUtils.EnumerateAvailableMoves(_moves); List <Move> shuffledMoves = Shuffle.FisherYatesCardDeckShuffle <Move>(possibleMoves); plannedMove = shuffledMoves[0]; watch.Stop(); Debug.Log("Time Taken: " + watch.Elapsed.Milliseconds); if (watch.Elapsed.Milliseconds < 1000) { int delay = 1000 - watch.Elapsed.Milliseconds; Thread.Sleep(delay); } planning = false; return; }
public static int minimax(int depth, List <Move> movesPlayed, int alpha, int beta, bool isMaximisingPlayer) { if (depth == 0 || IsTerminalState(BoardStateUtils.GenerateBoardState(movesPlayed))) { return(-GetBoardValue(BoardStateUtils.GenerateBoardState(movesPlayed))); } List <Move> shuffledMoves = BoardStateUtils.EnumerateAvailableMoves(movesPlayed); if (isMaximisingPlayer) { int bestMove = -9999; foreach (Move move in shuffledMoves) { List <Move> newMoves = new List <Move>(movesPlayed); newMoves.Add(move); bestMove = Math.Max(bestMove, minimax(depth - 1, newMoves, alpha, beta, !isMaximisingPlayer)); alpha = Math.Max(alpha, bestMove); if (beta <= alpha) { return(bestMove); } } return(bestMove); } else { int bestMove = 9999; foreach (Move move in shuffledMoves) { List <Move> newMoves = new List <Move>(movesPlayed); newMoves.Add(move); bestMove = Math.Min(bestMove, minimax(depth - 1, newMoves, alpha, beta, !isMaximisingPlayer)); beta = Math.Min(beta, bestMove); if (beta <= alpha) { return(bestMove); } } return(bestMove); } }