private void CleanChessBoardPieces() { BoxLocation location = new BoxLocation(); ChessPiece blackBox = new ChessPiece(" "); ChessPiece whiteBox = new ChessPiece("██"); for (int row = 8; row > 0; row--) { for (int col = 1; col < 9; col++) { location.Id = (int.Parse(row.ToString() + col.ToString())); if (((row % 2 == 0) && (col % 2 != 0)) || ((row % 2 != 0) && (col % 2 == 0))) { whiteBox.BoardLocation = location; PrintPiece(whiteBox); } else { blackBox.BoardLocation = location; PrintPiece(blackBox); }; } } }
private List <BoxLocation> AddPosibleLocation(int r, int c, List <BoxLocation> posibleLocations) { //discard the rows and columns that are outside the board if (r > 0 && r < 9 && c > 0 && c < 9) { int calculatedLocation = int.Parse(r.ToString() + c.ToString()); BoxLocation location = Locations.Where(l => l.Id == calculatedLocation).FirstOrDefault(); if (location != null) { posibleLocations.Add(location); } } return(posibleLocations); }
private ChessPiece setRandomLocation(ChessPiece piece) { do { BoxLocation randomLocation = GetRandomLocation(); ChessPiece searchPiece = ChessPieces.Find(p => p.BoardLocation != null && p.BoardLocation.Id == randomLocation.Id); if (searchPiece == null) { piece.BoardLocation = randomLocation; } } while (piece.BoardLocation == null); return(piece); }
private void UpdateChessPieceLocation(ChessPiece selectedPiece, BoxLocation selectedDestiny) { // If there is a piece in the destiny location, // I remove/delete the piece ChessPiece pieceToRemove = ChessPieces.Where(p => p.BoardLocation == selectedDestiny).FirstOrDefault(); if (pieceToRemove != null) { ChessPieces.Remove(pieceToRemove); } ; //Update piece's location int i = ChessPieces.FindIndex(p => p.Id == selectedPiece.Id); ChessPieces[i].BoardLocation = selectedDestiny; }
private ChessPiece setRandomLocation(ChessPiece piece, string backgroundColor) { //refactor setRandomLocation(,) and setRandomLocation() do { BoxLocation randomLocation = GetRandomLocation(backgroundColor); ChessPiece searchPiece = ChessPieces.Find(p => p.BoardLocation != null && p.BoardLocation.Id == randomLocation.Id); if (searchPiece == null) { piece.BoardLocation = randomLocation; } } while (piece.BoardLocation == null); return(piece); }
private void PlayTurn() { bool inputFlag = true; ChessPiece selectedPiece = null; BoxLocation selectedDestiny = null; do { //piece selection string selectedBox = myChessBoardUI.SelectBox("Ingrese ubicación de la pieza a mover"); inputFlag = Locations.Any(l => l.BoardReference.Equals(selectedBox)); if (inputFlag == false) { myChessBoardUI.PrintMessage("Ingreso NO valido - Revise las Referencias"); } else { selectedPiece = ChessPieces.Find(p => p.BoardLocation.BoardReference.Equals(selectedBox)); if (selectedPiece == null) { inputFlag = false; myChessBoardUI.PrintMessage("No hay una pieza en el casillero ingresado"); } //destiny selection and movement validation if (inputFlag != false) { selectedBox = myChessBoardUI.SelectBox("Ingrese el destino de la pieza: " + selectedPiece.BoardLocation.BoardReference); selectedDestiny = Locations.Where(l => l.BoardReference.Equals(selectedBox)).FirstOrDefault(); if (selectedDestiny == null) { inputFlag = false; myChessBoardUI.PrintMessage("Ingreso NO valido - Revise las Referencias"); } else if (selectedDestiny.Id == selectedPiece.BoardLocation.Id) { inputFlag = false; myChessBoardUI.PrintMessage("Ingreso NO valido - Ingreso repetido"); } else { bool movementValidation = MovementValidation(selectedPiece, selectedDestiny); if (!MovementValidation(selectedPiece, selectedDestiny)) { inputFlag = false; } }; } } } while (inputFlag == false); UpdateChessPieceLocation(selectedPiece, selectedDestiny); myChessBoardUI.UpdateChessBoard(ChessPieces); }
private bool ValidateRookMove(int colDestinyLocation, int rowDestinyLocation, int col, int row, ChessPiece piece, BoxLocation destinationBox) { int startIndex; int endIndex; string destinyAxis; int calculatedLocation; if (colDestinyLocation == col || rowDestinyLocation == row) { //Considering the axis and the destiny locacion from the piece //I calculate the limits of the indexs if (rowDestinyLocation == row) { destinyAxis = "row"; if (colDestinyLocation > col) { startIndex = col + 1; endIndex = colDestinyLocation; } else { startIndex = colDestinyLocation; endIndex = col - 1; }; } else { destinyAxis = "col"; if (rowDestinyLocation > row) { startIndex = row + 1; endIndex = rowDestinyLocation; } else { startIndex = rowDestinyLocation; endIndex = row - 1; }; }; //I go through the locations between the destination and the piece to move for (int indexAux = startIndex; indexAux <= endIndex; indexAux++) { //I calculate the location based on the destiny axis if (destinyAxis == "row") { calculatedLocation = int.Parse(row.ToString() + indexAux.ToString()); } else { calculatedLocation = int.Parse(indexAux.ToString() + col.ToString()); }; ChessPiece pieceLocation = ChessPieces.Where(p => p.BoardLocation.Id == calculatedLocation).FirstOrDefault(); if (calculatedLocation == destinationBox.Id && pieceLocation != null && pieceLocation.Player == piece.Player) { //if there is an available piece of the same player in the location, //it is a invalid movement return(false); } if (calculatedLocation != destinationBox.Id && pieceLocation != null) { // if there is a available piece in the way to the destiny location, //it is a invalid movement return(false); } } } else { //the destiny location is not in any of the axis of the piece location, //it is a invalid movement return(false); }; //refactor this return(true); }
public Boolean MovementValidation(ChessPiece piece, BoxLocation destinationBox) { //I get the axis of the piece to move int col = (int)Char.GetNumericValue(piece.BoardLocation.Id.ToString()[1]); int row = (int)Char.GetNumericValue(piece.BoardLocation.Id.ToString()[0]); //I get the axis of the destiny location int colDestinyLocation = (int)Char.GetNumericValue(destinationBox.Id.ToString()[1]); int rowDestinyLocation = (int)Char.GetNumericValue(destinationBox.Id.ToString()[0]); switch (piece.ShortName) { case "P": { int rowIndex; List <BoxLocation> posibleLocations = new List <BoxLocation>(); if (piece.Player == 1) { //player WHITE rowIndex = row + 1; } else { //player BLACK rowIndex = row - 1; } posibleLocations = AddPosibleLocation(rowIndex, col, posibleLocations); ChessPiece destinyPiece = ChessPieces.Where(p => p.BoardLocation.Id == destinationBox.Id).FirstOrDefault(); if ((posibleLocations.Count == 0) || // location outside of the board (posibleLocations.Count != 0 && posibleLocations.First().Id != destinationBox.Id) || // destiny not in possible location (posibleLocations.Count != 0 && posibleLocations.First().Id == destinationBox.Id && destinyPiece != null && destinyPiece.Player == piece.Player)) // piece of the same player in the destiny { myChessBoardUI.PrintMessage("Movimiento no sorportado por el momento"); return(false); } else { return(true); } } case "T": { bool rook = ValidateRookMove(colDestinyLocation, rowDestinyLocation, col, row, piece, destinationBox); if (!rook) { myChessBoardUI.PrintMessage("Movimiento invalido"); } return(rook); } case "C": { int startRowIndex; int endRowIndex; int colIndex; bool isTopFlag; List <BoxLocation> posibleLocations = new List <BoxLocation>(); //calculation of the row and col indexs if (row < rowDestinyLocation) { startRowIndex = 1; endRowIndex = 2; colIndex = 2; isTopFlag = true; } else if (row > rowDestinyLocation) { startRowIndex = -2; endRowIndex = -1; colIndex = 1; isTopFlag = false; } else { // destiny location in the same line of the piece myChessBoardUI.PrintMessage("Movimiento invalido"); return(false); } //get possible locations for (int rowIndex = startRowIndex; rowIndex <= endRowIndex; rowIndex++) { //calculation of the right side int r = row + rowIndex; int c = col + colIndex; posibleLocations = AddPosibleLocation(r, c, posibleLocations); //calculation of the left side if (col - colIndex == 0) { c = 1; } else { c = col - colIndex; } posibleLocations = AddPosibleLocation(r, c, posibleLocations); //update index considering the position of the selected location if (isTopFlag) { colIndex = colIndex - 1; } else { colIndex = colIndex + 1; }; } if (posibleLocations.Any(l => l.Id == destinationBox.Id)) { ChessPiece pieceLocation = ChessPieces.Where(p => p.Id == destinationBox.Id).FirstOrDefault();; if ((pieceLocation != null && pieceLocation.Player == piece.Player)) { //There is a piece of the same player in the selected location myChessBoardUI.PrintMessage("Movimiento invalido"); return(false); } } else { //The selected destiny location is not included in the possible locations myChessBoardUI.PrintMessage("Movimiento invalido"); return(false); } return(true); } case "A": { bool bishop = ValidateBishopMove(colDestinyLocation, rowDestinyLocation, col, row, piece, destinationBox); if (!bishop) { myChessBoardUI.PrintMessage("Movimiento invalido"); } return(bishop); } case "Ra": { bool rook = ValidateRookMove(colDestinyLocation, rowDestinyLocation, col, row, piece, destinationBox); if (rook) { return(true); } else { bool bishop = ValidateBishopMove(colDestinyLocation, rowDestinyLocation, col, row, piece, destinationBox); if (bishop) { return(true); } else { myChessBoardUI.PrintMessage("Movimiento invalido"); return(false); } } } case "Ry": { int yAux; int xAux; List <BoxLocation> posibleLocations = new List <BoxLocation>(); //calculation of all the possible locations for (int i = -1; i <= 1; i++) { yAux = row + i; for (int j = -1; j <= 1; j++) { xAux = col + j; //discard the piece's location if (yAux != row && xAux != col) { posibleLocations = AddPosibleLocation(yAux, xAux, posibleLocations); } } ; } //discard locations that have an available piece of the same player for (int i = posibleLocations.Count - 1; i > 0; i--) { ChessPiece pieceLocation = ChessPieces.Where(p => p.BoardLocation == posibleLocations[i]).FirstOrDefault(); if (pieceLocation != null && pieceLocation.Player == piece.Player) { posibleLocations.RemoveAt(i); } ; } if (posibleLocations.Any(l => l.Id == destinationBox.Id)) { //if the destiny location is a possible locations //It is avalid movement return(true); } else { //if the destiny location is NOT a possible locations //It is not a valid movement myChessBoardUI.PrintMessage("Movimiento invalido"); return(false); }; } default: break; } return(true); }
private bool ValidateBishopMove(int colDestinyLocation, int rowDestinyLocation, int col, int row, ChessPiece piece, BoxLocation destinationBox) { int startIndexRow; int startIndexCol; int endIndexRow; int calculatedLocation; List <BoxLocation> posibleLocations = new List <BoxLocation>(); string flag; //calculation of the indexs if (colDestinyLocation > col && rowDestinyLocation > row) { //upward (top) startIndexRow = row + 1; endIndexRow = rowDestinyLocation; startIndexCol = col + 1; flag = "upward"; } else if (colDestinyLocation < col && rowDestinyLocation < row) { //upward (bottom) startIndexRow = rowDestinyLocation; endIndexRow = row - 1; startIndexCol = colDestinyLocation; flag = "upward"; } else if (rowDestinyLocation > row && colDestinyLocation < col) { //downward (top) startIndexRow = row + 1; endIndexRow = rowDestinyLocation; startIndexCol = col - 1; flag = "downward"; } else { //downward (bottom) startIndexRow = rowDestinyLocation; endIndexRow = row - 1; startIndexCol = colDestinyLocation; flag = "downward"; }; //get possible locations int j = startIndexCol; for (int i = startIndexRow; i <= endIndexRow; i++) { calculatedLocation = int.Parse(i.ToString() + j.ToString()); posibleLocations = AddPosibleLocation(i, j, posibleLocations); if (flag == "upward") { j = j + 1; } else { j = j - 1; }; } if (posibleLocations.Any(l => l.Id == destinationBox.Id)) { for (int i = 0; i < posibleLocations.Count; i++) { ChessPiece pieceLocation = ChessPieces.Where(p => p.BoardLocation.Id == posibleLocations[i].Id).FirstOrDefault(); if (posibleLocations[i].Id == destinationBox.Id && pieceLocation != null && pieceLocation.Player == piece.Player) { //if there is an available piece of the same player in the destiny location, //it is an invalid move return(false); } if (posibleLocations[i].Id != destinationBox.Id && pieceLocation != null) { // if there is a available piece in the way to the destiny location, //it is an invalid move return(false); } //if it did not break at the last loop, //it is a valid move if ((i + 1) == posibleLocations.Count) { return(true); } ; } } else { //the selected location is not in the possible locations return(false); }; return(true); }