Ejemplo n.º 1
0
 public bool CanCaptureByEnPassant(ChessGameField.Cell[,] field, ChessGameField.Cell cellTo, Point posFrom, Point posTo, ChessMove lastMove)
 {
     if (cellTo.figure == null && (posFrom.X == posTo.X - 1 || posFrom.X == posTo.X + 1)) // En passant
     {
         if ((GetOwner() == Player.White && posFrom.Y == posTo.Y + 1)
             || (GetOwner() == Player.Black && posFrom.Y == posTo.Y - 1))
         {
             Figure opponentsFigure = field[posTo.X, posFrom.Y].figure;
             if (opponentsFigure is Pawn && opponentsFigure.GetOwner() != GetOwner())
             {
                 Pawn opponentsPawn = (Pawn)opponentsFigure;
                 if (lastMove.Fig == opponentsPawn && Math.Abs(lastMove.GetPrevPos().Y - posFrom.Y) == 2)
                 {
                     return true;
                 }
             }
         }
     }
     return false;
 }
Ejemplo n.º 2
0
        public bool CanMove(ChessGameField.Cell[,] field, Point posFrom, Point posTo, ChessMove lastMove)
        {
            ChessGameField.Cell cellTo = field[posTo.X, posTo.Y];
            if (CanMoveOneStep(cellTo, posFrom, posTo))
            {
                return true;
            }
            else if (CanMoveTwoSteps(cellTo, posFrom, posTo))
            {
                return true;
            }
            else if (CanCapture(cellTo, posFrom, posTo))
            {
                return true;
            }
            else if (CanCaptureByEnPassant(field, cellTo, posFrom, posTo, lastMove))
            {
                return true;
            }

            return false;
        }
Ejemplo n.º 3
0
        public ChessMove Move(ref ChessGameField.Cell[,] field, Point posFrom, Point posTo, ChessMove lastMove)
        {
            ChessGameField.Cell cellTo = field[posTo.X, posTo.Y];
            ChessMove move = new ChessMove();
            if (CanMoveOneStep(cellTo, posFrom, posTo))
            {
                move.AddAction(new ChessMove.MoveAction(posFrom, posTo));
                return move;
            }
            else if (CanMoveTwoSteps(cellTo, posFrom, posTo))
            {
                move.AddAction(new ChessMove.MoveAction(posFrom, posTo));
                return move;
            }
            else if (CanCapture(cellTo, posFrom, posTo))
            {
                move.AddAction(new ChessMove.RemoveAction(posTo));
                move.AddAction(new ChessMove.MoveAction(posFrom, posTo));
                return move;
            }
            else if (CanCaptureByEnPassant(field, cellTo, posFrom, posTo, lastMove))
            {
                move.AddAction(new ChessMove.RemoveAction(new Point(posTo.X, posFrom.Y)));
                move.AddAction(new ChessMove.MoveAction(posFrom, posTo));
                return move;
            }

            return null;
        }
Ejemplo n.º 4
0
        public bool MakeMove(Player player, Point from, Point to)
        {
            if (!IsPosValid(from) || !IsPosValid(to) || (from.X == to.X && from.Y == to.Y))
            {
                return false;
            }

            ChessFigure figure = GetCell(from).figure;
            if (figure != null && figure.GetOwner() == player)
            {
                ChessMove move = figure.Move(ref Field, from, to, LastMove);
                if (move != null)
                {
                    move.Do(ref Field, figure);

                    if (IsKingInCheck(player))
                    {
                        move.UnDo(ref Field);
                        return false;
                    }

                    LastMove = move;
                    return true;
                }
            }

            return false;
        }