Пример #1
0
 public Move(Position pOld, Position pNew)
 {
     this.previousPosition = pOld;
     this.newPosition = pNew;
 }
Пример #2
0
 private static bool isValidPosition(Position pos)
 {
     return ((pos.Row >= 0 && pos.Row <= 7) && (pos.Col >= 0 && pos.Col <= 7));
 }
Пример #3
0
 public void handleClick(object sender, EventArgs args)
 {
     if (isHighlighted())
     {
         Position old = currentHighlight;
         makeMove(old, this.Pos);
     }
     else
     {
         clearHighlights();
         currentHighlight = new Position(this.Row, this.Col);
         Console.WriteLine(currentHighlight);
         toggleHighlight();
         highlightPossiblePositions();
     }
 }
Пример #4
0
        public static List<Position> rookTransform(Position pos)
        {
            Position temp;
            List<Position> transform = new List<Position>();

            for(int i=pos.Row+1;i<8;i++)
            {
                temp = new Position(i,pos.Col);
                if (isEmptyPosition(temp))
                    transform.Add(temp);
                else
                {
                    if (isCapturePosition(temp,pos))
                        transform.Add(temp);
                    break;
                }
            }
            for (int i = pos.Row - 1; i >= 0; i--)
            {
                temp = new Position(i, pos.Col);
                if (isEmptyPosition(temp))
                    transform.Add(temp);
                else
                {
                    if (isCapturePosition(temp,pos))
                        transform.Add(temp);
                    break;
                }
            }
            for (int i = pos.Col + 1; i < 8; i++)
            {
                temp = new Position(pos.Row, i);
                if (isEmptyPosition(temp))
                    transform.Add(temp);
                else
                {
                    if (isCapturePosition(temp,pos))
                        transform.Add(temp);
                    break;
                }
            }
            for (int i = pos.Col - 1; i >= 0; i--)
            {
                temp = new Position(pos.Row, i);
                if (isEmptyPosition(temp))
                    transform.Add(temp);
                else
                {
                    if (isCapturePosition(temp,pos))
                        transform.Add(temp);
                    break;
                }
            }
            return transform;
        }
Пример #5
0
        public static List<Position> horseTransform(Position pos)
        {
            List<Position> transform = new List<Position>();
            int x = 1, y = 2, count = 0;
            while (count < 2)
            {
                transform.Add(new Position(pos.Row + x, pos.Col + y));
                transform.Add(new Position(pos.Row - x, pos.Col + y));
                transform.Add(new Position(pos.Row + x, pos.Col - y));
                transform.Add(new Position(pos.Row - x, pos.Col - y));
                swap(ref x,ref y);
                count++;
            }

            List<Position> finalList = new List<Position>();
            //check if all the transforms are valid positions and empty
            foreach(Position t in transform)
            {
                if(isValidPosition(t) && (squares[t.Row,t.Col].isEmpty() || isCapturePosition(t,pos)))
                {
                    finalList.Add(t);
                }
            }
            transform.Clear();
            return finalList;
        }
Пример #6
0
 public static void makeMove(Position oldPosition, Position newPosition)
 {
     ChessPiece piece = squares[oldPosition.Row, oldPosition.Col].getPiece(); // obtained a reference to the checkpiece that's selected.
     if (oldPosition == newPosition) // stupid or accidental maybe ?
         return;
     if ((isWhitesTurn && getPlayerSide(piece) == Player.White) || (!isWhitesTurn && getPlayerSide(piece) == Player.Black))
     {
         ChessPiece capturedPiece = squares[newPosition.Row,newPosition.Col].getPiece();
         if(capturedPiece!=ChessPiece.None)
         {
             // enemy piece captured.
             // TODO: Add the captured pieces to the opponent's capture List.
         }
         clearHighlights();
         squares[oldPosition.Row, oldPosition.Col].removeImage();
         squares[newPosition.Row, newPosition.Col].setPiece(piece);
         // TODO: king in check ? notify by highlighting in red.
         kingInCheck(newPosition);
         isWhitesTurn = !isWhitesTurn;
         updateStatus();
         lastMove = new Move(oldPosition,newPosition);
     }
     else
     {
         setErrorHighlight();
         highlightStatus();
     }
 }
Пример #7
0
        public static void kingInCheck(Position position)  // checks if the piece in the provided position poses a check to the opposite king.
        {
            ChessPiece piece = squares[position.Row, position.Col].getPiece(); // the piece which had been moved now.
            ChessPiece enemyKing;
            Position enemyKingPosition = null ;
            if (getPlayerSide(piece) == Player.White)
                enemyKing = ChessPiece.BlackKing;
            else
                enemyKing = ChessPiece.WhiteKing;

            // getting position of enemy king.
            foreach(Position p in pieceMap.Keys)
            {
                if(pieceMap[p] == enemyKing)
                {
                    Debug.Print("Position : "+p);
                    enemyKingPosition = p;
                    break;
                }
            }

            if(enemyKingPosition == null)
            {
                setErrorHighlight();
                Debug.Print("cidjf");
                setStatus("[Debug] FATAL: Enemy King position cannot be determined");
                return;
            }

            foreach(Position possiblePosition in getPossiblePositions(position))
            {
                Debug.Print("Position : " + possiblePosition);
                if(possiblePosition == enemyKingPosition)
                {
                    setStatus("King in check!");
                    // setting check info - required later for checking. (and ofcourse, removing checkHighlight)
                    if (info == null)
                        info = new CheckInfo();
                    info.threatenedBy = position;
                    info.pieceInCheck = enemyKingPosition;
                    //TODO: while making move check both if the move leads to a check or if the king is in check, the move should clear the check.
                    // ---
                    isKingInCheck = true;
                    squares[enemyKingPosition.Row, enemyKingPosition.Col].setCheckHighlight();
                }
            }
        }
Пример #8
0
        private static List<Position> kingTransform(Position pos)
        {
            List<Position> transform = new List<Position>();
            List<Position> temp = new List<Position>();
            temp.Add(pos + new Position(1, 1));
            temp.Add(pos + new Position(1, -1));
            temp.Add(pos + new Position(-1, 1));
            temp.Add(pos + new Position(-1, -1));
            temp.Add(pos + new Position(0, 1));
            temp.Add(pos + new Position(1, 0));
            temp.Add(pos + new Position(-1, 0));
            temp.Add(pos + new Position(0, -1));

            foreach(Position t in temp)
            {
                if(isValidPosition(t))
                {
                    if (isEmptyPosition(t) || isCapturePosition(t,pos))
                        transform.Add(t);
                }
            }
            temp.Clear();
            return transform;
        }
Пример #9
0
        private static List<Position> bishopTransform(Position pos)
        {
            Position[] temp = new Position[4];
            List<Position> transform = new List<Position>();
            bool flag0 = false, flag1 = false, flag2 = false, flag3 = false;

            for (int i = 1; i < 8; i++)
            {
                if (!flag0)
                    temp[0] = pos + new Position(i, i);
                else
                    temp[0] = new Position(-1,-1);
                if(!flag1)
                    temp[1] = pos + new Position(i, -1 * i);
                else
                    temp[1] = new Position(-1, -1);
                if(!flag2)
                    temp[2] = pos + new Position(i * -1, i);
                else
                    temp[2] = new Position(-1, -1);
                if(!flag3)
                    temp[3] = pos + new Position(i * -1, i * -1);
                else
                    temp[3] = new Position(-1, -1);
                for (int j = 0; j < temp.Length; j++)
                {
                    if (isValidPosition(temp[j]))
                    {
                        if (isEmptyPosition(temp[j]))
                            transform.Add(temp[j]);
                        else if (isCapturePosition(temp[j],pos))
                        {
                            transform.Add(temp[j]);
                            switch(j)
                            {
                                case 0:
                                    flag0 = true;
                                    break;
                                case 1:
                                    flag1 = true;
                                    break;
                                case 2:
                                    flag2 = true;
                                    break;
                                case 3:
                                    flag3 = true;
                                    break;
                            }
                        }
                        else
                        {
                            switch (j)
                            {
                                case 0:
                                    flag0 = true;
                                    break;
                                case 1:
                                    flag1 = true;
                                    break;
                                case 2:
                                    flag2 = true;
                                    break;
                                case 3:
                                    flag3 = true;
                                    break;
                            }
                        }
                    }
                }
            }
            return transform;
        }
Пример #10
0
 public static List<Position> getPossiblePositions(Position pos)
 {
     ChessPiece piece = squares[pos.Row, pos.Col].getPiece();
     List<Position> nPossiblePositions = new List<Position>();
     switch (piece)
     {
         case ChessPiece.None:
             break;
         case ChessPiece.WhitePawn:
             Position possiblePos;
             if (pos.Row == 1) // check if pawn is in initial position
             {
                 possiblePos = new Position(pos.Row + 2, pos.Col);
                 if (isEmptyPosition(possiblePos) && isEmptyPosition(possiblePos + new Position(-1, 0)))
                 {
                     nPossiblePositions.Add(possiblePos);
                 }
             }
             possiblePos = new Position(pos.Row + 1, pos.Col);
             if (isEmptyPosition(possiblePos))
             {
                 nPossiblePositions.Add(possiblePos);
             }
             // TODO:Add pawn cross capture to possible moves here...
             possiblePos = new Position(pos.Row + 1, pos.Col - 1);
             if (isValidPosition(possiblePos) && isCapturePosition(possiblePos,pos))
             {
                 nPossiblePositions.Add(possiblePos);
             }
             possiblePos = new Position(pos.Row + 1, pos.Col + 1);
             if (isValidPosition(possiblePos) && isCapturePosition(possiblePos,pos))
             {
                 nPossiblePositions.Add(possiblePos);
             }
             break;
         case ChessPiece.BlackPawn:
             //Position possiblePos;
             if (pos.Row == 6) // check if pawn is in initial position
             {
                 possiblePos = new Position(pos.Row - 2, pos.Col);
                 if (isEmptyPosition(possiblePos) && isEmptyPosition(possiblePos + new Position(1, 0))) // path should be clear when pawn is going to move 2 steps
                 {
                     nPossiblePositions.Add(possiblePos);
                 }
             }
             possiblePos = new Position(pos.Row - 1, pos.Col);
             if (isEmptyPosition(possiblePos))
             {
                 nPossiblePositions.Add(possiblePos);
             } possiblePos = new Position(pos.Row - 1, pos.Col - 1);
             if (isValidPosition(possiblePos) && isCapturePosition(possiblePos,pos))
             {
                 nPossiblePositions.Add(possiblePos);
             }
             possiblePos = new Position(pos.Row - 1, pos.Col + 1);
             if (isValidPosition(possiblePos) && isCapturePosition(possiblePos,pos))
             {
                 nPossiblePositions.Add(possiblePos);
             }
             break;
         case ChessPiece.WhiteHorse:
         case ChessPiece.BlackHorse:
             nPossiblePositions.AddRange(horseTransform(pos));
             break;
         case ChessPiece.BlackRook:
         case ChessPiece.WhiteRook:
             nPossiblePositions.AddRange(rookTransform(pos));
             break;
         case ChessPiece.WhiteBishop:
         case ChessPiece.BlackBishop:
             nPossiblePositions.AddRange(bishopTransform(pos));
             break;
         case ChessPiece.BlackQueen:
         case ChessPiece.WhiteQueen:
             nPossiblePositions.AddRange(bishopTransform(pos));
             nPossiblePositions.AddRange(rookTransform(pos));
             break;
         case ChessPiece.BlackKing:
         case ChessPiece.WhiteKing:
             nPossiblePositions.AddRange(kingTransform(pos));
             break;
     }
     return nPossiblePositions;
 }
Пример #11
0
        public Form1()
        {
            InitializeComponent();
            isWhitesTurn = true;
            isKingInCheck = false;
            currentHighlight = null;
            possiblePositions = new List<Position>();
            pieceMap = new Dictionary<Position, ChessPiece>();
            statusLabel = new ToolStripLabel();
            info = null;
            lastMove = null;    // a reference for the last Position.

            //adding status label to the status strip, cant be added via the designer because this is a static object.
            statusStrip1.Items.Add(statusLabel);

            tableLayoutPanel1.RowCount = 8;
            tableLayoutPanel1.ColumnCount = 8;
            for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
            {
                for (int j = 0; j < tableLayoutPanel1.ColumnCount; j++)
                {
                    squares[i, j] = new ChessBoardSquare(i, j);
                    tableLayoutPanel1.Controls.Add(squares[i,j],j,i);
                }
            }

            // defining the resource dictionary
            resourceMap = new Dictionary<ChessPiece, Icon>();
            resourceMap.Add(ChessPiece.WhiteRook, WindowsFormsApplication2.Properties.Resources.White_Rook);
            resourceMap.Add(ChessPiece.WhiteHorse, WindowsFormsApplication2.Properties.Resources.White_Horse);
            resourceMap.Add(ChessPiece.WhiteBishop, WindowsFormsApplication2.Properties.Resources.White_Bishop);
            resourceMap.Add(ChessPiece.WhiteKing, WindowsFormsApplication2.Properties.Resources.White_King);
            resourceMap.Add(ChessPiece.WhiteQueen, WindowsFormsApplication2.Properties.Resources.White_Queen);
            resourceMap.Add(ChessPiece.WhitePawn, WindowsFormsApplication2.Properties.Resources.White_Pawn);
            resourceMap.Add(ChessPiece.BlackRook, WindowsFormsApplication2.Properties.Resources.Black_Rook);
            resourceMap.Add(ChessPiece.BlackHorse, WindowsFormsApplication2.Properties.Resources.Black_Horse);
            resourceMap.Add(ChessPiece.BlackBishop, WindowsFormsApplication2.Properties.Resources.Black_Bishop);
            resourceMap.Add(ChessPiece.BlackKing, WindowsFormsApplication2.Properties.Resources.Black_King);
            resourceMap.Add(ChessPiece.BlackQueen, WindowsFormsApplication2.Properties.Resources.Black_Queen);
            resourceMap.Add(ChessPiece.BlackPawn, WindowsFormsApplication2.Properties.Resources.Black_Pawn);

            // setting initial positions for pieces
            squares[0, 0].setPiece(ChessPiece.WhiteRook); 
            squares[0, 1].setPiece(ChessPiece.WhiteHorse); 
            squares[0, 2].setPiece(ChessPiece.WhiteBishop);
            squares[0, 3].setPiece(ChessPiece.WhiteKing);
            squares[0, 4].setPiece(ChessPiece.WhiteQueen);
            squares[0, 5].setPiece(ChessPiece.WhiteBishop);
            squares[0, 6].setPiece(ChessPiece.WhiteHorse);
            squares[0, 7].setPiece(ChessPiece.WhiteRook);

            squares[7, 0].setPiece(ChessPiece.BlackRook);
            squares[7, 1].setPiece(ChessPiece.BlackHorse);
            squares[7, 2].setPiece(ChessPiece.BlackBishop);
            squares[7, 3].setPiece(ChessPiece.BlackKing);
            squares[7, 4].setPiece(ChessPiece.BlackQueen);
            squares[7, 5].setPiece(ChessPiece.BlackBishop);
            squares[7, 6].setPiece(ChessPiece.BlackHorse);
            squares[7, 7].setPiece(ChessPiece.BlackRook);

            for (int i = 0; i < tableLayoutPanel1.ColumnCount; i++)
            {
                    squares[1, i].setPiece(ChessPiece.WhitePawn);
                    squares[6, i].setPiece(ChessPiece.BlackPawn);
            }

            setStatus("White's Move now");
        }
Пример #12
0
 public static void highlightPossiblePositions()
 {
     ChessPiece piece =  squares[currentHighlight.Row, currentHighlight.Col].getPiece();
     switch(piece)
     {
         case ChessPiece.None:
             break;
         case ChessPiece.WhitePawn:
             Position possiblePos;
             if (currentHighlight.Row == 1) // check if pawn is in initial position
             {
                 possiblePos =new Position(currentHighlight.Row + 2, currentHighlight.Col);
                 if (isEmptyPosition(possiblePos) && isEmptyPosition(possiblePos+new Position(-1,0)))
                 {
                     possiblePositions.Add(possiblePos);
                 }
             }
             possiblePos = new Position(currentHighlight.Row + 1, currentHighlight.Col);
             if (isEmptyPosition(possiblePos))
             {
                 possiblePositions.Add(possiblePos);
             }
             // TODO:Add pawn cross capture to possible moves here...
             possiblePos = new Position(currentHighlight.Row + 1, currentHighlight.Col-1);
             if (isValidPosition(possiblePos) && isCapturePosition(possiblePos))
             {
                 possiblePositions.Add(possiblePos);
             }
             possiblePos = new Position(currentHighlight.Row + 1, currentHighlight.Col+1);
             if (isValidPosition(possiblePos) && isCapturePosition(possiblePos))
             {
                 possiblePositions.Add(possiblePos);
             }
             break;
         case ChessPiece.BlackPawn:
             //Position possiblePos;
             if (currentHighlight.Row == 6) // check if pawn is in initial position
             {
                 possiblePos =new Position(currentHighlight.Row - 2, currentHighlight.Col);
                 if (isEmptyPosition(possiblePos) && isEmptyPosition(possiblePos+new Position(1,0))) // path should be clear when pawn is going to move 2 steps
                 {
                     possiblePositions.Add(possiblePos);
                 }
             }
             possiblePos = new Position(currentHighlight.Row - 1, currentHighlight.Col);
             if (isEmptyPosition(possiblePos))
             {
                 possiblePositions.Add(possiblePos);
             }possiblePos = new Position(currentHighlight.Row - 1, currentHighlight.Col-1);
             if (isValidPosition(possiblePos) && isCapturePosition(possiblePos))
             {
                 possiblePositions.Add(possiblePos);
             }
             possiblePos = new Position(currentHighlight.Row - 1, currentHighlight.Col+1);
             if (isValidPosition(possiblePos) && isCapturePosition(possiblePos))
             {
                 possiblePositions.Add(possiblePos);
             }
             break;
         case ChessPiece.WhiteHorse:
         case ChessPiece.BlackHorse:
             possiblePositions.AddRange(horseTransform());
             break;
         case ChessPiece.BlackRook:
         case ChessPiece.WhiteRook:
             possiblePositions.AddRange(rookTransform());
             break;
         case ChessPiece.WhiteBishop:
         case ChessPiece.BlackBishop:
             possiblePositions.AddRange(bishopTransform());
             break;
         case ChessPiece.BlackQueen:
         case ChessPiece.WhiteQueen:
             possiblePositions.AddRange(bishopTransform());
             possiblePositions.AddRange(rookTransform());
             break;
         case ChessPiece.BlackKing:
         case ChessPiece.WhiteKing:
             possiblePositions.AddRange(kingTransform());
             break;
     }
     foreach (Position pos in possiblePositions)
     {
         squares[pos.Row, pos.Col].toggleHighlight();
         if (isCapturePosition(pos))
         {
             squares[pos.Row, pos.Col].setCaptureHighlight();
         }
     }
 }
Пример #13
0
 public static bool isEmptyPosition(Position pos)  // determines whether the Position pos is empty(no pieces) on the board
 {
     return squares[pos.Row, pos.Col].isEmpty();
 }
Пример #14
0
        public static void clearHighlights()
        {
            if(!Object.ReferenceEquals(currentHighlight,null) || currentHighlight != null )
            {
                // removing highlights
                squares[currentHighlight.Row, currentHighlight.Col].toggleHighlight();
                foreach (Position pos in possiblePositions)
                {
                    squares[pos.Row, pos.Col].toggleHighlight();
                }

                // disposing highlight objects
                currentHighlight = null;
                possiblePositions.Clear();
            }
        }
Пример #15
0
 public static bool isCapturePosition(Position target,Position reference) // checks if target position is a capture position relative to provided reference position
 {
     if (squares[target.Row, target.Col].isEmpty())
         return false;
     else
     {
         if (getPlayerSide(squares[reference.Row, reference.Col].getPiece()) == getPlayerSide(squares[target.Row, target.Col].getPiece()))
             return false;
         else
             return true;
     }
 }
Пример #16
0
 public static bool isCapturePosition(Position pos) // determines if the specificied position pos is a capture position relative to currentHighlightPosition
 {
     if (squares[pos.Row,pos.Col].isEmpty())
         return false;
     else
     {
         // checking whether both pieces belong to the same side.....
         if (getPlayerSide(squares[currentHighlight.Row, currentHighlight.Col].getPiece()) == getPlayerSide(squares[pos.Row,pos.Col].getPiece()))
             return false;
         else
             return true;
     }
 }