Пример #1
0
 public bool canIeat(ChessBoard board, Coords start, Coords end)
 {
     if (board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(true);
     }
     return(false);
 }
Пример #2
0
        public bool DiagonalMoves(ChessBoard board, Coords start, Coords end)
        {
            if (Math.Abs(start.getX() - end.getX()) == Math.Abs(start.getY() - end.getY()))
            {
                if (start.getY() > end.getY() && start.getX() > end.getX())
                {//y--x--
                    for (int tempStartY = start.getY() - 1, tempStartX = start.getX() - 1; tempStartY > end.getY() && tempStartX > end.getX(); tempStartY--, tempStartX--)
                    {
                        if (board.GetSoldierByPosition(new Coords(tempStartY, tempStartX)).getColor() != " ")
                        {
                            return(false);  // If there is one (not empty) movement isn't allowed
                        }
                    }
                }
                else if (start.getY() > end.getY() && start.getX() < end.getX())
                {//y--x++
                    for (int tempStartY = start.getY() - 1, tempStartX = start.getX() + 1; tempStartY > end.getY() && tempStartX < end.getX(); tempStartY--, tempStartX++)
                    {
                        if (board.GetSoldierByPosition(new Coords(tempStartY, tempStartX)).getColor() != " ")
                        {
                            return(false);  // If there is one (not empty) movement isn't allowed
                        }
                    }
                }
                else if (start.getY() < end.getY() && start.getX() < end.getX())
                {//y++x++
                    for (int tempStartY = start.getY() + 1, tempStartX = start.getX() + 1; tempStartY > end.getY() && tempStartX < end.getX(); tempStartY++, tempStartX++)
                    {
                        if (board.GetSoldierByPosition(new Coords(tempStartY, tempStartX)).getColor() != " ")
                        {
                            return(false);  // If there is one (not empty) movement isn't allowed
                        }
                    }
                }
                else if (start.getY() < end.getY() && start.getX() > end.getX())
                {//y++x--
                    for (int tempStartY = start.getY() + 1, tempStartX = start.getX() - 1; tempStartY > end.getY() && tempStartX < end.getX(); tempStartY++, tempStartX--)
                    {
                        if (board.GetSoldierByPosition(new Coords(tempStartY, tempStartX)).getColor() != " ")
                        {
                            return(false);  // If there is one (not empty) movement isn't allowed
                        }
                    }
                }

                return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
            }
            return(false);
        }
Пример #3
0
        public bool PawnMove(ChessBoard board, Coords start, Coords end)
        {
            int direction;

            if (board.WhiteTurn())
            {
                direction = -1;
            }
            else
            {
                direction = 1;
            }

            if (!this.getMoved() &&//first move 2 steps
                start.getX() == end.getX() &&
                start.getY() + (2 * direction) == end.getY() &&
                board.GetSoldierByPosition(new Coords(start.getY() + (2 * direction), end.getX())).getType() == " ")
            {
                firstMoveTwoSteps = true;
                return(true);
            }
            if (start.getX() == end.getX() &&//move 1 step
                start.getY() + direction == end.getY() &&
                board.GetSoldierByPosition(new Coords(start.getY() + direction, end.getX())).getType() == " ")
            {
                return(true);
            }
            if ((start.getX() + 1 == end.getX() || start.getX() - 1 == end.getX()) &&//eating
                start.getY() + direction == end.getY() &&
                board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor() &&
                board.GetSoldierByPosition(end).getColor() != " ")
            {
                return(canIeat(board, start, end));
            }
            if (board.validEnPassant(start, end))//en passant
            {
                return(true);
            }
            return(false);
        }
Пример #4
0
 public bool StraightLines(ChessBoard board, Coords start, Coords end)
 {
     if (start.getX() == end.getX())
     {
         if (start.getY() < end.getY())
         {
             for (int i = start.getY() + 1; i < end.getY(); i++)
             {
                 if (board.GetSoldierByPosition(new Coords(i, start.getX())).getColor() != " ")
                 {
                     return(false);  // If there is one (not empty) movement isn't allowed
                 }
             }
         }
         else
         {
             for (int i = start.getY() - 1; i > end.getY(); i--)
             {
                 if (board.GetSoldierByPosition(new Coords(i, start.getX())).getColor() != " ")
                 {
                     return(false);
                 }
             }
         }
         return(canIeat(board, start, end) || board.GetSoldierByPosition(end).getColor() == " ");
     }
     else if (start.getY() == end.getY())
     {
         if (start.getX() < end.getX())
         {
             for (int i = start.getX() + 1; i < end.getX(); i++)
             {
                 if (board.GetSoldierByPosition(new Coords(start.getY(), i)).getColor() != " ")
                 {
                     return(false);
                 }
             }
         }
         else
         {
             for (int i = start.getX() - 1; i > end.getX(); i--)
             {
                 if (board.GetSoldierByPosition(new Coords(start.getY(), i)).getColor() != " ")
                 {
                     return(false);
                 }
             }
         }
         return(canIeat(board, start, end) || board.GetSoldierByPosition(end).getColor() == " ");
     }
     return(false);
 }
Пример #5
0
        public void run()
        {
            bool       valid = false;
            ChessBoard c     = new ChessBoard();

            c.initSoldiers();
            // !c.checkMate(c.FindKing())

            c.PrintBoard();
            while (!valid)
            {
                Console.WriteLine("Please enter start axis Y, start axis X, end axis Y, end axis X");
                Console.WriteLine("{0}", (c.WhiteTurn() ? "White its your turn:" : "Black its your turn:"));
                string input = Console.ReadLine().Trim(' ');
                if (input.Length == 4)
                {
                    Coords start     = new Coords(c.ConvertY(input[0]), c.ConvertX(input[1]));
                    Coords end       = new Coords(c.ConvertY(input[2]), c.ConvertX(input[3]));
                    bool   validTurn = false;
                    if ((c.WhiteTurn() && c.GetSoldierByPosition(start).getColor() == "W"))
                    {
                        validTurn = true;
                    }
                    if (!(c.WhiteTurn()) && c.GetSoldierByPosition(start).getColor() == "B")
                    {
                        validTurn = true;
                    }

                    if (c.ValidInput(start, end) && validTurn && c.PossibleToCancelCheck(start, end))
                    {
                        if (c.validCastling(start, end) && c.GetSoldierByPosition(start).validMove(c, start, end))

                        {
                            c.doCastlingMove(start, end);
                            c.nextTurn();
                        }
                        else if (c.GetSoldierByPosition(start).validMove(c, start, end))
                        {
                            if (c.GetSoldierByPosition(start).getType() == "P" ||
                                (c.GetSoldierByPosition(start).getColor() != c.GetSoldierByPosition(end).getColor() && c.GetSoldierByPosition(end).getColor() != " ") ||
                                c.inCheck(c.FindKing()))

                            {
                                c.setDrawCounter(0);
                            }

                            if (c.validPawnPromotion(start, end))
                            {
                                c.doPromotion(start, end);
                                c.nextTurn();
                            }
                            else if (c.validEnPassant(start, end))
                            {
                                c.doEnPassant(start, end);
                                c.nextTurn();
                            }
                            else
                            {
                                c.basicMove(start, end);
                                c.nextTurn();
                            }
                        }
                    }
                }
                if (c.isDraw(c.FindKing()))
                {
                    Console.WriteLine("Draw");
                    valid = true;
                    break;
                }
                if (c.checkMate(c.FindKing()))
                {
                    Console.WriteLine((!(c.WhiteTurn()) ? "White won" : "Black won"));
                    valid = true;
                }
            }
        }
Пример #6
0
 public bool rMoves(ChessBoard board, Coords start, Coords end)
 {
     if (start.getX() + 2 == end.getX() && start.getY() + 1 == end.getY() &&//x+2//y+1
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (start.getX() + 2 == end.getX() && start.getY() - 1 == end.getY() &&//x+2//y-1
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (start.getX() - 2 == end.getX() && start.getY() + 1 == end.getY() &&//x-2//y+1
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (start.getX() - 2 == end.getX() && start.getY() - 1 == end.getY() &&//x-2//y-1
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (start.getX() - 1 == end.getX() && start.getY() - 2 == end.getY() &&//x-1//y-2
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (start.getX() + 1 == end.getX() && start.getY() - 2 == end.getY() &&//x+1//y-2
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (start.getX() - 1 == end.getX() && start.getY() + 2 == end.getY() &&//x-1//y+2
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (start.getX() + 1 == end.getX() && start.getY() + 2 == end.getY() &&//x+1//y+2
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     return(false);
 }
Пример #7
0
 public bool KingMoves(ChessBoard board, Coords start, Coords end)
 {
     if (start.getX() + 1 == end.getX() && start.getY() + 1 == end.getY() &&//x+1//y+1
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (start.getX() + 1 == end.getX() && start.getY() - 1 == end.getY() &&//x+1//y-1
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (start.getX() - 1 == end.getX() && start.getY() + 1 == end.getY() &&//x-1//y+1
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (start.getX() - 1 == end.getX() && start.getY() - 1 == end.getY() &&//x-1//y-1
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }//
     if (start.getX() == end.getX() && start.getY() - 1 == end.getY() &&//x//y-1
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (start.getX() == end.getX() && start.getY() + 1 == end.getY() &&//x//y+1
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (start.getX() - 1 == end.getX() && start.getY() == end.getY() &&//x-1//y
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (start.getX() + 1 == end.getX() && start.getY() == end.getY() &&//x+1//y
         board.GetSoldierByPosition(start).getColor() != board.GetSoldierByPosition(end).getColor())
     {
         return(board.GetSoldierByPosition(end).getColor() == " " || canIeat(board, start, end));
     }
     if (board.validCastling(start, end))
     {
         return(true);
     }
     return(false);
 }