Пример #1
0
        public IHttpActionResult Play(PlayRequestDataModel request)
        {
            var currentUserId = this.User.Identity.GetUserId();

            if (request == null || !ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var gameIdAsGuid = new Guid(request.GameId);

            var game = this.data.Games.Find(gameIdAsGuid);

            if(game == null)
            {
                return this.BadRequest("Invalida game id!");
            }

            if(game.State == GameState.Draw ||
                game.State == GameState.FirstPlayerWon ||
                game.State == GameState.SecondPlayerWon)
            {
                return BadRequest("The game has ended!");
            }

            if(game.State != GameState.TurnO && game.State != GameState.TurnX)
            {
                return this.BadRequest("Invalid game state!");
            }

            if(game.FirstPlayerId != currentUserId && game.SecondPlayerId != currentUserId)
            {
                return this.BadRequest("This is not your game!");
            }

            if((game.State == GameState.TurnX && currentUserId != game.FirstPlayerId) ||
                game.State == GameState.TurnO && currentUserId != game.SecondPlayerId)
            {
                return this.BadRequest("It is not your turn!");
            }

            var positionIndex = (request.Row - 1) * 3 + request.Col - 1;
            if(game.Board[positionIndex] != '-')
            {
                return this.BadRequest("Invalid position!");
            }

            var gameBoardAsStringBuilder = new StringBuilder(game.Board);
            gameBoardAsStringBuilder[positionIndex] =
                game.State == GameState.TurnO ? 'O' : 'X';
            game.Board = gameBoardAsStringBuilder.ToString();

            game.State = (game.State == GameState.TurnO ? GameState.TurnX : GameState.TurnO);

            this.data.SaveChanges();

            var gameResult = resultValidator.GetResult(game.Board);
            switch (gameResult)
            {
                case GameResult.NotFinished:
                    break;
                case GameResult.WonByO:
                    game.State = GameState.SecondPlayerWon;
                    this.data.SaveChanges();
                    break;
                case GameResult.WonByX:
                    game.State = GameState.FirstPlayerWon;
                    this.data.SaveChanges();
                    break;
                case GameResult.Draw:
                    game.State = GameState.Draw;
                    this.data.SaveChanges();
                    break;
                default:
                    break;
            }

            if(game.State == GameState.Draw)
            {
                return this.Ok("Draw!");
            }

            if (game.State == GameState.FirstPlayerWon)
            {
                return this.Ok("First player won!");
            }
            if (game.State == GameState.SecondPlayerWon)
            {
                return this.Ok("Second player won!");
            }

            return this.Ok();
        }
Пример #2
0
        public IHttpActionResult Play(PlayRequestDataModel request)
        {
            var currentUserId = this.User.Identity.GetUserId();

            if (request == null || !this.ModelState.IsValid)
            {
                return this.BadRequest(ModelState);
            }

            var idAsGuid = new Guid(request.GameId);

            var game = this.data.Games.GetById(idAsGuid);

            if (game == null)
            {
                return this.BadRequest("Invalid game id!");
            }

            if (game.FirstPlayerId != currentUserId &&
                game.SecondPlayerId != currentUserId)
            {
                return this.BadRequest("You are not part of this game");

            }

            if (game.State != GameState.TurnX &&
                game.State != GameState.TurnY)
            {
                return this.BadRequest("INvalid game state!");
            }

            if ((game.State == GameState.TurnX && game.FirstPlayerId != currentUserId) ||
                (game.State == GameState.TurnY && game.SecondPlayerId != currentUserId))
            {
                return this.BadRequest("Its not your turn to play!");
            }

            var positionIndex = (request.Row - 1) * 3 + request.Col - 1;

            if (game.Board[positionIndex] != '-')
            {
                return this.BadRequest("Invalid position");
            }

            var sbBoard = new StringBuilder(game.Board);
            sbBoard[positionIndex] = game.State == GameState.TurnX ? 'X' : 'O';
            game.Board = sbBoard.ToString();
            game.State = game.State == GameState.TurnX ? GameState.TurnY : GameState.TurnX;

            this.data.SaveChanges();

            var gameResult = validator.GetResult(game.Board);
            switch (gameResult)
            {
                case GameResult.NotFinished:

                    break;
                case GameResult.WonByX:
                    game.State = GameState.WonByX;
                    this.data.SaveChanges();
                    break;
                case GameResult.WonByY:
                    game.State = GameState.WonByY;
                    this.data.SaveChanges();
                    break;
                case GameResult.Draw:
                    game.State = GameState.Draw;
                    this.data.SaveChanges();
                    break;
                default:
                    break;
            }

            return this.Ok();
        }