コード例 #1
0
ファイル: GameHandler.cs プロジェクト: hymeck/Chess
 public void MakeMove(ref ChessGame game, string move)
 {
     var(from, to) = MoveInputParser.ParseMove(move);
     game          = game.Move(from, to);
 }
コード例 #2
0
ファイル: ChessGameService.cs プロジェクト: hymeck/ChessWeb
        public async Task <Game> MakeMove(long gameId, string userId, string move)
        {
            var game = await _gameRepository.GetAsync(gameId);

            if (game == null)
            {
                return(game);
            }

            if (game.IsDone || game.IsWaiting)
            {
                return(game);
            }

            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                return(game);
            }

            var side = (await _sideRepository.GetGameSides(game))
                       .FirstOrDefault(x => x.UserId == user.Id);

            if (side == null)
            {
                return(game);
            }

            var fen       = game.Fen;
            var chessGame = new ChessGame(fen);
            var color     = await _colorRepository.FindAsync(side.ColorId);

            if (color.ToChar() != (char)chessGame.ActiveColor)
            {
                return(game);
            }

            var moveEntry = new Move
            {
                Fen      = game.Fen,
                Game     = game,
                MoveNext = move,
                User     = user
            };

            var moveNext = moveEntry.MoveNext;

            try
            {
                var squares       = MoveInputParser.ParseMove(moveNext);
                var nextChessGame = chessGame.Move(squares);

                ChangeGameState(nextChessGame, ref game, moveEntry, color);

                await _moveRepository.AddAsync(moveEntry);

                _gameRepository.Update(game);
                await _gameRepository.SaveChangesAsync();

                return(game);
            }

            catch (FormatException e)
            {
                Debug.WriteLine(e.Message + "\n" + e.StackTrace);
                return(game);
            }

            catch (ChessGameException e)
            {
                Debug.WriteLine(e.Message + "\n" + e.StackTrace);
                return(game);
            }
        }