Exemple #1
0
        private Boolean CheckmateMovementCheck(int x, int y)
        {
            int[,] moves = GetValidKingMoves(x, y);
            Board temp;

            for(int i = 0; i < moves.GetLength(0); i++)
            {
                temp = new Board(this);
                temp.MovePiece(x, y, moves[i, 0], moves[i, 1]);

                if(!temp.Check(squares[x,y].Color)) // if it is possible to move the king out of check
                {
                    return false;
                }
            }
            return true;
        }
        // Handles movement
        private void moveLogic(int x, int y)
        {
            Board tempBoard;
            int[,] possibleMoves;

            tempBoard = new Board(board);
            possibleMoves = board.GetValidMoves(storedX, storedY);

            for (int i = 0; i < possibleMoves.GetLength(0); i++)
            {
                if (board.squares[storedX, storedY].GetType().ToString().Equals("Backend.King")) // Logic branch for Castling
                {
                    King tempKing = (King)board.squares[storedX, storedY];
                    Rook tempRook;

                    if (!tempKing.hasMoved && y == storedY && Math.Abs(x - storedX) == 2)
                    {
                        int rookIndex = 0;
                        int direction = -1;

                        if (x > storedX)
                        {
                            rookIndex = 7;
                            direction = 1;
                        }

                        if (board.squares[rookIndex, y].GetType().ToString().Equals("Backend.Rook"))
                        {
                            tempRook = (Rook)board.squares[rookIndex, y];
                            if (!tempRook.hasMoved)
                            {
                                if (direction == -1 &&             // Scan for pieces blocking the way in the proper direction
                                    board.squares[y, 1] == null &&
                                    board.squares[y, 2] == null &&
                                    board.squares[y, 3] == null ||
                                    direction == 1 &&
                                    board.squares[y, 5] == null &&
                                    board.squares[y, 6] == null)
                                {
                                    if (!tempBoard.Check(board.squares[storedX, storedY].Color)) // The next 3 IF statements are for the rules that you cannot castle into, out of, or through check
                                    {
                                        tempBoard.MovePiece(storedX, y, storedX + direction, y);

                                        if (!tempBoard.Check(board.squares[storedX + direction, storedY].Color))
                                        {
                                            tempBoard.MovePiece(storedX, y, storedX + (direction * 2), y);

                                            if (!tempBoard.Check(board.squares[storedX + (direction * 2), storedY].Color))
                                            {
                                                board.MovePiece(storedX, storedY, x, y);                           // Congratulations, sucessful castle
                                                board.MovePiece(rookIndex, storedY, storedX + direction, storedY);
                                                storedX = -1;
                                                storedY = -1;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else if (possibleMoves[i, 0] == x && possibleMoves[i, 1] == y)
                {
                    tempBoard.MovePiece(storedX, storedY, x, y);
                    if (!tempBoard.Check(tempBoard.squares[x, y].Color))
                    {
                        board.MovePiece(storedX, storedY, x, y);
                        UpdateBoard(board);

                        if(board.squares[x,y].GetType().ToString().Equals("Backend.King"))
                        {
                            King tempKing = (King)board.squares[x, y];
                            tempKing.hasMoved = true;
                        }
                        if (board.squares[x, y].GetType().ToString().Equals("Backend.Rook"))
                        {
                            Rook tempRook = (Rook)board.squares[x, y];
                            tempRook.hasMoved = true;
                        }

                        if (currentColor == TeamColor.WHITE)
                        {
                            if (board.Check(TeamColor.BLACK) && board.Checkmate(TeamColor.BLACK))
                            {
                                MessageBox.Show("WHITE wins!");
                                Environment.Exit(0); // Enviroment.Exit closes the program. Game ends at checkmate, right?
                            }
                            currentColor = TeamColor.BLACK;

                        }
                        else
                        {
                            if (board.Check(TeamColor.WHITE) && board.Checkmate(TeamColor.BLACK))
                            {
                                MessageBox.Show("BLACK wins!");
                                Environment.Exit(0);
                            }
                            currentColor = TeamColor.WHITE;
                        }

                        this.Title = currentColor.ToString() + " Turn!";
                    }
                    break;
                }

                tempBoard.DestroyBoard();
                storedX = -1;
                storedY = -1;
            }
        }