public ChessSquare(int x, int y, ChessColor color) { Loc = new Location(); Loc.X = x; Loc.Y = y; Color = color; }
public Controller() { _board = new ChessBoard(); _startLoc = new Location(); _endLoc = new Location(); _turn = 1; _selectedSquare = null; _promotion = 0; }
public Square(string image) { Pic = new Image(); loc = new Location(); _panel = new StackPanel(); _panel.Orientation = Orientation.Horizontal; _panel.Margin = new System.Windows.Thickness(5); BitmapImage source = new BitmapImage(); source.BeginInit(); source.UriSource = new Uri("Images/" + image, UriKind.RelativeOrAbsolute); source.EndInit(); Pic.Source = source; _panel.Children.Add(Pic); this.Content = _panel; }
/// <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 void IsInCheckMate(int turn) { List<int[]> kingsMoves = new List<int[]>(); List<int[]> enemyMoves = new List<int[]>(); bool inCheck = IsInCheck(turn); int[] king = new int[2];//The king(that is in check)'s location. if (inCheck == true)//If check has occurred. { inCheck = CantBeSaved(); 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. { PrintWinner(); } } } }
/// <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. 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. { MessageBox.Show("Light King is In danger!!!", "Check"); 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. { MessageBox.Show("Dark King is In danger!!!", "Check"); inCheck = true; } } return inCheck; }
/// <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 CantBeSaved() { 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.Search(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 dark king's location. { //Returns moves leading to the king's location. placeHold = Board.Squares[Pieces[x][0], Pieces[x][1]].Piece.Search(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; }
/// <summary> /// Checks if the selected piece's desired location willl take the king out of check. /// </summary> /// <param name="endX"></param> /// <param name="endY"></param> /// <returns> /// True: If the desired location for the piece will take the king out of chess. /// False: If the desired location for the piece will not take the king out of chess. /// </returns> public bool WillSaveTheKing(int startX, int startY, int endX, int endY) { List<int[]> enemyMovement; List<int[]> ally = new List<int[]>(); List<int[]> movement = new List<int[]>(); Location l = new Location(); bool temp = true; enemyMovement = StoreEnemyMovements(); bool isValid = false; for (int x = 0; x < 8; ++x) { for (int y = 0; y < 8; ++y) { if ((int)Board.Squares[x, y].Piece.Color == Turn) { if (Board.Squares[x, y].Piece.GetType() != typeof(King)) { for (int w = 0; w < enemyMovement.Count; ++w) { l.X = enemyMovement[w][0]; l.Y = enemyMovement[w][1]; if (endX == enemyMovement[w][0] && endY == enemyMovement[w][1]) { isValid = true; } } } else { if (Board.Squares[startX, startY].Piece.GetType() == typeof(King)) { bool valid = WillKingCheck(startX, startY, endX, endY); if (valid == true) { for (int w = 0; w < enemyMovement.Count; ++w) { if (endX == enemyMovement[w][0] && endY == enemyMovement[w][1]) { if (Board.Squares[enemyMovement[w][0], enemyMovement[w][1]].Piece.GetType() == typeof(Space)) { temp = false; } else { isValid = true; } } else if (endX != enemyMovement[w][0] || endY != enemyMovement[w][1]) { isValid = true; } } } else { temp = false; } } } } } } if (temp == false) { isValid = temp; } return isValid; }
/// <summary> /// Loops through, getting enemy movements, and storing them. /// Looks for the king of the current player and stores his location. /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns>Returns a list of movements thats are directed to the king.</returns> public List<int[]> StoreEnemyMovements() { List<int[]> enemyMovement = new List<int[]>();//A storage for all possible legal move for enemy pieces. List<int[]> allyMovement = new List<int[]>();//A storage for all possible legal move for ally pieces. List<int[]> enemy = new List<int[]>();//A placeholder for enemy movements List<int[]> Pieces = new List<int[]>();//a list int[] king = new int[2];//King's location. for (int x = 0; x < 8; ++x) { for (int y = 0; y < 8; y++) { if ((int)Board.Squares[x, y].Piece.Color != Turn && Board.Squares[x, y].Piece.Color != ChessColor.NONE) { if (Board.Squares[x, y].Piece.GetType() != typeof(Pawn)) { Location pieceLoc = new Location();//Gets the current piece's location, which will eventually loop through all pieces. pieceLoc.X = x; pieceLoc.Y = y; enemy = 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 < enemy.Count; ++j) { enemyMovement.Add(enemy[j]);//Adds all possible legal enemy movements from the current piece being looped through. Pieces.Add(new int[] { pieceLoc.X, pieceLoc.Y });//Contains all piece's that have legal moves. } } else { Location pieceLoc = new Location();//Gets the current piece's location, which will eventually loop through all pieces. pieceLoc.X = x; pieceLoc.Y = y; enemy = ((Pawn)Board.Squares[x, y].Piece).Attacks(Board.Squares, pieceLoc.X, pieceLoc.Y);//Gets all possible legal moves for current piece. for (int j = 0; j < enemy.Count; ++j) { enemyMovement.Add(enemy[j]);//Adds all possible legal enemy movements from the current piece being looped through. Pieces.Add(new int[] { pieceLoc.X, pieceLoc.Y });//Contains all piece's that have legal moves. } } } else if ((int)Board.Squares[x, y].Piece.Color == Turn) { if (Board.Squares[x, y].Piece.GetType() == typeof(King)) { king = new int[] { x, y };//Stores the king's location. } } } } for (int x = 0; x < enemyMovement.Count; ++x)//Loops through all possible legal moves. { if (enemyMovement[x][0] == king[0] && enemyMovement[x][1] == king[1])//If an enemy movement is the same as the king's location. { //Returns moves leading to the king's location. enemy = Board.Squares[Pieces[x][0], Pieces[x][1]].Piece.Search(Board.Squares, Pieces[x][0], Pieces[x][1], king[0], king[1]); enemy.Add(new int[] { Pieces[x][0], Pieces[x][1] }); break; } } return enemy; }
/// <summary> /// Gets all possible enemy moves. /// </summary> /// <returns></returns> public List<int[]> StoreAllEnemyMovements() { List<int[]> enemyMovement = new List<int[]>();//A storage for all possible legal move for enemy pieces. List<int[]> enemy = new List<int[]>();//A placeholder for enemy movements List<int[]> Pieces = new List<int[]>();//a list for (int x = 0; x < 8; ++x) { for (int y = 0; y < 8; y++) { if ((int)Board.Squares[x, y].Piece.Color != Turn && Board.Squares[x, y].Piece.Color != ChessColor.NONE) { if (Board.Squares[x, y].Piece.GetType() != typeof(Pawn)) { Location pieceLoc = new Location();//Gets the current piece's location, which will eventually loop through all pieces. pieceLoc.X = x; pieceLoc.Y = y; enemy = 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 < enemy.Count; ++j) { enemyMovement.Add(enemy[j]);//Adds all possible legal enemy movements from the current piece being looped through. } } else { Location pieceLoc = new Location();//Gets the current piece's location, which will eventually loop through all pieces. pieceLoc.X = x; pieceLoc.Y = y; enemy = ((Pawn)Board.Squares[x, y].Piece).Attacks(Board.Squares, pieceLoc.X, pieceLoc.Y);//Gets all possible legal moves for current piece. for (int j = 0; j < enemy.Count; ++j) { enemyMovement.Add(enemy[j]);//Adds all possible legal enemy movements from the current piece being looped through. } } } } } return enemyMovement; }
public void ResetGame() { _board = new ChessBoard(); g.Children.Clear(); inCheck = false; CreateBoard(g, l, l2); _startLoc = new Location(); _endLoc = new Location(); ChangeTurn(); _startSquare = null; _selectedSquare = null; }
/// <summary> /// If the piece contains a movement that can save the king, then the piece if selected, /// will have it's movements that will save the king highlighted. /// </summary> /// <param name="j"></param> /// <param name="k"></param> public void CanSaveKing(int j, int k) { List<int[]> enemyMovement; List<int[]> allyMovement = new List<int[]>(); List<int[]> kingMovement = new List<int[]>(); List<int[]> ally = new List<int[]>(); List<int[]> movement = new List<int[]>(); Location l = new Location(); Location l2 = new Location(); enemyMovement = StoreEnemyMovements(); for (int x = 0; x < 8; ++x) { for (int y = 0; y < 8; ++y) { if ((int)Board.Squares[x, y].Piece.Color == Turn) { if (Board.Squares[x, y].Piece.GetType() != typeof(King)) { List<int[]> temp = Board.Squares[x, y].Piece.RestrictMovement(Board.Squares, x, y); for (int z = 0; z < temp.Count; ++z) { for (int w = 0; w < enemyMovement.Count; ++w) { if (temp[z][0] == enemyMovement[w][0] && temp[z][1] == enemyMovement[w][1]) { ally.Add(new int[] { x, y }); allyMovement.Add(temp[z]); } } } } else { List<int[]> kingMoves = Board.Squares[x, y].Piece.RestrictMovement(Board.Squares, x, y); for (int z = 0; z < kingMoves.Count; ++z) { for (int w = 0; w < enemyMovement.Count; ++w) { //if (kingMoves[z][0] == enemyMovement[w][0] && kingMoves[z][1] == enemyMovement[w][1]) //{ // if (Board.Squares[enemyMovement[w][0], enemyMovement[w][1]].Piece.GetType() != typeof(Space)) // { // ((Square)g.Children[(kingMoves[z][0] * 8) + kingMoves[z][1]]).Background = Brushes.LightYellow; // } //} /*else*/ if (kingMoves[z][0] != enemyMovement[w][0] || kingMoves[z][1] != enemyMovement[w][1]) { kingMovement.Add(kingMoves[z]); } } } } } } } a = ally; if (Board.Squares[j, k].Piece.GetType() != typeof(King)) { movement = Board.Squares[j, k].Piece.RestrictMovement(Board.Squares, j, k); for (int x = 0; x < allyMovement.Count; ++x) { for (int y = 0; y < movement.Count; ++y) { l.X = allyMovement[x][0]; l.Y = allyMovement[x][1]; l2.X = movement[y][0]; l2.Y = movement[y][1]; if (allyMovement[x][0] == movement[y][0] && allyMovement[x][1] == movement[y][1]) { ((Square)g.Children[(allyMovement[x][0] * 8) + allyMovement[x][1]]).Background = Brushes.Plum; } } } } else { PrintKingMoves(j, k); } }