示例#1
0
        private void ProcessCheck()
        {
            var isCheck = PieceManager.isCheck(ColorPlayingNow == "W" ? Color.White : Color.Black, board);

            if (isCheck)
            {
                var ColorInCheck = ColorPlayingNow == "B" ? "W" : "B";
                var kingInCheck  = Getkingincheck(ColorPlayingNow == "W" ? Color.White : Color.Black, board);

                var newCheck = new Check()
                {
                    isChecked    = true,
                    isActive     = true,
                    ColorInCheck = ColorInCheck,
                    Xpos         = kingInCheck.Xpos,
                    Ypos         = kingInCheck.Ypos,
                    CheckID      = -1
                };
                TrackChecks.Add(newCheck);


                Message = ColorPlayingNow == "B" ? "White your king is under threat by black (Check)" : "Black your king is under threat by white (Check)";
            }
        }
示例#2
0
        public void OnPost()
        {
            var res = Request.Form["DataField"];

            hasMoved = false;
            ReadMovesFromDb();
            if (TrackMoves != null && TrackMoves.Count > 0)
            {
                ColorPlayingNow = TrackMoves.LastOrDefault().Color == Color.White ? "B" : "W";
            }
            ReadFromDb();

            ReadChecksFromDb();
            board = PieceManager.UpdateAttackedSpots(ColorPlayingNow != "W" ? Color.White : Color.Black, board);
            //  var neighbours = obj.Tiles.Where(x => x.Value.Description == DataField);
            var movefrom   = board.GetTile(DataField1);
            var moveto     = board.GetTile(DataField2);
            var color      = ColorPlayingNow == "W" ? Color.White : Color.Black;
            var validation = PieceManager.ValidateMove(color, movefrom, moveto, board);

            if (!validation)
            {
                Message = $"Invalid move {color}";
                return;
            }
            var lastCheck = TrackChecks.LastOrDefault();

            if (!movefrom.IsEmpty)
            {
                if (lastCheck != null && lastCheck.isChecked && lastCheck.isActive)
                {
                    var kingInCheck      = Getkingincheck(ColorPlayingNow == "W" ? Color.White : Color.Black, board);
                    var moves            = kingInCheck.Piece.GetNeighbours(board);
                    var EndageredSquared = PieceManager.GetSquaresUnderThreat(ColorPlayingNow == "W" ? Color.White : Color.Black, board);
                    moves.AddRange(EndageredSquared);
                    var canMovePieces = new List <Tile>();
                    if (ColorPlayingNow == "W")
                    {
                        var currColor = lastCheck.ColorInCheck == "B" ? "Black" : "White";
                        //white attacking black, check what squares black is controlling/attacking
                        if (moves.Any(x => x.isUnderAttackWhite))
                        {
                            var squareUnderAttack = moves.Where(x => x.isUnderAttackWhite || (x.Piece != null && x.Piece.Color != Color.Black));
                            if (!hasMoved)
                            {
                                if (movefrom.Piece.GetName() != "king")
                                {
                                    var currentPlayersPieces = PieceManager.GetPlayersPieces(board, Color.Black);

                                    var currentMoves = currentPlayersPieces.Select(x => x.Piece);
                                    foreach (var piece in currentMoves)
                                    {
                                        foreach (var sq  in squareUnderAttack)
                                        {
                                            var canMove = piece.GetNeighbours(board).Any(x => x.Xpos == sq.Xpos && x.Ypos == sq.Ypos);
                                            if (canMove)
                                            {
                                                canMovePieces.Add(currentPlayersPieces.FirstOrDefault(x => x.Piece.GetName() == piece.GetName()));
                                            }
                                        }
                                    }

                                    if (canMovePieces.Any(x => x.Piece.GetName() == movefrom.Piece.GetName()))
                                    {
                                        // Validate move, then Process the move if it passes validation, else return false
                                        var flag = PieceManager.ValidateMove(ColorPlayingNow == "W" ? Color.White : Color.Black, movefrom, moveto, board);
                                        if (!flag)
                                        {
                                            var tmpColor = lastCheck.ColorInCheck == "B" ? "Black" : "White";
                                            Message = $"{tmpColor} - Your king is currently under threat (Check)<br/> Please protect your king to continue";
                                            return;
                                        }
                                        var currentMove = new TrackMove(ColorPlayingNow == "W" ? Color.White : Color.Black, movefrom.Piece.TypeName, movefrom.Description);
                                        TrackMoves.Add(currentMove);
                                        PieceManager.ProcessMove(board, movefrom, moveto, TrackMoves);
                                        PieceManager.UpdateAttackedSpots(Color.Black, board);
                                        lastCheck.isActive = false;
                                        hasMoved           = true;
                                        TrackChecks.Add(lastCheck);
                                        ProcessCheck();
                                        ProcessCheckMate();
                                        ChangePlayers();
                                        CompleteWrites();
                                        return;
                                    }
                                    else
                                    {
                                        Message = $"{currColor} - Your king is currently under threat (Check)<br/> Please move your king to continue";
                                        return;
                                    }
                                }
                            }
                            if (!hasMoved)
                            {
                                if (movefrom.Piece.GetName() == "king")
                                {
                                    // Validate can the king move there.
                                    if (moveto.isUnderAttackWhite)
                                    {
                                        Message = $"{currColor} - You cannot move to {moveto.Description}, king is currently under threat (Check)<br/> Please move your king to continue";
                                        return;
                                    }
                                    lastCheck.isActive = false;
                                    var currentMove = new TrackMove(ColorPlayingNow == "W" ? Color.White : Color.Black, movefrom.Piece.TypeName, movefrom.Description);
                                    TrackMoves.Add(currentMove);
                                    PieceManager.ProcessMove(board, movefrom, moveto, TrackMoves);
                                    PieceManager.UpdateAttackedSpots(Color.Black, board);
                                    hasMoved = true;
                                    TrackChecks.Add(lastCheck);
                                    ProcessCheck();
                                    ProcessCheckMate();
                                    ChangePlayers();
                                    CompleteWrites();
                                    return;
                                }
                            }
                        }
                    }
                    if (ColorPlayingNow == "B")
                    {
                        //Black attacking white, check what squares black is controlling/attacking
                        if (moves.Any(x => x.isUnderAttackWhite))
                        {
                            var squareUnderAttack = moves.Where(x => x.isUnderAttackWhite || (x.Piece != null && x.Piece.Color != Color.White));
                            if (movefrom.Piece.GetName() != "king")
                            {
                                var currentPlayersPieces = PieceManager.GetPlayersPieces(board, Color.White);

                                var currentMoves = currentPlayersPieces.Select(x => x.Piece);
                                foreach (var piece in currentMoves)
                                {
                                    foreach (var sq in squareUnderAttack)
                                    {
                                        var canMove = piece.GetNeighbours(board).Any(x => x.Xpos == sq.Xpos && x.Ypos == sq.Ypos);
                                        if (canMove)
                                        {
                                            canMovePieces.Add(currentPlayersPieces.FirstOrDefault(x => x.Piece.GetName() == piece.GetName()));
                                        }
                                    }
                                }

                                if (canMovePieces.Any(x => x.Piece.GetName() == movefrom.Piece.GetName()))
                                {
                                    var flag = PieceManager.ValidateMove(ColorPlayingNow == "W" ? Color.White : Color.Black, movefrom, moveto, board);
                                    if (!flag)
                                    {
                                        var currColor = lastCheck.ColorInCheck == "B" ? "Black" : "White";
                                        Message = $"{currColor} - Your king is currently under threat (Check)<br/> Please protect your king to continue";
                                        return;
                                    }
                                    var currentMove = new TrackMove(ColorPlayingNow == "W" ? Color.White : Color.Black, movefrom.Piece.TypeName, movefrom.Description);
                                    TrackMoves.Add(currentMove);
                                    PieceManager.ProcessMove(board, movefrom, moveto, TrackMoves);
                                    PieceManager.UpdateAttackedSpots(Color.White, board);
                                    hasMoved           = true;
                                    lastCheck.isActive = false;
                                    TrackChecks.Add(lastCheck);
                                    ProcessCheck();
                                    ProcessCheckMate();
                                    ChangePlayers();
                                    CompleteWrites();
                                    return;
                                }
                                else
                                {
                                    var currColor = lastCheck.ColorInCheck == "B" ? "Black" : "White";
                                    Message = $"{currColor} - Your king is currently under threat (Check)<br/> Please move your king to continue";
                                    return;
                                }
                            }
                            if (movefrom.Piece.GetName() == "king")
                            {
                                // Validate can the king move there.
                                if (moveto.isUnderAttackWhite)
                                {
                                    var currColor = lastCheck.ColorInCheck == "B" ? "Black" : "White";
                                    Message = $"{currColor} - You cannot move to {moveto.Description}, king is currently under threat (Check)<br/> Please move your king to continue";
                                    return;
                                }
                                lastCheck.isActive = false;
                                var currentMove = new TrackMove(ColorPlayingNow == "W" ? Color.White : Color.Black, movefrom.Piece.TypeName, movefrom.Description);
                                TrackMoves.Add(currentMove);
                                PieceManager.ProcessMove(board, movefrom, moveto, TrackMoves);
                                PieceManager.UpdateAttackedSpots(Color.White, board);
                                hasMoved = true;
                                TrackChecks.Add(lastCheck);
                                ProcessCheck();
                                ProcessCheckMate();
                                ChangePlayers();
                                CompleteWrites();
                                return;
                            }
                        }
                    }
                }

                if (TrackMoves.Count == 0 && movefrom.Piece.Color != Color.White)
                {
                    ColorPlayingNow = "W";
                    return;
                }



                if (!hasMoved)
                {
                    PieceNeighbours = movefrom.Piece.GetNeighbours(board);
                    if (PieceNeighbours.Any(x => x.Description == moveto.Description))
                    {
                        var currentMove = new TrackMove(ColorPlayingNow == "W" ? Color.White : Color.Black, movefrom.Piece.TypeName, movefrom.Description);
                        TrackMoves.Add(currentMove);
                        PieceManager.ProcessMove(board, movefrom, moveto, TrackMoves);
                        hasMoved = true;

                        ProcessCheck();
                        ProcessCheckMate();
                        if (movefrom.Piece != null && movefrom.Piece.GetName() == "king")
                        {
                            lastCheck.isActive = false;
                        }
                        ChangePlayers();
                        CompleteWrites();
                        return;
                    }
                }
            }
        }