/// <summary> /// Collects types decorated with <see cref="AddToServicesAttribute"/> and user the <paramref name="applier"/> to apply it. /// </summary> /// <param name="applier">The applier to use.</param> /// <param name="tag">The tag to collect.</param> /// <exception cref="System.ArgumentNullException"><paramref name="applier"/> is null.</exception> public void Collect(IApplier applier, string tag = null) { if (applier == null) { throw new ArgumentNullException(nameof(applier)); } var implementations = Assemblies .SelectMany(a => a.ExportedTypes) .Where(t => t.GetTypeInfo() .CustomAttributes .Any(cd => cd.AttributeType == typeof(AddToServicesAttribute))); foreach (var implementation in implementations) { var attributes = implementation.GetTypeInfo().GetCustomAttributes <AddToServicesAttribute>(); foreach (var attribute in attributes) { if ((attribute.InternalTags == null && tag != null) || (attribute.InternalTags != null && tag == null) || (attribute.InternalTags != null && tag != null && !attribute.InternalTags.Any( t => t.Equals(tag, StringComparison.OrdinalIgnoreCase)))) { continue; } var context = new ApplierContext(implementation, attribute); applier.Apply(context); } } }
/// <summary> /// Creates a minimax average algorithm object. /// </summary> /// <param name="evaluator">The evaluator for the nodes.</param> /// <param name="moveGenerator">The move generator for the nodes.</param> /// <param name="applier">The move applier for the moves.</param> public MinimaxAverageAlgorithm(IEvaluator <TState> evaluator, IGenerator <TState, TMove> moveGenerator, IApplier <TState, TMove> applier) { Evaluator = evaluator; MoveGenerator = moveGenerator; MoveApplier = applier; MaxDepthInnerValue = 3; }
/// <summary> /// Collects types decorated with <see cref="AddToServicesAttribute"/> and user the <paramref name="applier"/> to apply it. /// </summary> /// <param name="applier">The applier to use.</param> /// <param name="tag">The tag to collect.</param> /// <exception cref="ArgumentNullException"><paramref name="applier"/> is null.</exception> public void Collect(IApplier applier, string tag = null) { if (applier == null) { throw new ArgumentNullException(nameof(applier)); } var pairs = _provider.GetAttributes(); foreach (var pair in pairs) { var implementation = pair.Implementation; var attributes = pair.Attributes; foreach (var attribute in attributes) { if ((attribute.InternalTags == null && tag != null) || (attribute.InternalTags != null && tag == null) || (attribute.InternalTags != null && tag != null && !attribute.InternalTags.Any( t => t.Equals(tag, StringComparison.OrdinalIgnoreCase)))) { continue; } var service = ValidateService(implementation, attribute); var context = new ApplierContext(service, implementation, attribute.ForwardTo, attribute.Lifetime); applier.Apply(context); } } }
public AlphaBetaAlgorithm(IEvaluator <TState> evaluator, IGenerator <TState, TMove> moveGenerator, IApplier <TState, TMove> applier) { _evaluator = evaluator; _moveGenerator = moveGenerator; _moveApplier = applier; _maxDepth = 3; }
public GreedyAlgorithm(IEvaluator <TState> evaluator, IGenerator <TState, TMove> moveGenerator, IApplier <TState, TMove> applier) { _evaluator = evaluator; _moveGenerator = moveGenerator; _moveApplier = applier; }
/// <summary> /// Creates a minimax algorithm object. /// (Which is the same as hte minimax average with the number of nodes average calculations set to 1. /// </summary> /// <param name="evaluator">The evaluator for the nodes.</param> /// <param name="moveGenerator">The move generator for the nodes.</param> /// <param name="applier">The move applier for the moves.</param> public MinimaxAlgorithm(IEvaluator <TState> evaluator, IGenerator <TState, TMove> moveGenerator, IApplier <TState, TMove> applier) : base(evaluator, moveGenerator, applier) { MaxLevelAverageDepthInnerValue = 1; MinLevelAverageDepthInnerValue = 1; }
public MainForm() { game = new ChessRepresentationInitializer().Create(); _mechanism = new ChessMechanism(true); _evaluator = new Evaluator(_mechanism); _generator = new MoveGenerator(_mechanism); _applier = new MoveApplier(_mechanism); taskRight = new Task(() => { do { if (!_isActive || _mechanism.GetGameState(game) != GameState.InProgress) { InvokeIfRequired(progressbarBotActiveRight, () => { progressbarBotActiveRight.MarqueeAnimationSpeed = 0; }); // Not doing anything, checking in every second... Thread.Sleep(1000); continue; } InvokeIfRequired(progressbarBotActiveRight, () => { if (progressbarBotActiveRight.MarqueeAnimationSpeed != 10) { progressbarBotActiveRight.MarqueeAnimationSpeed = 10; } }); DoRobotWork(ChessPlayer.White); Thread.Sleep(1000); } while (true); }); taskLeft = new Task(() => { do { if (!_isActive || _mechanism.GetGameState(game) != GameState.InProgress) { InvokeIfRequired(progressbarBotActiveLeft, () => { progressbarBotActiveLeft.MarqueeAnimationSpeed = 0; }); // Not doing anything, checking in every second... Thread.Sleep(1000); continue; } InvokeIfRequired(progressbarBotActiveLeft, () => { if (progressbarBotActiveLeft.MarqueeAnimationSpeed != 10) { progressbarBotActiveLeft.MarqueeAnimationSpeed = 10; } }); DoRobotWork(ChessPlayer.Black); Thread.Sleep(1000); } while (true); }); InitializeComponent(); listboxAlgorithmsRight.Items.Add(Algorithms.Minimax); listboxAlgorithmsRight.Items.Add(Algorithms.MinimaxAverage); listboxAlgorithmsRight.Items.Add(Algorithms.AlphaBeta); listboxAlgorithmsRight.Items.Add(Algorithms.Greedy); listboxAlgorithmsRight.Items.Add(Algorithms.Random); listboxAlgorithmsRight.SelectedIndex = Randomizer.Next(0, listboxAlgorithmsRight.Items.Count - 1); listboxAlgorithmsLeft.Items.Add(Algorithms.Minimax); listboxAlgorithmsLeft.Items.Add(Algorithms.MinimaxAverage); listboxAlgorithmsLeft.Items.Add(Algorithms.AlphaBeta); listboxAlgorithmsLeft.Items.Add(Algorithms.Greedy); listboxAlgorithmsLeft.Items.Add(Algorithms.Random); listboxAlgorithmsLeft.SelectedIndex = Randomizer.Next(0, listboxAlgorithmsLeft.Items.Count - 1); chessBoardVisualizerPanel1.ChessRepresentation = game; chessBoardVisualizerPanel1.Refresh(); taskLeft.Start(); taskRight.Start(); }