public void AddNewMember(GeneticIndividual individual, TicTacToeContext context) { var newIndividual = new GeneticIndividual { GenerationNumber = individual.GenerationNumber, ImportanceOrder = 10, PlayedGames = 0, Score = 0, }; context.Set <GeneticIndividual>().Add(newIndividual); context.SaveChanges(); var factors = context.Set <GeneticFactor>() .Where(factor => factor.GeneticIndividualId == individual.GeneticIndividualId) .ToList(); var newFactors = factors.Select(factor => new GeneticFactor { GeneticIndividualId = newIndividual.GeneticIndividualId, Factor = this.random.Next(0, 100), FieldId = factor.FieldId }).ToList(); context.Set <GeneticFactor>().AddRange(newFactors); context.SaveChanges(); }
public async Task <Player> GetPlayer(ulong discordId, ulong guildId) { using var context = new TicTacToeContext(_options); return(await context.Players .Where(x => x.GuildId == guildId).FirstOrDefaultAsync(x => x.UserId == discordId)); }
public async Task <Player> GetOrCreatePlayer(ulong discordId, ulong guildId) { using var context = new TicTacToeContext(_options); var profile = await context.Players .Where(x => x.GuildId == guildId).FirstOrDefaultAsync(x => x.UserId == discordId); if (profile != null) { return(profile); } profile = new Player { UserId = discordId, GuildId = guildId, isBanned = false, Wins = 0, Losses = 0, Abandons = 0 }; context.Add(profile); await context.SaveChangesAsync(); return(profile); }
public Coordinates GetStep(CellCondition[,] gameField, TicTacToeContext context) { var allPossibleSteps = this.possibleStepsProvider.GetPossibleSteps(gameField); var individual = context.GetModel <GeneticIndividual>() .FirstOrDefault(geneticIndividual => geneticIndividual.ImportanceOrder == 0); var factor = -1.0; Coordinates nextStep = null; foreach (var step in allPossibleSteps) { var currentField = this.stepMaker.MakeStep(gameField, step.X, step.Y); var currentFieldCode = this.fieldStateConverter.GameFieldToString(currentField); var field = GameHelper.GetFieldByCode(currentFieldCode, context); var individualFactor = context.Set <GeneticFactor>() .FirstOrDefault(geneticFactor => geneticFactor.Field.FieldId == field.FieldId && geneticFactor.GeneticIndividual.GeneticIndividualId == individual.GeneticIndividualId); var currentFactor = individualFactor.Factor; if (currentFactor > factor) { factor = currentFactor; nextStep = step; } } return(nextStep); }
public GameProcessStatistic MakeMonkeyBotStep(int gameId, TicTacToeContext context) { var game = context.Set <Game>().Include(game1 => game1.Field).FirstOrDefault(game1 => game1.GameId == gameId); var field = game.Field; var fieldCode = GameHelper.GetFieldByNumber(game.FieldNumber, field); var gameField = this.fieldStateConverter.StringToGameField(fieldCode); var nextStep = this.monkeyBot.GetStep(gameField); var nextGameField = this.stepMaker.MakeStep(gameField, nextStep.X, nextStep.Y); var nextFieldCode = this.fieldStateConverter.GameFieldToString(nextGameField); var nextField = GameHelper.GetFieldByCode(nextFieldCode, context); var nextNumber = GameHelper.GetCodeNumber(nextFieldCode, nextField); var result = this.gameProcessStatisticProvider.GetGameProcessStatistic(nextGameField); game.Field = nextField; game.FieldNumber = nextNumber; game.Proccess += "|" + nextField.FieldId; context.Set <Game>().AddOrUpdate(game); context.SaveChanges(); return(result); }
public PlayerVsMonkeyBotWinGameCommandResult ExecuteCommand(PlayerVsMonkeyBotWinGameCommand command) { PlayerVsMonkeyBotWinGameCommandResult result; using (var context = new TicTacToeContext()) { var game = context.Set <Game>().Include(game1 => game1.Field).FirstOrDefault(game1 => game1.GameId == command.GameId); var field = game.Field; var fieldCode = GameHelper.GetFieldByNumber(game.FieldNumber, field); var gameField = this.fieldStateConverter.StringToGameField(fieldCode); var gameProcessStatistic = this.gameProcessStatisticProvider.GetGameProcessStatistic(gameField); var winCoordinates = GameHelper.GetWinCoordinates(gameProcessStatistic); GameHelper.RefreshStatistic(game.GameId, gameProcessStatistic.GameStatus, context); context.Set <Game>().Remove(game); context.SaveChanges(); result = new PlayerVsMonkeyBotWinGameCommandResult { CellSize = GameFieldConstants.LineLength, GameField = gameField, WinCoordinates = winCoordinates }; } return(result); }
public PlayerVsMonkeyBotNewGameCommandResult ExecuteCommand(PlayerVsMonkeyBotNewGameCommand command) { PlayerVsMonkeyBotNewGameCommandResult result; using (var context = new TicTacToeContext()) { var gameField = this.newGameFieldCreator.CreateNewGameField(); var fieldCode = this.fieldStateConverter.GameFieldToString(gameField); var field = GameHelper.GetFieldByCode(fieldCode, context); var number = GameHelper.GetCodeNumber(fieldCode, field); var game = new Game { Field = field, FieldNumber = number, Proccess = field.FieldId.ToString() }; context.Set <Game>().Add(game); context.SaveChanges(); var gameId = game.GameId; result = new PlayerVsMonkeyBotNewGameCommandResult { GameField = gameField, GameId = gameId, CellSize = GameFieldConstants.LineLength, IsBotTurn = random.Next(2) == 0 }; } return(result); }
public void AddMutations(List <GeneticIndividual> winners, TicTacToeContext context) { foreach (var winner in winners) { this.AddMutation(winner, context); } }
public PlayerVsPlayerTakeDrawCommandResult ExecuteCommand(PlayerVsPlayerTakeDrawCommand command) { PlayerVsPlayerTakeDrawCommandResult result; using (var context = new TicTacToeContext()) { var game = context.Set <Game>().Include(game1 => game1.Field).FirstOrDefault(game1 => game1.GameId == command.GameId); var field = game.Field; var fieldCode = GameHelper.GetFieldByNumber(game.FieldNumber, field); var gameField = this.fieldStateConverter.StringToGameField(fieldCode); GameHelper.RefreshStatistic(game.GameId, GameStatus.Draw, context); context.Set <Game>().Remove(game); context.SaveChanges(); result = new PlayerVsPlayerTakeDrawCommandResult { CellSize = GameFieldConstants.LineLength, GameField = gameField, }; } return(result); }
public static Field GetFieldByCode(string fieldCode, TicTacToeContext context) { return(context.Set <Field>().Include(field => field.FieldStatistic).FirstOrDefault(field1 => field1.FirstVariant == fieldCode || field1.SecondVariant == fieldCode || field1.ThirdVariant == fieldCode || field1.FourthVariant == fieldCode)); }
public async Task CreateNewPlayerAsync(Player player) { using var context = new TicTacToeContext(_options); context.Add(player); await context.SaveChangesAsync(); }
public PlayerVsMonkeyBotMakeStepCommandResult ExecuteCommand(PlayerVsMonkeyBotMakeStepCommand command) { PlayerVsMonkeyBotMakeStepCommandResult result; using (var context = new TicTacToeContext()) { var game = context.Set <Game>().Include(game1 => game1.Field).FirstOrDefault(game1 => game1.GameId == command.GameId); var field = game.Field; var fieldCode = GameHelper.GetFieldByNumber(game.FieldNumber, field); var gameField = this.fieldStateConverter.StringToGameField(fieldCode); CellCondition[,] nextGameField; try { nextGameField = this.stepMaker.MakeStep(gameField, command.X, command.Y); } catch (Exception) { var previousProcessStatistic = this.gameProcessStatisticProvider.GetGameProcessStatistic(gameField); return(new PlayerVsMonkeyBotMakeStepCommandResult { CellSize = GameFieldConstants.LineLength, GameId = command.GameId, GameField = gameField, GameProcessStatistic = previousProcessStatistic, }); } var nextFieldCode = this.fieldStateConverter.GameFieldToString(nextGameField); var nextField = GameHelper.GetFieldByCode(nextFieldCode, context); var nextNumber = GameHelper.GetCodeNumber(nextFieldCode, nextField); var gameProcessStatistic = this.gameProcessStatisticProvider.GetGameProcessStatistic(nextGameField); game.Field = nextField; game.FieldNumber = nextNumber; game.Proccess += "|" + nextField.FieldId; context.Set <Game>().AddOrUpdate(game); context.SaveChanges(); result = new PlayerVsMonkeyBotMakeStepCommandResult { CellSize = GameFieldConstants.LineLength, GameId = command.GameId, GameField = nextGameField, GameProcessStatistic = gameProcessStatistic }; } return(result); }
public async Task UpdatePlayer(Player player) { using var context = new TicTacToeContext(_options); //Player gotPlayer = await context.Players.FirstOrDefaultAsync(x => x.Id == player.Id); //gotPlayer = player; context.Update(player); await context.SaveChangesAsync(); }
public PlayerService(TicTacToeContext dbContext) { if (dbContext == null) { throw new ArgumentNullException("dbContext"); } _dbContext = dbContext; _mapConfig = new MapperConfiguration(mc => { mc.CreateMap <Player, PlayerDTO>().ReverseMap(); }); _mapper = _mapConfig.CreateMapper(); }
public List <Player> GetPlayersByUserId(ulong id) { using var context = new TicTacToeContext(_options); var players = context.Players.Where(x => x.UserId == id); List <Player> toret = new List <Player>(); foreach (var player in players) { toret.Add(player); } return(toret); }
public void CreateNextGeneration() { IKernel kernel = new StandardKernel(new TicTacToeNinjectModule(), new MVCNinjectModule()); var developer = kernel.Get <IGeneticBotDeveloper>(); for (var i = 0; i < 5; i++) { using (var context = new TicTacToeContext()) { developer.GenerateNextGeneration(context); } } }
public void TakeDraw(int gameId, int geneticIndividualId, TicTacToeContext context) { var game = context.Set <Game>().Include(game1 => game1.Field).FirstOrDefault(game1 => game1.GameId == gameId); GameHelper.RefreshStatistic(game.GameId, GameStatus.Draw, context); context.Set <Game>().Remove(game); context.SaveChanges(); var individual = context.Set <GeneticIndividual>() .FirstOrDefault(geneticIndividual => geneticIndividual.GeneticIndividualId == geneticIndividualId); individual.Score += 0.5; individual.PlayedGames++; context.Set <GeneticIndividual>().AddOrUpdate(individual); context.SaveChanges(); }
public int CreateNewGame(TicTacToeContext context) { var gameField = this.newGameFieldCreator.CreateNewGameField(); var fieldCode = this.fieldStateConverter.GameFieldToString(gameField); var field = GameHelper.GetFieldByCode(fieldCode, context); var number = GameHelper.GetCodeNumber(fieldCode, field); var game = new Game { Field = field, FieldNumber = number, Proccess = field.FieldId.ToString() }; context.Set <Game>().Add(game); context.SaveChanges(); var gameId = game.GameId; return(gameId); }
static void Main(string[] args) { using (var context = new TicTacToeContext(ConfigurationService.ConnectionString)) { Providers.ServiceProvider = new TicTacToeServiceProvider(context); int numberOfTimes; Console.WriteLine("Number of times"); string strNumberOfTimes = Console.ReadLine(); int.TryParse(strNumberOfTimes, out numberOfTimes); Console.WriteLine("Player 1"); IPlayer player1 = TranslateStringToPlayer(Console.ReadLine(), 0); Console.WriteLine("Player 2"); IPlayer player2 = TranslateStringToPlayer(Console.ReadLine(), 1); IPlayer winner; for (int i = 0; i < numberOfTimes; i++) { IGame game = new TicTacToeGame(player1, player2); while (!game.IsGameOver()) { game.NextPlayer(); game.Move(); } // foreach (var item in game.Moves) // { // Console.WriteLine(item); // } GameHelper.PrintMoves(game.Moves); Console.WriteLine($"Winner number: {game.Winner?.PlayerNumber}"); winner = game.Winner; foreach (IPlayer player in game.Players) { player.AfterGameFinished(game); } } } }
public static void FillDb() { using(TicTacToeContext db = new TicTacToeContext()) { Player human = new Player { Age = 20, Email = "*****@*****.**", Name = "Stanly" }; Player Ai = new Player { Age = 100, Email = "*****@*****.**", Name = "Brainston" }; db.Players.AddRange(new List<Player> { human, Ai }); db.SaveChanges(); } }
public void GenerateNextGeneration(TicTacToeContext context) { var indivadualIds = this.GetIndividualIds(context); foreach (var indivadualId in indivadualIds) { var isCross = true; for (var gameNumber = 0; gameNumber < 50; gameNumber++) { var gameId = this.CreateNewGame(context); var isGeneticBotTurn = isCross; GameProcessStatistic statistic = null; do { statistic = isGeneticBotTurn ? this.MakeGeneticBotStep(gameId, context) : this.MakeMonkeyBotStep(gameId, context); isGeneticBotTurn = !isGeneticBotTurn; }while (statistic.GameStatus == GameStatus.InProgress); if (statistic.GameStatus == GameStatus.Draw) { this.TakeDraw(gameId, indivadualId, context); } else { this.Win(gameId, indivadualId, isCross, context); } isCross = !isCross; } } var winners = this.TakeThreeWinners(context); this.AddMutations(winners, context); this.AddIntersections(winners, context); this.AddNewMember(winners[0], context); }
static void Main(string[] args) { var contextOptions = new DbContextOptionsBuilder <TicTacToeContext>() .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=TicTacToe;Trusted_Connection=True;") .Options; TicTacToeContext context = new TicTacToeContext(contextOptions); IPlayerService playerService = new PlayerService(context); IGameService gameService = new GameService(context, new GameBot()); var player = playerService.GetPlayerInfo(); //var game = gameService.StartGame(player.Id); //game = gameService.MakeMove(player.Id, game.Id, 4); //game = gameService.MakeMove(player.Id, game.Id, 1); //game = gameService.MakeMove(player.Id, game.Id, 5); //game = gameService.MakeMove(player.Id, game.Id, 8); ////game = gameService.MakeMove(player.Id, game.Id, 8); //game = gameService.MakeMove(player.Id, game.Id, 0); }
public GameService(TicTacToeContext dbContext, IGameBot gameBot) { if (dbContext == null) { throw new ArgumentNullException("dbContext"); } if (gameBot == null) { throw new ArgumentNullException("gameBot"); } _dbContext = dbContext; _gameBot = gameBot; _mapConfig = new MapperConfiguration(mc => { mc.CreateMap <Game, GameDTO>().ReverseMap(); mc.CreateMap <Player, PlayerDTO>().ReverseMap(); mc.CreateMap <Movement, MovementDTO>().ReverseMap(); mc.CreateMap <GamePlayer, GamePlayerDTO>().ForMember(gp => gp.Piece, opt => opt.MapFrom(gp => gp.Piece == "X" ? Pieces.X : Pieces.O)); mc.CreateMap <GamePlayerDTO, GamePlayer>().ForMember(gp => gp.Piece, opt => opt.MapFrom(gp => gp.Piece.ToString())); }); _mapper = _mapConfig.CreateMapper(); }
public List <GeneticIndividual> TakeThreeWinners(TicTacToeContext context) { var individuals = context.Set <GeneticIndividual>() .OrderByDescending(individual => individual.Score / individual.PlayedGames) .ToList(); var result = individuals.OrderByDescending(individual => individual.Score / individual.PlayedGames).Take(3).ToList(); for (var order = 0; order < result.Count; order++) { result[order].ImportanceOrder = order; result[order].GenerationNumber++; context.Set <GeneticIndividual>().AddOrUpdate(result[order]); } var individualsToRemove = individuals.Except(result).ToList(); foreach (var individual in individualsToRemove) { var factors = context.Set <GeneticFactor>() .Where(factor => factor.GeneticIndividualId == individual.GeneticIndividualId) .ToList(); foreach (var factor in factors) { context.Set <GeneticFactor>().Remove(factor); } context.Set <GeneticIndividual>().Remove(individual); } context.SaveChanges(); return(result); }
public Coordinates GetStep(CellCondition[,] gameField, TicTacToeContext context) { var allPossibleSteps = this.possibleStepsProvider.GetPossibleSteps(gameField); var factor = -1.0; Coordinates nextStep = null; foreach (var step in allPossibleSteps) { var currentField = this.stepMaker.MakeStep(gameField, step.X, step.Y); var currentFieldCode = this.fieldStateConverter.GameFieldToString(currentField); var field = GameHelper.GetFieldByCode(currentFieldCode, context); var currentFactor = this.CalculateFactor(field.FieldStatistic.PlayedGames, field.FieldStatistic.Score); if (currentFactor > factor) { factor = currentFactor; nextStep = step; } } return(nextStep); }
public void AddMutation(GeneticIndividual individual, TicTacToeContext context) { var newIndividual = new GeneticIndividual { GenerationNumber = individual.GenerationNumber, ImportanceOrder = 4, PlayedGames = 0, Score = 0, }; context.Set <GeneticIndividual>().Add(newIndividual); context.SaveChanges(); var factors = context.Set <GeneticFactor>() .Where(factor => factor.GeneticIndividualId == individual.GeneticIndividualId) .ToList(); var newFactors = factors.Select(factor => new GeneticFactor { GeneticIndividualId = newIndividual.GeneticIndividualId, Factor = this.winFactors.ContainsKey(factor.FieldId) ? factor.Factor + this.winFactors[factor.FieldId] * 5 : factor.Factor + this.random.Next(11) - 5, FieldId = factor.FieldId }) .Select(factor => new GeneticFactor { GeneticIndividualId = factor.GeneticIndividualId, Factor = factor.Factor > 100 ? 100 : factor.Factor < 0 ? 0 : factor.Factor, FieldId = factor.FieldId }).ToList(); context.Set <GeneticFactor>().AddRange(newFactors); context.SaveChanges(); }
public PlayfieldStatesService(TicTacToeContext context) { this.context = context; }
public EntityBaseRepository(TicTacToeContext context) { _context = context; }
public TicTacToeService(TicTacToeContext context) { _context = context; }
public EfUnitOfWork(string connectionString) { _db = new TicTacToeContext(connectionString); }
public PlayerRepository(TicTacToeContext db) { _db = db; }