protected void FindFieldsInColumn(ChessBoard board, FieldStatus ownStatus) { char column = CurrentField.Column; int row = CurrentField.Row - 1; while (row > 0) { ChessField field = board.GetField(row, column); if (field.Status != ownStatus) { FieldsToMove.Add(field); } if (field.Status != FieldStatus.EMPTY) { break; } row--; } row = CurrentField.Row + 1; while (row <= 8) { ChessField field = board.GetField(row, column); if (field.Status != ownStatus) { FieldsToMove.Add(field); } if (field.Status != FieldStatus.EMPTY) { break; } row++; } }
protected void FindFieldsInRow(ChessBoard board, FieldStatus ownStatus) { int row = CurrentField.Row; char column = (char)((int)CurrentField.Column - 1); while ((int)column >= (int)'A') { ChessField field = board.GetField(row, column); if (field.Status != ownStatus) { FieldsToMove.Add(field); } if (field.Status != FieldStatus.EMPTY) { break; } column--; } column = (char)((int)CurrentField.Column + 1); while ((int)column <= (int)'H') { ChessField field = board.GetField(row, column); if (field.Status != ownStatus) { FieldsToMove.Add(field); } if (field.Status != FieldStatus.EMPTY) { break; } column++; } }
public override void FindFieldsToMove(ChessBoard board) { if (IsKilled) { return; } FieldsToMove.Clear(); FieldStatus ownStatus = Color == Color.WHITE ? FieldStatus.OCCUPIED_WITH_WHITE : FieldStatus.OCCUPIED_WITH_BLACK; int row; char column = '\0'; for (int i = 0; i < 8; i++) { if (i < 2) { row = CurrentField.Row + 2; } else if (i < 4) { row = CurrentField.Row + 1; } else if (i < 6) { row = CurrentField.Row - 1; } else { row = CurrentField.Row - 2; } if (i == 2 || i == 4) { column = (char)(CurrentField.Column - 2); } else if (i == 0 || i == 6) { column = (char)(CurrentField.Column - 1); } else if (i == 1 || i == 7) { column = (char)(CurrentField.Column + 1); } else if (i == 3 || i == 5) { column = (char)(CurrentField.Column + 2); } if (row < 1 || row > 8 || column < 'A' || column > 'H') { continue; } ChessField field = board.GetField(row, column); if (field.Status != ownStatus) { FieldsToMove.Add(field); } } }
public bool Check() { ChessPlayer enemy = CurrentPlayer == WhitePlayer ? BlackPlayer : WhitePlayer; Color enemyColor = CurrentPlayer == WhitePlayer ? Color.BLACK : Color.WHITE; ChessKing enemyKing = enemy.Figures.OfType <ChessKing>().First(); ChessField enemyKingField = enemyKing.CurrentField; if (IsFieldUnderAttack(enemyColor, enemyKingField.Row, enemyKingField.Column)) { return(true); } return(false); }
public override void FindFieldsToMove(ChessBoard board) { if (IsKilled) { return; } FieldsToMove.Clear(); FieldStatus enemyStatus = Color == Color.WHITE ? FieldStatus.OCCUPIED_WITH_BLACK : FieldStatus.OCCUPIED_WITH_WHITE; int row = Color == Color.WHITE ? CurrentField.Row + 1 : CurrentField.Row - 1; if (row < 1 || row > 8) { return; } ChessField field = board.GetField(row, CurrentField.Column); if (field.Status == FieldStatus.EMPTY) { FieldsToMove.Add(field); } char column = (char)(CurrentField.Column - 1); field = board.GetField(row, column); if (field.Status == enemyStatus) { FieldsToMove.Add(field); } column = (char)(CurrentField.Column + 1); field = board.GetField(row, column); if (field.Status == enemyStatus) { FieldsToMove.Add(field); } if (PreviousMoves.Count == 0) { row = Color == Color.WHITE ? CurrentField.Row + 2 : CurrentField.Row - 2; field = board.GetField(row, CurrentField.Column); if (field.Status == FieldStatus.EMPTY) { FieldsToMove.Add(field); } } }
public override void FindFieldsToMove(ChessBoard board) { FieldsToMove.Clear(); FieldStatus ownStatus = Color == Color.WHITE ? FieldStatus.OCCUPIED_WITH_WHITE : FieldStatus.OCCUPIED_WITH_BLACK; List <int> potentialRows = new List <int>(); if (CurrentField.Row != 1) { potentialRows.Add(CurrentField.Row - 1); } potentialRows.Add(CurrentField.Row); if (CurrentField.Row != 8) { potentialRows.Add(CurrentField.Row + 1); } List <char> potentialColumns = new List <char>(); if (CurrentField.Column != 'A') { potentialColumns.Add((char)((int)CurrentField.Column - 1)); } potentialColumns.Add(CurrentField.Column); if (CurrentField.Column != 'H') { potentialColumns.Add((char)((int)CurrentField.Column + 1)); } foreach (int row in potentialRows) { foreach (char column in potentialColumns) { ChessField field = board.GetField(row, column); if (field.Status != ownStatus) { FieldsToMove.Add(field); } } } }
public virtual void Move(ChessField field, ChessPlayer enemy, ChessBoard board, int moveNumber, ChessFigure[] otherFigures) { FieldStatus enemyStatus = Color == Color.WHITE ? FieldStatus.OCCUPIED_WITH_BLACK : FieldStatus.OCCUPIED_WITH_WHITE; ChessFigure killedFigure = null; if (field.Status == enemyStatus) { foreach (ChessFigure enemyFigure in enemy.Figures) { if (enemyFigure.CurrentField == field) { enemyFigure.FigureKilled(); killedFigure = enemyFigure; break; } if (enemyFigure is ChessPawn && ((ChessPawn)enemyFigure).EnPassant && enemyFigure.CurrentField.Column == field.Column) { if ((Color == Color.WHITE && field.Row == 6 && ((ChessPawn)enemyFigure).EnPassantMove == moveNumber - 1) || (Color == Color.BLACK && field.Row == 3 && ((ChessPawn)enemyFigure).EnPassantMove == moveNumber)) { enemyFigure.FigureKilled(); killedFigure = enemyFigure; break; } } } } ChessMove move = new ChessMove(Color, moveNumber, this, killedFigure, CurrentField, field); PreviousMoves.Add(move); board.EmptyField(CurrentField); board.OccupyField(field.Row, field.Column, Color); CurrentField = field; FindFieldsToMove(board); }
public override void Move(ChessField field, ChessPlayer enemy, ChessBoard board, int moveNumber, ChessFigure[] otherFigures) { if (PreviousMoves.Count == 0) { if ((Color == Color.WHITE && field.Row == 4) || (Color == Color.BLACK && field.Row == 5)) { EnPassant = true; EnPassantMove = moveNumber; } } else { EnPassant = false; } base.Move(field, enemy, board, moveNumber, otherFigures); if ((Color == Color.WHITE && field.Row == 8) || (Color == Color.BLACK && field.Row == 1)) { int pawnIndex = 16; for (var i = 0; i < otherFigures.Length; i++) { var figure = otherFigures[i]; if (this == figure) { pawnIndex = i; } } ChessFigure newFigure; DialogResult result = MessageBox.Show("Do you want to turn into Queen?", "Pawn To Queen", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { newFigure = new ChessQueen(Color, board); } else { result = MessageBox.Show("Do you want to turn into Rook?", "Pawn To Rook", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { newFigure = new ChessRook(Color, board, false); } else { result = MessageBox.Show("Do you want to turn into Knight?", "Pawn To Knight", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { newFigure = new ChessKnight(Color, board, false); } else { newFigure = new ChessBishop(Color, board, false); } } } newFigure.CurrentField = field; newFigure.PreviousMoves = PreviousMoves; newFigure.FindFieldsToMove(board); otherFigures[pawnIndex] = newFigure; } }
public bool CheckMate() { bool isMate = false; ChessPlayer enemy = CurrentPlayer == WhitePlayer ? BlackPlayer : WhitePlayer; Color enemyColor = CurrentPlayer == WhitePlayer ? Color.BLACK : Color.WHITE; ChessKing enemyKing = enemy.Figures.OfType <ChessKing>().First(); ChessField enemyKingField = enemyKing.CurrentField; if (IsFieldUnderAttack(enemyColor, enemyKingField.Row, enemyKingField.Column)) { isMate = true; //1. King can move to other field List <ChessField> kingToMoveFields = enemyKing.FieldsToMove; foreach (ChessField field in kingToMoveFields) { if (!IsFieldUnderAttack(enemyColor, field.Row, field.Column)) { isMate = false; break; } } //2. Attacking figures can be killed List <ChessFigure> currentFigures = CurrentPlayer.Figures.Where(f => !f.IsKilled).ToList(); List <ChessFigure> attackingFigures = new List <ChessFigure>(); foreach (ChessFigure figure in currentFigures) { if (figure.FieldsToMove.Contains(enemyKingField)) { attackingFigures.Add(figure); } } List <ChessFigure> attackingToKill = new List <ChessFigure>(); foreach (ChessFigure attackingFigure in attackingFigures) { foreach (ChessFigure defendingFigure in enemy.Figures.Where(f => !f.IsKilled)) { if (defendingFigure.FieldsToMove.Contains(attackingFigure.CurrentField)) { attackingToKill.Add(attackingFigure); } } } foreach (ChessFigure figure in attackingToKill) { attackingFigures.Remove(figure); } //3. It's possible to move figure between attacking figure and king foreach (ChessFigure attackingFigure in attackingFigures) { List <ChessField> fieldsBetween = new List <ChessField>(); if (attackingFigure is ChessRook || attackingFigure is ChessQueen) { if (enemyKingField.Column == attackingFigure.CurrentField.Column) { int minRow = Math.Min(enemyKingField.Row, attackingFigure.CurrentField.Row); int maxRow = Math.Max(enemyKingField.Row, attackingFigure.CurrentField.Row); for (int i = minRow + 1; i < maxRow; i++) { fieldsBetween.Add(Board.GetField(i, enemyKingField.Column)); } } else if (enemyKingField.Row == attackingFigure.CurrentField.Row) { char minColumn = (char)Math.Min(enemyKingField.Column, attackingFigure.CurrentField.Column); char maxColumn = (char)Math.Max(enemyKingField.Column, attackingFigure.CurrentField.Column); for (int i = minColumn + 1; i < maxColumn; i++) { fieldsBetween.Add(Board.GetField(enemyKingField.Row, (char)i)); } } } if (attackingFigure is ChessBishop || attackingFigure is ChessQueen) { int minRow = Math.Min(enemyKingField.Row, attackingFigure.CurrentField.Row); int maxRow = Math.Max(enemyKingField.Row, attackingFigure.CurrentField.Row); char minColumn = (char)Math.Min(enemyKingField.Column, attackingFigure.CurrentField.Column); char maxColumn = (char)Math.Max(enemyKingField.Column, attackingFigure.CurrentField.Column); if ((enemyKingField.Row < attackingFigure.CurrentField.Row && enemyKingField.Column < attackingFigure.CurrentField.Column) || (enemyKingField.Row > attackingFigure.CurrentField.Row && enemyKingField.Column > attackingFigure.CurrentField.Column)) { for (int i = minRow + 1, j = minColumn + 1; i < maxRow && j < maxColumn; i++, j++) { fieldsBetween.Add(Board.GetField(i, (char)j)); } } else { for (int i = maxRow - 1, j = minColumn + 1; i > minRow && j < maxColumn; i--, j++) { fieldsBetween.Add(Board.GetField(i, (char)j)); } } } List <ChessFigure> enemyFigures = enemy.Figures.Where(f => f != enemyKing).ToList(); foreach (ChessField field in fieldsBetween) { foreach (var figure in enemyFigures) { if (figure.FieldsToMove.Contains(field)) { attackingFigures.Remove(attackingFigure); } } } } if (attackingFigures.Count < 1) { isMate = false; } } return(isMate); }