public PhutballMoveScore Search(IFieldsGraph fieldsGraph) { if (_alphaBetaSearchDepth.SearchDepth == 0) { return(PhutballMoveScore.Empty()); } var actualGraph = (IFieldsGraph)fieldsGraph.Clone(); var performMoves = new PerformMoves(actualGraph, _playersState); var visitedNodes = new VisitedNodesCounter <JumpNode>(); _alphaBetaSearch = new AlphaBetaSearch <JumpNode>( new WhiteStoneToCurrentPlayerBorderDistance(_playersState, actualGraph, _alphaBetaSearchDepth.DistanceToBorderWeight) .Add(new BlackStoneToTargetBorderCount(_playersState, actualGraph, _alphaBetaSearchDepth.BlackStonesToBorderWeight)) , _alphaBetaSearchDepth, new PerformMovesNodeVisitor(performMoves).FollowedBy(visitedNodes) ); var movesTree = _movesFactory(actualGraph); _alphaBetaSearch.Run(movesTree); var result = new CompositeMove(); _alphaBetaSearch.BestMove.Move.MovesFromRoot.CollectToPlayerSwitch(result); return(new PhutballMoveScore(result, _alphaBetaSearch.BestMove.Score) { CuttoffsCount = _alphaBetaSearch.CuttoffsCount, VisitedNodesCount = visitedNodes.Count }); }
public void should_generate_moves_properly() { _fieldsGraph = new TestFieldsGraph( new[] { new[] { Empty, Empty, Empty, Empty, Empty }, new[] { Empty, Empty, Empty, Empty, Empty }, new[] { Empty, Empty, Black, Empty, Empty }, new[] { Empty, Empty, Black, Empty, Empty }, new[] { Empty, Empty, Empty, Empty, Empty }, new[] { Empty, Empty, Black, Empty, Empty }, new[] { Empty, Empty, White, Empty, Empty }, new[] { Empty, Empty, Empty, Empty, Empty }, new[] { Empty, Empty, Empty, Empty, Empty }, new[] { Empty, Empty, Empty, Empty, Empty }, } ).Build(); var current = new RootedBySelectingWhiteFieldBoardJumpTree(_fieldsGraph); var currentMoves = current.TraverseDfs( new PerformMovesNodeVisitor(PerformMoves.DontCareAboutPlayerStateChange(_fieldsGraph))) .Skip(1).ToList(); currentMoves.ShouldHaveCount(2); }
public IEnumerator <IJumpNodeTreeWithFactory> GetEnumerator() { var actualGraph = (IFieldsGraph)_parentJumpNode.ActualGraph.Clone(); var localMovePerformer = PerformMoves.DontCareAboutPlayerStateChange(actualGraph); var current = new RootedBySelectingWhiteFieldBoardJumpTree(actualGraph); var visitor = new PerformMovesNodeVisitor(localMovePerformer).FollowedBy(_afterMoveVisitor); var currentMoves = current.TraverseDfs(visitor, _alphaBetaOptions.JumpsMaxDepth) .Skip(_alphaBetaOptions.SkipShortMoves); foreach (var currentMove in currentMoves) { var newMove = CreateNewMove(currentMove); var jumpNode = _parentJumpNode.FollowedBy(newMove); yield return(new AlternatingJumpsMovesTree(jumpNode, Parent.ChildFactory)); } }
public PhutballMoveScore Search(IFieldsGraph fieldsGraph) { var graphCopy = (IFieldsGraph)fieldsGraph.Clone(); var tree = _movesFactory.GetMovesTree(graphCopy); var targetBorder = _playersState.CurrentPlayer.GetTargetBorder(fieldsGraph); var performMoves = new PerformMoves(graphCopy, _playersState); var bestValuePicker = new PickBestValueNodeVisitor(targetBorder, graphCopy, performMoves); var nodeCounter = new VisitedNodesCounter <JumpNode>(); var depthCounter = new DepthCounterNodeVisitor <JumpNode>(); var searchNodeVisitor = _defaultNodeVistor.FollowedBy(bestValuePicker).FollowedBy(nodeCounter).FollowedBy(depthCounter); var search = _searchFactory(searchNodeVisitor, performMoves, targetBorder); search.Run(tree); return(new PhutballMoveScore(bestValuePicker.ResultMove, bestValuePicker.CurrentMaxValue) { VisitedNodesCount = nodeCounter.Count, MaxDepth = depthCounter.MaxDepth }); }
protected IFieldsGraph AfterMoveOn(TestFieldsGraph graphToSearch) { var actualGraph = graphToSearch.Build(); _playersState = PlayersState.SecondIsOnTheMove(); var phutballOptions = new PhutballOptions() { RowCount = actualGraph.RowCount, ColumnCount = actualGraph.ColumnCount }; RawMoveFinders = new RawMoveFinders(new MovesFactory(), _playersState, phutballOptions); _performMoves = new PerformMoves(actualGraph, new NulloPlayersSwapper()); _strategy = GetSearchStrategy(); _bestMove = _strategy.Search(actualGraph); if (_bestMove.Move != null) { _performMoves.Perform(_bestMove.Move); } return(actualGraph); }