public void AddToTree(DfsNode parent, CheckerBoard currentBoard, int currentDepth) { if (currentDepth >= MAX_DEPTH) { return; } if (currentBoard.GetGameStatus() != GameStatuses.Running) { return; } IList <Move> allPossibleMoves = GetAvaliableMoves(currentBoard, currentBoard.NextPlayer); foreach (var move in allPossibleMoves) { // Kloniras board i odigraš jedan potez iz liste var newBoard = (CheckerBoard)currentBoard.Clone(); newBoard.MakeMove(move, currentBoard.NextPlayer); // Kreiraš node i dodaš ga kao child u parent var node = new DfsNode(move, newBoard); node.Name = $"{parent.Name}-{currentDepth + 1}"; parent.AddChild(node); // Rekurzivno pozivaš AddToTree da doda i za slijedece poteze AddToTree(node, newBoard, currentDepth + 1); } }
public Move GetMove(CheckerBoard currentBoard, Player player, int turn = 0) { var root = new DfsNode(currentBoard); root.Name = "0"; AddToTree(root, currentBoard, 0); if (root.Children == null || root.Children.Count() == 0) { return(null); } var bestScore = new BestInTreeScore { Score = Int32.MinValue, Moves = new List <IList <Move> >() }; findBestMoveInTree(root, new List <Move>(), player, bestScore); // Save tree //TreeJsonSaver.Save("dfs", root); if (bestScore.Moves == null || bestScore.Moves.Count == 0) { return(null); } else { //return bestScore.Moves.OrderBy(o => Guid.NewGuid()).First().First(); var bestPath = listHelper.Random(bestScore.Moves); return(bestPath.First()); } }
private void findBestMoveInTree(DfsNode node, IList <Move> previousMoves, Player player, BestInTreeScore bestScore) { // Dosao je do lista, evaluate if (node.Children == null || node.Children.Count() == 0) { var s = scoreForceAdventage(node.Board, player); if (s > bestScore.Score) { bestScore.Score = s; bestScore.Moves.Clear(); bestScore.Moves.Add(previousMoves); } else if (s == bestScore.Score) { bestScore.Moves.Add(previousMoves); } } else { foreach (var child in node.Children) { var pm = previousMoves.ToList(); pm.Add(child.Move); findBestMoveInTree(child, pm, player, bestScore); } } }