Esempio n. 1
0
        public GameProgress View(int id)
        {
            var game = context.Game.Include(game => game.Moves).Where(game => game.Id == id).First();

            var chess = chessService.ReplayMoves(game.Moves.ToList());

            return(new GameProgress()
            {
                GameId = game.Id,
                State = CoordinateOutput.GameToState(chess),
            });
        }
Esempio n. 2
0
        public GameProgress PostStartGame([FromBody] StartGameRequest request)
        {
            // limit difficulty between 1 and 8
            request.difficulty = Math.Min(8, Math.Max(1, request.difficulty));
            var game = context.Game.Add(new Game()
            {
                Name         = request.name,
                Opponent     = "AI",
                AIDifficulty = request.difficulty,
            });

            ;
            context.SaveChanges();

            var chess = chessService.ReplayMoves(new List <Models.Move>());

            var progress = new GameProgress()
            {
                GameId = game.Entity.Id,
                State  = CoordinateOutput.GameToState(chess),
            };

            return(progress);
        }
Esempio n. 3
0
        public async Task <GameProgress> PlayMove(PlayDTO play, int id)
        {
            var game = context.Game.Include(game => game.Moves).Where(game => game.Id == id).First();

            var chess = chessService.ReplayMoves(game.Moves.ToList());

            var move = chess.Move(play.SAN);

            var boardState = CoordinateOutput.GameToState(chess);

            if (boardState.whiteWins || boardState.isDraw || boardState.blackWins)
            {
                var moveRow = new Models.Move()
                {
                    SAN       = play.SAN,
                    Game      = game,
                    CreatedAt = DateTime.Now
                };
                context.Move.Add(moveRow);

                context.SaveChanges();
                return(new GameProgress()
                {
                    GameId = id,
                    State = boardState
                });
            }

            var aiMove = await chessService.GetAiMove(game.AIDifficulty, chess);

            var aiMoveSan = chess.board.StandardAlgebraicNotation(aiMove);

            chess.Move(aiMove);

            if (MoveHelper.isValidMove(move.move))
            {
                var moveRow = new Models.Move()
                {
                    SAN       = play.SAN,
                    Game      = game,
                    CreatedAt = DateTime.Now
                };

                var aiMoveRow = new Models.Move()
                {
                    SAN       = aiMoveSan,
                    Game      = game,
                    CreatedAt = DateTime.Now
                };

                context.Move.Add(moveRow);

                context.Move.Add(aiMoveRow);

                context.SaveChanges();
            }

            return(new GameProgress()
            {
                GameId = id,
                State = CoordinateOutput.GameToState(chess)
            });
        }
Esempio n. 4
0
        public static String GetState()
        {
            var state = CoordinateOutput.GameToState(game);

            return(JsonConvert.SerializeObject(state));
        }