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 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 PhutballMoveScore Search(IFieldsGraph fieldsGraph) { var graphCopy = (IFieldsGraph)fieldsGraph.Clone(); var tree = _movesFactory.GetMovesTree(graphCopy); var targetBorder = _playersState.CurrentPlayer.GetTargetBorder(fieldsGraph); var cuttoffVisitor = new CuttoffPickBestValueNodeVisitor(targetBorder, graphCopy, _playersState) { CuttoffToTargetBorder = CuttoffToTarget }; var performMoves = cuttoffVisitor.MovesPerformer; var depthCounter = new DepthCounterNodeVisitor<JumpNode>(); var stopOnMaxNodesVisited = new StopOnVisitedNodesCount<JumpNode>(MaxVisitedNodes); var searchNodeVisitor = stopOnMaxNodesVisited .FollowedBy(cuttoffVisitor) .FollowedBy(depthCounter) .FollowedBy(_defaultNodeVistor); var search = _searchFactory(searchNodeVisitor, performMoves, targetBorder); search.Run(tree); return new PhutballMoveScore(cuttoffVisitor.PickBestValue.ResultMove, cuttoffVisitor.PickBestValue.CurrentMaxValue) { VisitedNodesCount = stopOnMaxNodesVisited.VistedNodesCount, MaxDepth = depthCounter.MaxDepth, CuttoffsCount = cuttoffVisitor.CuttoffsCount }; }
public FieldJumpIterator(Field start, IFieldsGraph fieldsGraph, StoneMover stoneMover) { _start = start; Current = _start; _fieldsGraph = fieldsGraph; _stoneMover = stoneMover; }
public StoneJumper(IFieldsGraph fieldsGraph, Field from, Field to) { _fieldsGraph = fieldsGraph; _from = from; _to = to; _jumpersFactory = new JumpersFactory(_fieldsGraph); }
public Tuple <TimeSpan, PhutballMoveScore> brute_force_time(IFieldsGraph graph) { var search = GetSearchEngine(graph); var time = MessureTime(() => search.Search(graph)); return(Tuple.Create(time.Item1, time.Item2)); }
public StoneJumper(IFieldsGraph fieldsGraph, Field from, Field to) { _fieldsGraph = fieldsGraph; _from = from; _to = to; _jumpersFactory= new JumpersFactory(_fieldsGraph); }
public PhutballMoveScore Search(IFieldsGraph fieldsGraph) { var graphCopy = (IFieldsGraph)fieldsGraph.Clone(); var tree = _movesFactory.GetMovesTree(graphCopy); var targetBorder = _playersState.CurrentPlayer.GetTargetBorder(fieldsGraph); var cuttoffVisitor = new CuttoffPickBestValueNodeVisitor(targetBorder, graphCopy, _playersState) { CuttoffToTargetBorder = CuttoffToTarget }; var performMoves = cuttoffVisitor.MovesPerformer; var depthCounter = new DepthCounterNodeVisitor <JumpNode>(); var stopOnMaxNodesVisited = new StopOnVisitedNodesCount <JumpNode>(MaxVisitedNodes); var searchNodeVisitor = stopOnMaxNodesVisited .FollowedBy(cuttoffVisitor) .FollowedBy(depthCounter) .FollowedBy(_defaultNodeVistor); var search = _searchFactory(searchNodeVisitor, performMoves, targetBorder); search.Run(tree); return(new PhutballMoveScore(cuttoffVisitor.PickBestValue.ResultMove, cuttoffVisitor.PickBestValue.CurrentMaxValue) { VisitedNodesCount = stopOnMaxNodesVisited.VistedNodesCount, MaxDepth = depthCounter.MaxDepth, CuttoffsCount = cuttoffVisitor.CuttoffsCount }); }
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 IEnumerable <Tuple <int, int> > BottomIsTarget(IFieldsGraph fieldsGraph) { var coord = fieldsGraph.GetWhiteFieldCoords(); return(_allBottom.SelectMany(MultiplyByRadius) .Select(r => coord.Add(r))); }
public IEnumerable<Tuple<int, int>> UpperIsTarget(IFieldsGraph fieldsGraph) { var coord = fieldsGraph.GetWhiteFieldCoords(); return _allUpper.SelectMany(MultiplyByRadius) .Select(r=> coord.Add(r)); }
public BestMoveApplier(IMoveFinders moveFinders, MovesHistory movesHistory, IFieldsGraph fieldsGraph, IPerformMoves performMoves, IEventPublisher eventPublisher) { _moveFinders = moveFinders; _eventPublisher = eventPublisher; _performMoves = performMoves; _movesHistory = movesHistory; _fieldsGraph = fieldsGraph; }
public IEnumerable <Tuple <int, int> > BottomIsTarget(IFieldsGraph fieldsGraph) { return(Direction.AllBottom.Take(5).Select(dir => _firstPosition.Add(dir)) .Concat(new[] { _firstPosition }) .Concat(_whiteCoords) .Concat(Direction.AllBottom.Take(3).SelectMany( dir => _whiteCoords.Select(coord => coord.Add(dir))))); }
public IEnumerable<Tuple<int, int>> UpperIsTarget(IFieldsGraph fieldsGraph) { return Direction.AllUpper.Take(5).Shuffle().Select(dir => _firstPosition.Add(dir)) .Concat(new[]{_firstPosition}) .Concat(_whiteCoords) .Concat(Direction.AllUpper.Take(3).SelectMany( dir => _whiteCoords.Select(coord => coord.Add(dir)))); }
public static IFieldsGraph ShouldHaveWhiteFieldAt(this IFieldsGraph graph, int row, int column) { var whiteField = graph.GetWhiteField(); whiteField.RowIndex.ShouldEqual(row, "Row should be {0} but was {1}"); whiteField.ColumnIndex.ShouldEqual(column, "Column should be {0} but was {1}"); return(graph); }
public WhiteStoneToCurrentPlayerBorderDistance(IPlayersState playersState, IFieldsGraph fieldsGraph, int distanceToBorderWeight) { _distanceToBorderWeight = distanceToBorderWeight; var currentPlayer = playersState.CurrentPlayer; _targetBorder = currentPlayer.GetTargetBorder(fieldsGraph); _whiteStoneToBorderDistance = new WhiteStoneToBorderDistanceValue(_targetBorder); }
public PickBestValueNodeVisitor(TargetBorder targetBorder, IFieldsGraph graphCopy, IPerformMoves performMoves) { _targetBorder = targetBorder; _valueOfGraph = new WhiteStoneToBorderDistanceValue(targetBorder); _graphCopy = graphCopy; _performMoves = performMoves; CurrentMaxValue = _valueOfGraph.GetValue(_graphCopy); MaxUpdated += () => { }; }
public PhutballMoveScore Search(IFieldsGraph fieldsGraph) { var result = _realStrategy.Search(fieldsGraph); if(result == null || result.Move == null) { return PhutballMoveScore.Empty(); } return result.FollowedBy(new DeselectWhiteFieldIfSelectedMove()); }
public PhutballBoard( IPlayersState playerState, IFieldsGraph fieldsGraph, IEventPublisher eventPublisher, IPhutballOptions options) : base(fieldsGraph, options) { _playerState = playerState; _eventPublisher = eventPublisher; }
private void InitliazeBlackBuckets(IFieldsGraph fieldsGraph) { _bestWhiteStonePostion = fieldsGraph.GetWhiteField().RowIndex; var blackFields = fieldsGraph.GetBlackFields(); _blackFields = blackFields.ToHashSet(); _blackFieldsByRowIndex = _fieldsGraph.RowCount.Times((row) => new { row, fields = new HashSet <Field>() }) .ToDictionary(item => item.row, item => item.fields); blackFields.Each(field => _blackFieldsByRowIndex[field.RowIndex].Add(field)); }
public CuttoffPickBestValueNodeVisitor(TargetBorder targetBorder, IFieldsGraph fieldsGraph, IPlayersState playersState) { _targetBorder = targetBorder; _fieldsGraph = fieldsGraph; MovesPerformer = new PerformMoves(this, playersState); PickBestValue = new PickBestValueNodeVisitor(targetBorder, fieldsGraph, MovesPerformer); InitliazeBlackBuckets(fieldsGraph); PickBestValue.MaxUpdated += OnBestPositionUpdated; _targetBorderRowEndIndex = _targetBorder.EndRowIndex; }
public PhutballMoveScore Search(IFieldsGraph fieldsGraph) { var result = _realStrategy.Search(fieldsGraph); if (result == null || result.Move == null) { return(PhutballMoveScore.Empty()); } return(result.FollowedBy(new DeselectWhiteFieldIfSelectedMove())); }
protected override IMoveFindingStartegy GetSearchEngine(IFieldsGraph graph) { options.AlphaBeta.JumpsMaxDepth = 10; options.AlphaBeta.StoneRadius = 1; options.AlphaBeta.SkipShortMoves = 1; options.AlphaBeta.SearchDepth = 3; options.AlphaBeta.BlackStonesToBorderWeight = 0; options.AlphaBeta.DistanceToBorderWeight = 1; RawMoveFinders = new RawMoveFinders(new MovesFactory(), PlayersState.FirstIsOnTheMove(), options); return(RawMoveFinders.AlphaBeta()); }
protected override void EstablishContext() { ProvideImplementationOf <IPhutballOptions>(new PhutballOptions { RowCount = 7, ColumnCount = 5 }); _fieldsGraph = new FieldsGraph(Dependency <IPhutballOptions>()); var whiteField = WhiteField(); whiteField.PlaceWhiteStone(); _fieldsGraph.UpdateFields(new[] { whiteField }); }
protected override void EstablishContext() { _fieldsGraph = GraphBuilder().Build(); _playersState = PlayersState.SecondIsOnTheMove(); _performMoves = new PerformMoves(_fieldsGraph, new NulloPlayersSwapper()); var testPhutballOptions = new PhutballOptions { RowCount = _fieldsGraph.RowCount, ColumnCount = _fieldsGraph.ColumnCount }; _moveFinders = new RawMoveFinders(new MovesFactory(), _playersState, testPhutballOptions); ProvideImplementationOf <IPhutballOptions>(testPhutballOptions); }
public int GetValue(IFieldsGraph valueSubject) { var whiteField = valueSubject.GetWhiteField(); var rawDistance = _winingBorder.GetDistanceFrom(whiteField); if(rawDistance == 0) { return _winingBorder.WinValue; } var distanceBetweenBorders = DistanceBetweenBorders(); if(rawDistance >= distanceBetweenBorders) { return _winingBorder.LooseValue; } return distanceBetweenBorders - rawDistance; }
public TargetBorderEnum(IFieldsGraph fieldsGraph) { Upper = new TargetBorder(() => 1, UpperName) .OppositeIs(() => Bottom) .CountDistanceUsing(new DistanceToUpperBorderCounter(fieldsGraph)) .ComparePositionsUsing((left,right)=> left < right) .EndRowIndexIs(me=> 1) .WiningIndexes((me)=> new[]{0,1}); Bottom = new TargetBorder(() => fieldsGraph.RowCount - 2, Bottomname) .OppositeIs(() => Upper) .CountDistanceUsing(new DistanceToBottomBorderCounter(fieldsGraph)) .EndRowIndexIs((me)=> me.RowIndex ) .ComparePositionsUsing((left, right) => left > right) .WiningIndexes((me)=> new[]{me.RowIndex, me.RowIndex + 1}); }
public TargetBorderEnum(IFieldsGraph fieldsGraph) { Upper = new TargetBorder(() => 1, UpperName) .OppositeIs(() => Bottom) .CountDistanceUsing(new DistanceToUpperBorderCounter(fieldsGraph)) .ComparePositionsUsing((left, right) => left < right) .EndRowIndexIs(me => 1) .WiningIndexes((me) => new[] { 0, 1 }); Bottom = new TargetBorder(() => fieldsGraph.RowCount - 2, Bottomname) .OppositeIs(() => Upper) .CountDistanceUsing(new DistanceToBottomBorderCounter(fieldsGraph)) .EndRowIndexIs((me) => me.RowIndex) .ComparePositionsUsing((left, right) => left > right) .WiningIndexes((me) => new[] { me.RowIndex, me.RowIndex + 1 }); }
public int GetValue(IFieldsGraph valueSubject) { var whiteField = valueSubject.GetWhiteField(); var rawDistance = _winingBorder.GetDistanceFrom(whiteField); if (rawDistance == 0) { return(_winingBorder.WinValue); } var distanceBetweenBorders = DistanceBetweenBorders(); if (rawDistance >= distanceBetweenBorders) { return(_winingBorder.LooseValue); } return(distanceBetweenBorders - rawDistance); }
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 }; }
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 }); }
public TargetBorder GetTargetBorder(IFieldsGraph fieldsGraph) { return _targetBorderAccessor(fieldsGraph); }
public BoardJumpTree(IFieldsGraph actualGraph, IPhutballMove moveToApply, ITree <JumpNode> parent) { Parent = parent; Node = new JumpNode(actualGraph, moveToApply); _jumpersFactory = new JumpersFactory(actualGraph); }
public PhutballMoveScore Search(IFieldsGraph fieldsGraph) { return _bruteForceSearch.Search(fieldsGraph); }
public RootedBySelectingWhiteFieldBoardJumpTree GetMovesTree(IFieldsGraph graphCopy) { return(new RootedBySelectingWhiteFieldBoardJumpTree(graphCopy)); }
public FieldJump(IFieldsGraph fieldsGraph, Field from, Tuple<int, int> delta) { _fieldsGraph = fieldsGraph; _from = from; _delta = delta; }
public PhutballMoveScore Search(IFieldsGraph fieldsGraph) { return(_bruteForceSearch.Search(fieldsGraph)); }
public static JumpNode Empty(IFieldsGraph graph) { return new JumpNode(graph, new EmptyPhutballMove()); }
public JumpNode(IFieldsGraph sourceGraph, IPhutballMove moveToApply) { ActualGraph = sourceGraph; LastMove = moveToApply; MovesFromRoot = new EmptyPhutballMove(); }
public DistanceToUpperBorderCounter(IFieldsGraph fieldsGraph) { _fieldsGraph = fieldsGraph; }
public DistanceToBottomBorderCounter(IFieldsGraph fieldsGraph) { _fieldsGraph = fieldsGraph; }
protected override IMoveFindingStartegy GetSearchEngine(IFieldsGraph graph) { RawMoveFinders = new RawMoveFinders(new MovesFactory(), PlayersState.SecondIsOnTheMove(), options); return(RawMoveFinders.OrderByNodesValuesWithCuttofsToWhite()); }
public RootedBySelectingWhiteFieldBoardJumpTree GetMovesTree(IFieldsGraph graphCopy) { return new RootedBySelectingWhiteFieldBoardJumpTree(graphCopy); }
public JumpersFactory(IFieldsGraph fieldsGraph) { _fieldsGraph = fieldsGraph; }
public PlayerBlackStonePlacer(IFieldsGraph fieldsGraph, IPlayersState playersState) { _fieldsGraph = fieldsGraph; _playersState = playersState; }
public ReadOnlyPhutballBoard(IFieldsGraph fieldsGraph, IPhutballOptions options) { _fieldsGraph = fieldsGraph; _options = options; }
public IEnumerable <Tuple <int, int> > BottomIsTarget(IFieldsGraph fieldsGraph) { return(_first.BottomIsTarget(fieldsGraph).Concat(_right.BottomIsTarget(fieldsGraph))); }
public IEnumerable<Tuple<int, int>> UpperIsTarget(IFieldsGraph fieldsGraph) { return _first.UpperIsTarget(fieldsGraph).Concat(_right.UpperIsTarget(fieldsGraph)); }
protected abstract IMoveFindingStartegy GetSearchEngine(IFieldsGraph graph);