private GameNode GetMax(Graph graph, GameNode node, int depth) { IList<Edge<GameNode>> outEdges = graph.OutEdges(node).ToList(); if (!outEdges.Any()) { return null; } foreach (var edge in outEdges) { GetMax(graph, edge.Target, depth + 1); } if (depth%2 == 0) //Max Level { GameNode maxOf = MaxOf(outEdges); node.Value = maxOf.Value; return maxOf; } GameNode minOf = MinOf(outEdges); //Min Level node.Value = minOf.Value; return minOf; }
private Graph BuildTree(Game currentGame, int depth) { var graph = new Graph(false); var currentNode = new GameNode("00", null, _player); graph.AddVertex(currentNode); BuildTree(graph, currentGame, currentNode, depth); return graph; }
private void BuildTree(Graph graph, Game parentGame, GameNode parentNode, int depth) { //A potantial next parentNode may have no available moves if (depth == 0 || parentGame.AvailableMoves == null) { return; } bool leafNode = depth == 1; foreach (string move in parentGame.AvailableMoves) { Game childGame = GameService.Generate(parentGame, move, leafNode); string id = parentNode.Id + "=>" + move; var childNode = new GameNode(id, move, parentGame.CurrentPlayer) { Value = _evaluator.Evaluate(childGame.BoardState, _player) }; graph.AddVertex(childNode); graph.AddEdge(new Edge<GameNode>(parentNode, childNode)); BuildTree(graph, childGame, childNode, depth - 1); } }
private void BuildTree(Graph graph, Game parentGame, GameNode parentNode, int depth) { //A potantial next parentNode may have no available moves if (depth == 0 || parentGame.AvailableMoves == null) { return; } bool leafNode = depth == 1; foreach (string move in parentGame.AvailableMoves) { Game childGame = GameService.Generate(parentGame, move, leafNode); string id = parentNode.Id + "=>" + move; var childNode = new GameNode(id, move, parentGame.CurrentPlayer) { Value = _evaluator.Evaluate(childGame.BoardState, _player) }; graph.AddVertex(childNode); graph.AddEdge(new Edge <GameNode>(parentNode, childNode)); BuildTree(graph, childGame, childNode, depth - 1); } }
private void Traverse(Graph graph, GameNode node) { Console.WriteLine(node.Id + " : " + node.Value); foreach (var edge in graph.OutEdges(node)) { if (graph.OutEdges(edge.Target).Any()) { Traverse(graph, edge.Target); } else { Console.WriteLine(edge.Target.Id + " : " + edge.Target.Value); } } }