Beispiel #1
0
 public ChessSquare(int y, int x, ChessColor color)
 {
     Loc = new Location();
     Loc.Y = y;
     Loc.X = x;
     Color = color;
 }
Beispiel #2
0
 /// <summary>
 /// Checks if a player is in check.
 /// If they are, then it determines if the king has no way to get out of check.
 /// Prints out checkmate if the king can not get out of check.
 /// </summary>
 /// <param name="turn">An int representing a player's turn.</param>
 public bool IsInCheckMate(int turn)
 {
     List<int[]> kingsMoves = new List<int[]>();
     List<int[]> enemyMoves = new List<int[]>();
     bool inCheck = IsInCheck(turn);
     bool checkmate = false;
     int[] king = new int[2];//The king(that is in check)'s location.
     if (inCheck == true)//If check has occurred.
     {
         inCheck = CanBeSaved();
         if (inCheck == true)//If the piece could be saved.
         {
             for (int x = 0; x < 8; ++x)
             {
                 for (int y = 0; y < 8; y++)
                 {
                     if (Board.Squares[x, y].Piece.GetType() == typeof(King))//Loops through every piece searching for the king in check.
                     {
                         if ((int)Board.Squares[x, y].Piece.Color == turn)//Makes sure the color of the king is that of the current player's turn.
                         {
                             king = new int[] { x, y };//Stores the king's location.
                         }
                     }
                 }
             }//End of the for loop
             kingsMoves = Board.Squares[king[0], king[1]].Piece.RestrictMovement(Board.Squares, king[0], king[1]);//Stores the kings movements.
             for (int x = 0; x < 8; ++x)
             {
                 for (int y = 0; y < 8; y++)
                 {
                     if ((int)Board.Squares[x, y].Piece.Color != turn)
                     {
                         Location pieceLoc = new Location();//Gets the current piece's location, which will eventually loop through all pieces.
                         pieceLoc.X = x;
                         pieceLoc.Y = y;
                         //Gets all possible legal moves for current enemy piece.
                         List<int[]> placeHold = Board.Squares[x, y].Piece.RestrictMovement(Board.Squares, pieceLoc.X, pieceLoc.Y);
                         for (int j = 0; j < placeHold.Count; ++j)
                         {
                             enemyMoves.Add(placeHold[j]);//Adds all possible legal movements from the enemy piece being looped through.
                         }
                     }
                 }
             }//End of the for loop
             bool[] cantMove = new bool[kingsMoves.Count];//Sets a bool array length to the length of kingsMoves.
             for (int j = 0; j < enemyMoves.Count; ++j)
             {
                 for (int k = 0; k < kingsMoves.Count; ++k)
                 {
                     //Checks if any of the kings movements will make the king go back in check
                     if (enemyMoves[j][0] == kingsMoves[k][0] && enemyMoves[j][1] == kingsMoves[k][1])
                     {
                         cantMove[k] = true;
                     }
                 }
             }//End of the for loop
             int num = 0;
             for (int z = 0; z < cantMove.Count(); ++z)
             {
                 //Checks if all of kings moves will make him captured
                 if (cantMove[z] == true)
                 {
                     ++num;
                 }
             }//End of the for loop
             if (num == cantMove.Length)//If all the kings movements will cause the king to get captures.
             {
                 Console.WriteLine("Checkmate!!!");
                 checkmate = true;
             }
         }
     }
     return checkmate;
 }
Beispiel #3
0
 public Controller()
 {
     _board = new ChessBoard();
     _startLoc = new Location();
     _endLoc = new Location();
 }
Beispiel #4
0
        /// <summary>
        /// Grabs all available legal moves from every piece and checks if an
        /// available legal move from that piece can kill the opposite colored  king.
        /// Prints out the kings status.
        /// </summary>
        ///<returns>
        ///True: If a piece can move to kill a king.
        ///False: If no move can capture a king.
        /// </returns>
        public bool IsInCheck(int turn)
        {
            List<int[]> movements = new List<int[]>();//A storage for all possible legal move for the pieces.
            List<int[]> Pieces = new List<int[]>();
            int[] lKing = new int[2];//Light king's location.
            int[] dKing = new int[2];//Dark king's location.
            bool inCheck = false;
            for (int x = 0; x < 8; ++x)
            {
                for (int y = 0; y < 8; y++)
                {
                    Location pieceLoc = new Location();//Gets the current piece's location, which will eventually loop through all pieces.
                    pieceLoc.X = x;
                    pieceLoc.Y = y;
                    List<int[]> placeHold = Board.Squares[x, y].Piece.RestrictMovement(Board.Squares, pieceLoc.X, pieceLoc.Y);//Gets all possible legal moves for current piece.
                    for (int j = 0; j < placeHold.Count; ++j)
                    {
                        movements.Add(placeHold[j]);//Adds all possible legal movements from the current piece being looped through.
                        Pieces.Add(new int[] { pieceLoc.X, pieceLoc.Y });//Contains all piece's that have legal moves.
                    }
                    if (Board.Squares[x, y].Piece.GetType() == typeof(King))
                    {
                        if (Board.Squares[x, y].Piece.Color == ChessColor.LIGHT)
                        {
                            lKing = new int[] { x, y };//Stores the light king's location.
                        }
                        else
                        {
                            dKing = new int[] { x, y };//Stores the dark king's location.
                        }
                    }
                }
            }
            for (int x = 0; x < movements.Count; ++x)//Loops through all possible legal moves.
            {
                string[] word = GrabPiece(movements[x][0], movements[x][1]);//Word is equal to a move.

                if (movements[x][0] == lKing[0] && movements[x][1] == lKing[1])//If a move is the same as the light king's location.
                {
                    Console.WriteLine("Light King's Status: In danger!!!\n{0} {1}'s movement to {2}{3}, will capture {4} {5}.",
                                Board.Squares[Pieces[x][0], Pieces[x][1]].Piece.Color.ToString(),//Color of piece
                                Board.Squares[Pieces[x][0], Pieces[x][1]].Piece.Name,//Name of piece
                                word[0], word[1],//Piece's movement
                                Board.Squares[lKing[0], lKing[1]].Piece.Color.ToString(),//King's color
                                Board.Squares[lKing[0], lKing[1]].Piece.Name);//King's name
                    inCheck = true;
                }
                else if (movements[x][0] == dKing[0] && movements[x][1] == dKing[1])//If a move is the same as the dark king's location.
                {
                    Console.WriteLine("Dark King's Status: In danger!!!\n{0} {1}'s movement to {2}{3}, will capture {4} {5}.",
                                Board.Squares[Pieces[x][0], Pieces[x][1]].Piece.Color.ToString(),//Color of piece
                                Board.Squares[Pieces[x][0], Pieces[x][1]].Piece.Name,//Name of piece
                                word[0], word[1],//Piece's movement
                                Board.Squares[dKing[0], dKing[1]].Piece.Color.ToString(),//King's color
                                Board.Squares[dKing[0], dKing[1]].Piece.Name);//King's name
                    inCheck = true;
                }
            }
            return inCheck;
        }
Beispiel #5
0
        /// <summary>
        /// Checks if a piece can save the the same colored king from checkmate.
        /// </summary>
        /// <returns>
        /// True: If no piece can take the king out of check.
        /// False: If the king can be taken out of check.
        /// </returns>
        public bool CanBeSaved()
        {
            List<int[]> movements = new List<int[]>();//A storage for all possible legal move for the pieces.
            List<int[]> Pieces = new List<int[]>();//a list
            List<int[]> placeHold = new List<int[]>();//A placeholder for a piece's movements
            List<int[]> placeHold2 = new List<int[]>();//A placeholder for a piece's movements
            int[] lKing = new int[2];//Light king's location.
            int[] dKing = new int[2];//Dark king's location.
            bool inCheck = true;

            for (int x = 0; x < 8; ++x)
            {
                for (int y = 0; y < 8; y++)
                {
                    Location pieceLoc = new Location();//Gets the current piece's location, which will eventually loop through all pieces.
                    pieceLoc.X = x;
                    pieceLoc.Y = y;
                    placeHold = Board.Squares[x, y].Piece.RestrictMovement(Board.Squares, pieceLoc.X, pieceLoc.Y);//Gets all possible legal moves for current piece.
                    for (int j = 0; j < placeHold.Count; ++j)
                    {
                        movements.Add(placeHold[j]);//Adds all possible legal movements from the current piece being looped through.
                        Pieces.Add(new int[] { pieceLoc.X, pieceLoc.Y });//Contains all piece's that have legal moves.
                    }
                    if (Board.Squares[x, y].Piece.GetType() == typeof(King))
                    {
                        if (Board.Squares[x, y].Piece.Color == ChessColor.LIGHT)
                        {
                            lKing = new int[] { x, y };//Stores the light king's location.
                        }
                        else
                        {
                            dKing = new int[] { x, y };//Stores the dark king's location.
                        }
                    }
                }
            }
            placeHold = new List<int[]>();
            for (int x = 0; x < movements.Count; ++x)//Loops through all possible legal moves.
            {
                if (movements[x][0] == lKing[0] && movements[x][1] == lKing[1])//If an enemy movement is the same as the light king's location.
                {
                    //Returns moves leading to the king's location.
                    placeHold = Board.Squares[Pieces[x][0], Pieces[x][1]].Piece.Test(Board.Squares, Pieces[x][0], Pieces[x][1], lKing[0], lKing[1]);
                    for (int v = 0; v < movements.Count; ++v)//Loops through all possible legal moves.
                    {
                        //If the king is the same color as a piece.
                        if (Board.Squares[lKing[0], lKing[1]].Piece.Color == Board.Squares[movements[v][0], movements[v][1]].Piece.Color)
                        {
                            //Returns moves that can protect the king from check mate.
                            placeHold2 = Board.Squares[movements[x][0], movements[x][1]].Piece.RestrictMovement(Board.Squares, movements[x][0], movements[x][1]);
                        }
                        for (int y = 0; y < placeHold2.Count; ++y)
                        {
                            for (int z = 0; z < placeHold.Count; ++z)
                            {
                                if (placeHold[z] == placeHold2[y])
                                {
                                    inCheck = false;
                                }
                            }
                        }
                    }
                }
                if (movements[x][0] == dKing[0] && movements[x][1] == dKing[1])//If an enemy movement is the same as the light dark's location.
                {
                    //Returns moves leading to the king's location.
                    placeHold = Board.Squares[Pieces[x][0], Pieces[x][1]].Piece.Test(Board.Squares, Pieces[x][0], Pieces[x][1], dKing[0], dKing[1]);
                    for (int v = 0; v < movements.Count; ++v)//Loops through all possible legal moves.
                    {
                        //If the king is the same color as a piece.
                        if (Board.Squares[dKing[0], dKing[1]].Piece.Color == Board.Squares[movements[v][0], movements[v][1]].Piece.Color)
                        {
                            //Returns moves that can protect the king from check mate.
                            placeHold2 = Board.Squares[movements[x][0], movements[x][1]].Piece.RestrictMovement(Board.Squares, movements[x][0], movements[x][1]);
                        }
                        for (int y = 0; y < placeHold2.Count; ++y)
                        {
                            for (int z = 0; z < placeHold.Count; ++z)
                            {
                                if (placeHold[z][0] == placeHold2[y][0] && placeHold[z][1] == placeHold2[y][1])
                                {
                                    inCheck = false;
                                }
                            }
                        }
                    }
                }
            }
            return inCheck;
        }