public ActionResult MakeStatisticBotStep(PlayerVsStatisticBotMakeBotStepCommand command)
        {
            var answer = this.playerVsStatisticBotCommandHandler.ExecuteCommand(command);

            if (answer.GameProcessStatistic.GameStatus != GameStatus.InProgress)
            {
                if (answer.GameProcessStatistic.GameStatus == GameStatus.Draw)
                {
                    return(RedirectToAction("TakeDraw", new PlayerVsMonkeyBotTakeDrawCommand {
                        GameId = command.GameId
                    }));
                }
                else
                {
                    return(RedirectToAction("WinGame", new PlayerVsMonkeyBotWinGameCommand {
                        GameId = command.GameId, Message = "Statistic bot won"
                    }));
                }
            }

            return(this.View(answer));
        }
Beispiel #2
0
        public PlayerVsStatisticBotMakeBotStepCommandResult ExecuteCommand(PlayerVsStatisticBotMakeBotStepCommand command)
        {
            PlayerVsStatisticBotMakeBotStepCommandResult 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 nextStep      = statisticBot.GetStep(gameField, context);
                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 gameProcessStatistic = this.gameProcessStatisticProvider.GetGameProcessStatistic(nextGameField);

                game.Field       = nextField;
                game.FieldNumber = nextNumber;
                game.Proccess   += "|" + nextField.FieldId;
                context.Set <Game>().AddOrUpdate(game);
                context.SaveChanges();

                result = new PlayerVsStatisticBotMakeBotStepCommandResult
                {
                    CellSize             = GameFieldConstants.LineLength,
                    GameId               = command.GameId,
                    GameField            = nextGameField,
                    GameProcessStatistic = gameProcessStatistic,
                    BotStep              = nextStep
                };
            }

            return(result);
        }