public async Task Winner_Score_Is_Total_Score()
        {
            var strategyRepository   = new StrategyRepository(connection);
            var strategyService      = new StrategyService(strategyRepository);
            var simulationRepository = new SimulationRepository(connection);
            var gameService          = new GameService(strategyService, gameSettingsProvider);
            var populationService    = new PopulationService(gameService);
            //TODO: simplify /\
            var simulationServce = new SimulationService(simulationRepository, populationService,
                                                         strategyService, new SimulationSettingsProvider());
            Strategy cooperator = await strategyRepository
                                  .GetByNameAsync(NoMemoryStrategies.GetSimpleCooperator().Name);

            var players = new List <Player>();

            for (int i = 0; i < 10; i++)
            {
                players.Add(new Player()
                {
                    StrategyId = cooperator.Id
                });
            }

            Simulation simulation = await simulationServce.Run(players);

            GameSettings gameSettings = gameSettingsProvider.GetGameSettings();
            int          totalScore   = gameSettings.TotalRounds
                                        * (gameSettings.CooperateModifier + gameSettings.MoveModifier)
                                        * (players.Count - 1);

            Assert.AreEqual(totalScore, simulation.Winner.Score);
        }
        public async Task Run_Once_When_Consistent()
        {
            var strategyRepository   = new StrategyRepository(connection);
            var strategyService      = new StrategyService(strategyRepository);
            var simulationRepository = new SimulationRepository(connection);
            var gameService          = new GameService(strategyService, gameSettingsProvider);
            var populationService    = new PopulationService(gameService);
            //TODO: simplify /\
            var simulationServce = new SimulationService(simulationRepository, populationService,
                                                         strategyService, new SimulationSettingsProvider());
            Strategy cooperator = await strategyRepository
                                  .GetByNameAsync(NoMemoryStrategies.GetSimpleCooperator().Name);

            var players = new List <Player>();

            for (int i = 0; i < 10; i++)
            {
                players.Add(new Player()
                {
                    StrategyId = cooperator.Id
                });
            }

            Simulation simulation = await simulationServce.Run(players);

            Assert.IsNotNull(simulation.Winner);
            Assert.AreEqual(1, simulation.PopulationsCompleated);
        }
Пример #3
0
        public async Task No_Winner_If_Not_Consistent()
        {
            string strategyId = Guid.NewGuid().ToString();
            var    simulationRepositoryMock = new Mock <ISimulationRepository>();
            var    strategyServiceMock      = new Mock <IStrategyService>();
            var    populationServiceMock    = new Mock <IPopulationService>();
            var    settingsProviderMock     = new Mock <ISimulationSettingsProvider>();

            populationServiceMock.Setup(x => x.IsPopulationConsistent(It.IsAny <Population>()))
            .Returns(Task.FromResult(false));

            populationServiceMock.Setup(x => x.Evaluate(It.IsAny <List <Player> >()))
            .Returns((List <Player> p) => Task.FromResult(new Population()
            {
                Players = p
            }));

            populationServiceMock.Setup(x => x.GetNewPopulation(It.IsAny <Population>()))
            .Returns((Population p) => Task.FromResult(new Population()
            {
                Players = p.Players
            }));

            strategyServiceMock.Setup(x => x.GetStrategiesById(It.IsAny <List <string> >()))
            .Returns(new List <Strategy>()
            {
                GetCoopStrategy(strategyId)
            });

            settingsProviderMock.Setup(x => x.GetSimulationSettings())
            .Returns(new SimulationSettings()
            {
                PoplationsLimit = 10
            });

            List <Player> players = new List <Player>();

            for (int i = 0; i < 10; i++)
            {
                players.Add(new Player()
                {
                    StrategyId = strategyId
                });
            }

            SimulationService simulationService = new SimulationService(simulationRepositoryMock.Object,
                                                                        populationServiceMock.Object, strategyServiceMock.Object, settingsProviderMock.Object);

            Simulation simulation = await simulationService.Run(players);

            Assert.IsNull(simulation.Winner);
        }
Пример #4
0
        public async Task Throw_Exception_When_No_Players()
        {
            var simulationRepositoryMock = new Mock <ISimulationRepository>();
            var strategyServiceMonk      = new Mock <IStrategyService>();
            var populationServiceMock    = new Mock <IPopulationService>();
            var settingsProviderMock     = new Mock <ISimulationSettingsProvider>();

            SimulationService simulationService = new SimulationService(simulationRepositoryMock.Object,
                                                                        populationServiceMock.Object, strategyServiceMonk.Object, settingsProviderMock.Object);

            var ex = Assert.ThrowsExceptionAsync <ArgumentNullException>(() => simulationService.Run(new List <Player>()));

            Assert.IsNotNull(ex);
        }