Ejemplo n.º 1
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();
     }
 }
Ejemplo n.º 2
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");
        }