예제 #1
0
        public async Task RegisterDropAsync(Game subject, Drop drop)
        {
            CrazyhouseChessGame zhGame = subject.ChessGame as CrazyhouseChessGame;

            zhGame.ApplyDrop(drop, true);
            subject.LatestFEN = subject.ChessGame.GetFen();
            subject.UciMoves.Add(char.ToUpperInvariant(drop.ToDrop.GetFenCharacter()) + "@" + drop.Destination.ToString().ToLowerInvariant());
            ClockSwitchAfterMove(subject, drop.Player == Player.White);
            await gameRepository.UpdateAsync(subject);
        }
        public static void TestApplyMove_AddToPocketIfCapture_AndFenGeneration_AndApplyDrop()
        {
            CrazyhouseChessGame game = new CrazyhouseChessGame();

            game.ApplyMove(new Move("E2", "E4", Player.White), true);
            game.ApplyMove(new Move("D7", "D5", Player.Black), true);
            game.ApplyMove(new Move("E4", "D5", Player.White), true);
            Assert.AreEqual(new Pawn(Player.White), game.WhitePocket[0]);
            Assert.AreEqual("rnbqkbnr/ppp1pppp/8/3P4/8/8/PPPP1PPP/RNBQKBNR/P b KQkq - 0 2", game.GetFen());

            game.ApplyMove(new Move("A7", "A5", Player.Black), true);
            Assert.True(game.ApplyDrop(new Drop(new Pawn(Player.White), new Position("H3"), Player.White), false));
            Assert.AreEqual("rnbqkbnr/1pp1pppp/8/p2P4/8/7P/PPPP1PPP/RNBQKBNR b KQkq - 1 3", game.GetFen());
            Assert.AreEqual(0, game.WhitePocket.Count);
        }
예제 #3
0
        public IActionResult SubmitDrop(string id, string role, string pos)
        {
            int puzzleId;

            if (!int.TryParse(id, out puzzleId))
            {
                return(Json(new { success = false, error = "The given ID is invalid." }));
            }

            Puzzle puzzle = puzzlesBeingEdited.Get(puzzleId);

            if (puzzle == null)
            {
                return(Json(new { success = false, error = "The given ID doe snot correspond to a puzzle." }));
            }
            if (puzzle.Author != loginHandler.LoggedInUserId(HttpContext).Value)
            {
                return(Json(new { success = false, error = "Only the puzzle author can access this right now." }));
            }

            if (!(puzzle.Game is CrazyhouseChessGame))
            {
                return(Json(new { success = false, error = "This is not a crazyhouse puzzle." }));
            }

            Piece p = Utilities.GetByRole(role, puzzle.Game.WhoseTurn);

            if (p == null)
            {
                return(Json(new { success = false, error = "Invalid drop piece." }));
            }

            Drop drop = new Drop(p, new Position(pos), puzzle.Game.WhoseTurn);

            CrazyhouseChessGame zhGame = puzzle.Game as CrazyhouseChessGame;

            if (!zhGame.IsValidDrop(drop))
            {
                return(Json(new { success = true, valid = false }));
            }

            zhGame.ApplyDrop(drop, true);
            return(Json(new { success = true, valid = true }));
        }
        public SubmittedMoveResponse ApplyDrop(string role, string pos)
        {
            SubmittedMoveResponse response = new SubmittedMoveResponse()
            {
                Success = true,
                Error   = null
            };

            if (!(Current.Game is CrazyhouseChessGame))
            {
                response.Success = false;
                response.Error   = "Not a crazyhouse puzzle.";
                response.Correct = SubmittedMoveResponse.INVALID_MOVE;
                return(response);
            }

            CrazyhouseChessGame zhCurrentGame = Current.Game as CrazyhouseChessGame;
            Piece p = Utilities.GetByRole(role, zhCurrentGame.WhoseTurn);

            if (p == null)
            {
                response.Success = false;
                response.Error   = "Invalid drop piece.";
                response.Correct = SubmittedMoveResponse.INVALID_MOVE;
                return(response);
            }
            Drop drop = new Drop(p, new Position(pos), zhCurrentGame.WhoseTurn);

            if (!zhCurrentGame.ApplyDrop(drop, false))
            {
                response.Correct = SubmittedMoveResponse.INVALID_MOVE;
                return(response);
            }

            char   pieceChar = char.ToUpper(p.GetFenCharacter());
            string moveStr   = (pieceChar == 'P' ? "" : pieceChar.ToString()) + "@" + pos;

            Moves.Add(moveStr);

            return(MakeMoveAndDropCommon(response, moveStr));
        }
        SubmittedMoveResponse MakeMoveAndDropCommon(SubmittedMoveResponse response, string moveStr)
        {
            response.Check = Current.Game.IsInCheck(Current.Game.WhoseTurn) ? Current.Game.WhoseTurn.ToString().ToLowerInvariant() : null;
            Checks.Add(response.Check);
            string fen = Current.Game.GetFen();

            response.FEN = fen;
            FENs.Add(fen);
            Dictionary <string, int> pocket = Current.Game.GenerateJsonPocket();

            response.Pocket = pocket;
            Pockets.Add(pocket);

            if (Current.Game.IsWinner(ChessUtilities.GetOpponentOf(Current.Game.WhoseTurn)))
            {
                PuzzleFinished(response, true);
                return(response);
            }

            if (!PossibleVariations.Any(x => CompareMoves(x.First(), moveStr) == 0))
            {
                PuzzleFinished(response, false);
                return(response);
            }

            PossibleVariations = PossibleVariations.Where(x => CompareMoves(x.First(), moveStr) == 0).Select(x => x.Skip(1));

            if (PossibleVariations.Any(x => x.Count() == 0))
            {
                PuzzleFinished(response, true);
                return(response);
            }

            string moveToPlay = PossibleVariations.First().First();

            if (!moveToPlay.Contains("@"))
            {
                string[] parts = moveToPlay.Split('-', '=');
                Current.Game.MakeMove(new Move(parts[0], parts[1], Current.Game.WhoseTurn, parts.Length == 2 ? null : new char?(parts[2][0])), true);
            }
            else
            {
                string[] parts = moveToPlay.Split('@');

                CrazyhouseChessGame zhCurrent = Current.Game as CrazyhouseChessGame;
                Piece toDrop = zhCurrent.MapPgnCharToPiece(parts[0] == "" ? 'P' : parts[0][0], zhCurrent.WhoseTurn);
                Drop  drop   = new Drop(toDrop, new Position(parts[1]), zhCurrent.WhoseTurn);
                zhCurrent.ApplyDrop(drop, true);
            }

            response.Play = moveToPlay;
            Moves.Add(moveToPlay);
            response.FenAfterPlay = Current.Game.GetFen();
            FENs.Add(response.FenAfterPlay);
            response.CheckAfterAutoMove = Current.Game.IsInCheck(Current.Game.WhoseTurn) ? Current.Game.WhoseTurn.ToString().ToLowerInvariant() : null;
            Checks.Add(response.CheckAfterAutoMove);
            response.Moves               = Current.Game.GetValidMoves(Current.Game.WhoseTurn);
            response.Correct             = 0;
            response.PocketAfterAutoMove = Current.Game.GenerateJsonPocket();
            Pockets.Add(response.PocketAfterAutoMove);
            PossibleVariations = PossibleVariations.Select(x => x.Skip(1));
            if (PossibleVariations.Any(x => !x.Any()))
            {
                PuzzleFinished(response, true);
            }
            return(response);
        }