Ejemplo n.º 1
0
        private bool IsValidMove(pieceName sourcePieceType, int destinationY, int destinationX) // determine if the piece is able to move to its target square
        {
            int sourceY = PieceDetails.FindCoordinate(selectedpiece.Location.Y);
            int sourceX = PieceDetails.FindCoordinate(selectedpiece.Location.X);

            switch (sourcePieceType)
            {
            case pieceName.Pawn:
                Pawn pawn = new Pawn(board, sourceY, sourceX, destinationY, destinationX, History, turn);
                return(pawn.Move(board));

            case pieceName.Rook:
                Rook rook = new Rook(board, sourceY, sourceX, destinationY, destinationX, History, turn);
                return(rook.Move(board));

            case pieceName.Knight:
                Knight knight = new Knight(board, sourceY, sourceX, destinationY, destinationX, History, turn);
                return(knight.Move(board));

            case pieceName.Bishop:
                Bishop bishop = new Bishop(board, sourceY, sourceX, destinationY, destinationX, History, turn);
                return(bishop.Move(board));

            case pieceName.Queen:
                Queen queen = new Queen(board, sourceY, sourceX, destinationY, destinationX, History, turn);
                return(queen.Move(board));

            default:
                King king = new King(board, sourceY, sourceX, destinationY, destinationX, History, turn);
                return(king.Move(board));
            }
        }
Ejemplo n.º 2
0
        private bool MovePiece(PictureBox destination) // moving a piece to eat another piece
        {
            pieceName sourcePieceType = turn ? PieceDetails.selectedWhitePiece(selectedpiece.Name) : PieceDetails.selectedBlackPiece(selectedpiece.Name);
            int       destinationY    = PieceDetails.FindCoordinate(destination.Location.Y);
            int       destinationX    = PieceDetails.FindCoordinate(destination.Location.X);

            return(IsValidMove(sourcePieceType, destinationY, destinationX));
        }
Ejemplo n.º 3
0
 private void board_Click(object sender, EventArgs e) // when selected piece move to empty square
 {
     if (currentState != gameState.Checkmate && currentState != gameState.Stalemate && (PvP || PvE))
     {
         if (alreadySelected)
         {
             Point position = Cursor.Position;
             position = panel.PointToClient(position);
             int destinationY = PieceDetails.FindCoordinate(position.Y);
             int destinationX = PieceDetails.FindCoordinate(position.X);
             if (MovePiece(destinationY, destinationX))
             {
                 resetHighlightedPieces();
                 completeTurn();
                 AIMove();
             }
             else
             {
                 MessageBox.Show("Invalid Move, please try again.");
             }
         }
     }
 }
Ejemplo n.º 4
0
 private void clickWhite(object sender, EventArgs e)
 {
     if (currentState != gameState.Checkmate && currentState != gameState.Stalemate && (PvP || PvE))
     {
         if (alreadySelected && !turn) // when black piece eats white black piece
         {
             if (MovePiece((PictureBox)sender))
             {
                 panel.Refresh();
                 selectedpiece.BackColor = Color.Transparent;
                 foreach (PictureBox p in PossiblePieceToTake)
                 {
                     p.BackColor = Color.Transparent;
                 }
                 PossiblePieceToTake = new List <PictureBox>();
                 completeTurn();
                 AIMove();
             }
             else
             {
                 MessageBox.Show("Invalid Move, please try again.");
             }
         }
         else if (turn && selectedpiece != (PictureBox)sender) // selecting white piece
         {
             resetHighlightedPieces();
             selectedpiece           = (PictureBox)sender;
             selectedpiece.BackColor = Color.FromArgb(220, 13, 86, 212);
             alreadySelected         = true;
             pieceName            sourcePieceType = PieceDetails.selectedWhitePiece(selectedpiece.Name);
             int                  Y   = PieceDetails.FindCoordinate(selectedpiece.Location.Y);
             int                  X   = PieceDetails.FindCoordinate(selectedpiece.Location.X);
             DisplayPossibleMoves dpm = new DisplayPossibleMoves(panel.CreateGraphics(), Color.FromArgb(125, 48, 118, 240));
             dpm.DisplayMoves(Y, X, board, PossiblePieceToTake, turn, sourcePieceType);
         }
     }
 }