ScoringSquare Minimax(Board board, int depth) { // Devuelve el score del tablero y la jugada con la que se llega a él. int bestMove = 0; int bestScore = 0; ScoringSquare scoringSquare; // score, movimiento Board newBoard; // Comprobar si hemos terminado de hacer recursión if (board.IsEndOfGame() || depth == MAX_DEPTH) { scoringSquare = new ScoringSquare(board.Evaluate(activePlayer), 0); } else { if (board.activePlayer == activePlayer) { bestScore = MINUS_INFINITE; } else { bestScore = INFINITE; } List <Square> possibleMoves = new List <Square>(); possibleMoves = board.PossibleMoves(); Shuffle(possibleMoves); foreach (Square move in possibleMoves) { newBoard = board.GenerateNewBoardFromMove(move); // Recursividad scoringSquare = Minimax(newBoard, (depth + 1)); // Actualizar mejor score if (board.activePlayer == activePlayer) { if (scoringSquare.Score > bestScore) { bestScore = scoringSquare.Score; bestMove = move.Index; } } else { if (scoringSquare.Score < bestScore) { bestScore = scoringSquare.Score; bestMove = move.Index; } } } scoringSquare = new ScoringSquare(bestScore, bestMove); } return(scoringSquare); }
ScoringSquare NegamaxAB(Board board, byte depth, int alfa, int beta) { // Devuelve el score del tablero y la jugada con la que se llega a él. int bestMove = 0; int bestScore = 0; ScoringSquare scoringSquare; // score, movimiento Board newBoard; // Comprobar si hemos terminado de hacer recursión if (board.IsEndOfGame() || depth == MAX_DEPTH) { if (depth % 2 == 0) { scoringSquare = new ScoringSquare(board.Evaluate(activePlayer), 0); } else { scoringSquare = new ScoringSquare(-board.Evaluate(activePlayer), 0); } } else { bestScore = MINUS_INFINITE; List <Square> possibleMoves = new List <Square>(); possibleMoves = board.PossibleMoves(); Shuffle(possibleMoves); foreach (Square move in possibleMoves) { newBoard = board.GenerateNewBoardFromMove(move); // Recursividad scoringSquare = NegamaxAB(newBoard, (byte)(depth + 1), -beta, -Math.Max(alfa, bestScore)); int invertedScore = -scoringSquare.Score; // Actualizar mejor score if (invertedScore > bestScore) { bestScore = invertedScore; bestMove = move.Index; } if (bestScore >= beta) { scoringSquare = new ScoringSquare(bestScore, bestMove); return(scoringSquare); } } scoringSquare = new ScoringSquare(bestScore, bestMove); } return(scoringSquare); }
/// <summary> /// Controla el movimiento final de la IA /// </summary> /// <param name="scoringSquare"></param> public void AIEnded(ScoringSquare scoringSquare) { Line line = board.ChooseLine(scoringSquare.SquareIndex); line.On_Pressed(); }
public void Move(ScoringSquare move) //Realiza su movimiento { GameController.Instance.AIEnded(move); }