public static Move DecideMovement(Board board, Player player) { Board newBoard = new Board(); newBoard.pieceList = new List <Piece>(board.pieceList); MinimaxTreeNode minimaxTree = GenerateMinimaxTree(null, player, player.GetEnemy(), board, newBoard, 0, 3); List <MinimaxTreeNode> biggestNodes = new List <MinimaxTreeNode>(); int biggestValue = MIN_HEURISTIC_VALUE; foreach (MinimaxTreeNode childNode in minimaxTree.childMovements) { if (childNode.value > biggestValue) { biggestNodes.Clear(); biggestNodes.Add(childNode); biggestValue = childNode.value; } else if (childNode.value == biggestValue) { biggestNodes.Add(childNode); } } MinimaxTreeNode randomNode; Random rand = new Random(); randomNode = biggestNodes[rand.Next(biggestNodes.Count - 1)]; Console.WriteLine("Calculado movimento. Valor: " + randomNode.value); return(randomNode.movement); }
public static MinimaxTreeNode GenerateMinimaxTree(Move previousMovement, Player max, Player min, Board initial, Board current, int currentDepth, int treeDepth) { Player player; if (currentDepth % 2 == 0) { player = max; } else { player = min; } MinimaxTreeNode node = new MinimaxTreeNode(); node.movement = previousMovement; if (currentDepth == treeDepth) { node.value = CalculateMoveValue(initial, current, max); node.childMovements = null; } else { List <Move> possibleMoves = player.PossibleMovements(current); foreach (Move move in possibleMoves) { Board newBoard = new Board(); newBoard.pieceList = new List <Piece>(Board.GenerateMovement(current, move).pieceList); MinimaxTreeNode childNode = GenerateMinimaxTree(move, max, min, initial, newBoard, currentDepth + 1, treeDepth); node.childMovements.Add(childNode); } if (node.childMovements.Count == 0) { node.value = CalculateMoveValue(initial, current, max); } else { bool firstNode = true; foreach (MinimaxTreeNode childNode in node.childMovements) { if (firstNode == true) { firstNode = false; node.value = childNode.value; } else { if (player == max) { if (childNode.value > node.value) { node.value = childNode.value; } } else { if (childNode.value < node.value) { node.value = childNode.value; } } } } } } return(node); }