/// <summary> /// Initializes a new instance of the <see cref="AsyncMethodInvokeCommand"/> class. /// </summary> /// <param name="method">The method to invoke.</param> /// <param name="expansion">An expansion which is used to transform the single value in the /// Execute method into an appropriate number of input arguments for the method.</param> /// <param name="parameterInfo">The parameter that is being sent to this method.</param> public AsyncMethodInvokeCommand( IMethod method, IExpansion <object> expansion, ParameterInfo parameterInfo) : this(method, expansion, parameterInfo, DefaultTimeout) { }
internal void SetExpansion(IExpansion expansion) { CurrentExpansion = expansion; ExpansionName.text = CurrentExpansion.Name; ExpansionPrice.text = $"{CurrentExpansion.Cost} coins"; }
/// <summary> /// Initializes a new instance of the <see cref="AsyncMethodInvokeCommand"/> class. /// </summary> /// <param name="method">The method to invoke.</param> /// <param name="expansion">An expansion which is used to transform the single value in the /// Execute method into an appropriate number of input arguments for the method.</param> /// <param name="parameterInfo">The parameter that is being sent to this method.</param> /// <param name="timeout">The timeout (in milliseconds) to wait for an async task to exit. /// </param> public AsyncMethodInvokeCommand( IMethod method, IExpansion <object> expansion, ParameterInfo parameterInfo, int timeout) : base(method, expansion, parameterInfo) { this.Timeout = timeout; }
/// <summary> /// This method executes the simulation of a game. It decides the statistics (wins and simulations) /// for a certain starting node. Based on the current board and the current player, chooses the next move /// until the end of the game. If the current player (the bot) wins, it returns 1 (1 win) and updates /// (back-propagates the results) back in the recursion stack. /// </summary> /// <param name="token">Cancellation token used to stop the execution of the thread executing this function.</param> /// <param name="b">The current board.</param> /// <param name="n">The current node, representing the board configuration.</param> /// <param name="expansion">Object representing the expansion strategy.</param> /// <param name="expandColor">The color of the next player.</param> /// <returns></returns> private int Simulate(CancellationTokenSource token, Board b, Node n, IExpansion expansion, Color expandColor) { //Console.WriteLine("Expand " + expandColor.ToString()); int res = 0; if (token.IsCancellationRequested == false) { /* check if game is finished. */ if (b.IsFinished(thisPlayer) == true || b.IsFinished(opponent) == true || (b.GetScore(1) + b.GetScore(2) == 64) || b.PlayerHasPossibleMoves(thisPlayer) == false || b.PlayerHasPossibleMoves(opponent) == false) { /* set this node as terminal. */ n.expandable = false; /* find the game result. */ if (b.GetScore(1) == b.GetScore(2)) { res = 0; } else if (b.GetScore(1) > b.GetScore(2)) { /* player 1(black) won. */ res = (thisPlayer.GetColor() == Color.black) ? 1 : -1; } else { /* player 2(white) won. */ res = (thisPlayer.GetColor() == Color.black) ? -1 : 1; } } else { /* continue simulating. */ /* expand, if necessary. */ expansion.SetBoard(b); List <Node> chl = expansion.Expand(n, expandColor); expandColor = SwitchColor(expandColor); /* choose random child. */ Node nextNode = chl[rand.Next(chl.Count)]; /* update board. */ Piece pct = new Piece(nextNode.X, nextNode.Y, GetOwner(b, n)); if (b.pieces[nextNode.X, nextNode.Y] != null) { throw new MCTSException("next node is not null!"); } PlacePieceOnBoard(b, pct); /* simulate. */ res = Simulate(token, b, nextNode, expansion, expandColor); /* update node statistics if cancellation is not set. */ if (res != -2) { n.IncWins(res); /* res could be -1, 0 or 1. */ n.IncVisits(1); } } } else { /* cancel request while simulating. */ res = -2; } return(res); /* return result. */ }
public MonteCarloTreeSearch(ECultures cult, ISelection selection, ISimulation simulation, Game game) { GameTree = new Node(0, game.GetBoards(), null); CurGame = game; Selection = selection; Simulation = simulation; Expansion = new ExpandAll(); Culture = cult; Cursor = new Coord(1, 1); Pawns = new List <APawn>(); CultCenter = null; }
public void ExpansionIsCorrect() { // Arrange var dummyMethod = new DelegatingMethod(); var expansion = new DelegatingExpansion <object>(); var dummyParameter = MethodInvokeCommandTest.CreateAnonymousParameterInfo(); var sut = new MethodInvokeCommand(dummyMethod, expansion, dummyParameter); // Act IExpansion <object> result = sut.Expansion; // Assert Assert.Equal(expansion, result); }
public void ExpansionIsCorrect() { // Fixture setup var dummyMethod = new DelegatingMethod(); var expansion = new DelegatingExpansion <object>(); var dummyParameter = MethodInvokeCommandTest.CreateAnonymousParameterInfo(); var sut = new MethodInvokeCommand(dummyMethod, expansion, dummyParameter); // Exercise system IExpansion <object> result = sut.Expansion; // Verify outcome Assert.Equal(expansion, result); // Teardown }
private static IGuardClauseCommand CreateWrappedMethodInvokeCommand( IMethod method, IExpansion <object> expansion) { var testHelperMethod = typeof(GuardClauseExtensionsTests) .GetMethod( nameof(StaticCommandHelperMethod), BindingFlags.Static | BindingFlags.NonPublic); return(new ReflectionExceptionUnwrappingCommand(new MethodInvokeCommand( method, expansion, testHelperMethod.GetParameters().First()))); }
/* constructors. */ public RandomSimulation(IMCTSPlayer thisPlayer, IMCTSPlayer opponent, Board b, string expansion, string backPropagation) { board = new Board(b); this.thisPlayer = thisPlayer; this.opponent = opponent; wins = visits = 0; exp = ExpansionFactory.Create(expansion); bp = BackPropagationFactory.Create(backPropagation); rand = new Random(); workers = new List <Thread>(threadNo); mutex = new Mutex(); isSimulating = false; root = null; treeRoot = null; }
/// <summary> /// Initializes a new instance of the <see cref="MethodInvokeCommand"/> class. /// </summary> /// <param name="method">The method to invoke.</param> /// <param name="expansion"> /// An expansion which is used to transform the single value in the Execute method into an /// appropriate number of input arguments for the method. /// </param> /// <param name="parameterInfo">The parameter.</param> public MethodInvokeCommand(IMethod method, IExpansion<object> expansion, ParameterInfo parameterInfo) { this.method = method; this.expansion = expansion; this.parameterInfo = parameterInfo; }
/// <summary> /// Remove expansion from object /// </summary> /// <param name="expansion">Expansion to remove</param> public void RemoveExpansion(IExpansion expansion) { expansion.onDestroy(); expansions.Remove(expansion); }
/// <summary> /// Add a new expansion /// </summary> /// <param name="expansion">Expansion to add</param> public void AddExpansion(IExpansion expansion) { expansion.parent = this; expansion.OnCreate(); expansions.Add(expansion); }
/// <summary> /// Initializes a new instance of the <see cref="MethodInvokeCommand"/> class. /// </summary> /// <param name="method">The method to invoke.</param> /// <param name="expansion"> /// An expansion which is used to transform the single value in the Execute method into an /// appropriate number of input arguments for the method. /// </param> /// <param name="parameterInfo">The parameter.</param> public MethodInvokeCommand(IMethod method, IExpansion <object> expansion, ParameterInfo parameterInfo) { this.method = method; this.expansion = expansion; this.parameterInfo = parameterInfo; }