예제 #1
0
 private async Task HandleAiPlayer(ChessWebApiResult lastResult)
 {
     if (IsAiTurn(lastResult))
     {
         await PlayRandomMove(lastResult);
     }
 }
예제 #2
0
        public async Task <bool> OnMoveSelectedAsync(string move)
        {
            ChessBoard.Message = "";

            try
            {
                // TODO: Temp hack to stop endless auto-games, need proper stalement & not-enough-material-left checks
                if (_moveCount > 150)
                {
                    throw new Exception("Move limit exceeded");
                }
                _lastResult = await ApiClient.PlayMoveAsync(ChessBoard.Board, EncodeMove(move));

                UpdateBoardAndMoves();
                StateHasChanged();  // NOTE: We call StateHasChanged() because we are in a recursive method when handling AI players and therefore the state doesn't automatically get updated until the stack unwinds
                _moveCount++;
                if (!_lastResult.Message.ToLower().Contains("checkmate"))
                {
                    await HandleAiPlayer(_lastResult);
                }
            }
            catch (Exception e)
            {
                ChessBoard.Message = $"Error performing move;\n{e.Message}"; // TODO: Better exception handling and logging
                StateHasChanged();
            }
            return(true);
        }
예제 #3
0
        private void UpdateMoveListComponent(ChessWebApiResult result)
        {
            var title = string.IsNullOrEmpty(result.Message)
                ? $"{result.WhoseTurn} to play"
                : result.Message;

            MoveList.Update(title, result.AvailableMoves, !IsAiTurn(result));
        }
예제 #4
0
        public async Task ResetBoardAsync()
        {
            if (_firstResult == null)
            {
                await InitialiseState();
            }
            else
            {
                _lastResult = _firstResult;
            }

            UpdateBoardAndMoves();
        }
예제 #5
0
        private async Task PlayRandomMove(ChessWebApiResult lastResult)
        {
            if (!lastResult.AvailableMoves.Any())
            {
                return;
            }

            MoveList.Title        = $"{lastResult.WhoseTurn} is thinking...";
            MoveList.ShowMoveList = false;
            var rnd = Random.Next(lastResult.AvailableMoves.Length); // TODO: Ok so it's not really an AI ;)

            await OnMoveSelectedAsync(lastResult.AvailableMoves[rnd].SAN);
        }
예제 #6
0
 public Task <ChessWebApiResult> PlayMoveAsync(string board, string move)
 {
     return(Task.Run(() =>
     {
         var game = ChessGameConvert.Deserialise(board);
         var msg = game.Move(move);
         var items = game.BoardState.GetItems((int)game.CurrentPlayer).ToArray();
         var result = new ChessWebApiResult
         {
             Board = ChessGameConvert.Serialise(game),
             BoardText = new ChessBoardBuilder().FromChessGame(game).ToTextBoard(),
             AvailableMoves = ToMoveList(game, items),
             WhoseTurn = game.CurrentPlayer.ToString(),
             Message = msg
         };
         return result;
     }));
 }
예제 #7
0
        public async Task InitialiseState()
        {
            if (string.IsNullOrEmpty(BoardString))
            {
                _lastResult = await ApiClient.ChessGameAsync();
            }
            else
            {
                // NOTE: Blazor default routing gets confused by using '.' in the url and I couldn't work out how to fix it so...
                _lastResult = await ApiClient.ChessGameAsync(BoardString.Replace("_", "."));
            }

            if (_firstResult == null)
            {
                _firstResult = _lastResult;
            }

            Guard.NotNull(_lastResult, "Unable to initialise board");

            _moveCount = 0;
        }
예제 #8
0
 private void UpdateChessBoardComponent(ChessWebApiResult result)
 {
     ChessBoard.Update(result.Board, result.AvailableMoves, result.WhoseTurn.ToLower().Contains("white"));
 }
예제 #9
0
 private bool IsAiTurn(ChessWebApiResult lastResult)
 => lastResult.WhoseTurn.ToLower() == "black" && !BlackIsHuman ||
 lastResult.WhoseTurn.ToLower() == "white" && !WhiteIsHuman;