protected Figure(Cell location, NameFigure name, ColorFigure color, BoardOfChess board) { Location = location; Name = name; Color = color; Board = board; }
/// <summary> /// Проверить находится ли король под шахом /// </summary> /// <param name="whom">цвет короля</param> /// <returns>true-шах\false-нет мата</returns> public bool CheckShah(ColorFigure whom) { //ищем короля Figure king = Figures.FirstOrDefault(f => f.Color == whom && f.Name == NameFigure.King); if (king == null) { throw new Exception(whom + " king is dont exists on board"); } //перебираем вражеские фигуры return (Figures.Where(f => f.Color != whom).Any( f => f.GetMoves().FirstOrDefault(cell => (cell == king.Location)) != null)); }
/// <summary> /// Проверить поставлен ли мат королю /// </summary> /// <param name="whom">Цвет короля</param> /// <returns>true-мат\false- мата нет</returns> public bool CheckMat(ColorFigure whom) { //ищем короля Figure king = Figures.FirstOrDefault(f => f.Color == whom && f.Name == NameFigure.King); if (king == null) { throw new Exception(whom + " king is dont exists on board"); } if (CheckShah(whom)) //Если был шах, то возможен мат { bool isMat = true; foreach (Figure figure in Figures.Where(f => f.Color == whom).ToList()) { foreach (Cell newLocation in figure.GetMoves().ToList()) { Cell oldLocation = figure.Location; //Запоминаем где раньше стояла фигура Figure figureOnNewPlace = figure.Move(newLocation); //передвигаем фигуру на новое возможное место(если там стояла другая фигура вернем ее if (CheckShah(whom) == false) { //если при перемещении фигуры шаха больше нет.. то значит и мата нет isMat = false; } //Возвращаем фигуру на прежнее место figure.Move( oldLocation); if (figureOnNewPlace != null) { AddFigure(figureOnNewPlace); } if (!isMat) { return(false); } } } return(true); } return(false); }
/// <summary> /// Проверить не поставлен ли пат королю /// </summary> /// <param name="whom">цвет короля</param> /// <returns>true-пат, false-пата нет</returns> public bool CheckPat(ColorFigure whom) { Figure king = Figures.FirstOrDefault(f => f.Color == whom && f.Name == NameFigure.King); if (king == null) { throw new Exception(whom + " king is dont exists on board"); } //Проверим что ни одна фигура не насосит шах королю if (!CheckShah(whom)) //Может ли хоть одна фигура нашего короля пойти { bool isPat = true; foreach (Figure figure in Figures.Where(f => f.Color == whom).ToList()) //перебираем свои фигуры { foreach (Cell newLocation in figure.GetMoves()) //получаем список клеток куда фигура могла сходить { Cell oldLocation = figure.Location; //Запоминаем где раньше стояла фигура Figure figureOnNewPlace = figure.Move(newLocation); //передвигаем фигуру на новое возможное место(если там стояла другая фигура вернем ее if (CheckShah(whom) == false) { //если при перемещении фигуры шаха нет значит все оки доки пата нет isPat = false; } //Возвращаем фигуру на прежнее место figure.Move( oldLocation); if (figureOnNewPlace != null) { AddFigure(figureOnNewPlace); } if (!isPat) { return(false); } } } return(true); } return(false); }
/// <summary> /// Функция возьмет шахматную комбпнацию из Console.In и напечатает в Console.Out белым мат\шах\пат\ок /// </summary> public void ReadFromConsole() { for (int i = 0; i < MaxSizeY; i++) { string str = Console.In.ReadLine(); for (int j = 0; j < MaxSizeX; j++) { if (str[j] == '.') { continue; } ColorFigure color = (str[j] >= 'A' && str[j] <= 'Z') ? ColorFigure.White : ColorFigure.Black; switch (str[j].ToString().ToUpper()) { case "N": AddFigure(new Nag(new Cell(j, i), color, this)); break; case "B": AddFigure(new Bishop(new Cell(j, i), color, this)); break; case "R": AddFigure(new Rook(new Cell(j, i), color, this)); break; case "Q": AddFigure(new Quine(new Cell(j, i), color, this)); break; case "K": AddFigure(new King(new Cell(j, i), color, this)); break; } } } }
protected INMFigure(Cell location, NameFigure name, ColorFigure color, BoardOfChess board) : base(location, name, color, board) { }
public Bishop(Cell location, ColorFigure color, BoardOfChess board) : base(location, NameFigure.Bishop, color, board) { }
public Rook(Cell location, ColorFigure color, BoardOfChess board) : base(location, NameFigure.Rook, color, board) { }
public Quine(Cell location, ColorFigure color, BoardOfChess board) : base(location, NameFigure.Quine, color, board) { }
public Nag(Cell location, ColorFigure color, BoardOfChess board) : base(location, NameFigure.Nag, color, board) { }