Esempio n. 1
0
        void SelectUnselectPiece(PictureBox oBox)
        {
            //if figure is picked conteniu.
            if (!pickedFlag)
            {
                //if player is white conteniu
                if (EnvironmentVariables.Player == DataStructure.Color.white)
                {
                    //save coordination data to variable and check what piece was picked up.
                    pickedPiece.i = myCollection[oBox.Name].i;
                    pickedPiece.j = myCollection[oBox.Name].j;
                    if (Validation.FigureColor(pickedPiece, boardMatrix) == DataStructure.Color.white)
                    {
                        pickedFlag = true;

                        switch (boardMatrix[myCollection[oBox.Name].i, myCollection[oBox.Name].j])
                        {
                        case (int)DataStructure.Figures.white_pawn:
                            possiblePossitions = pawn.FindPossibleMoves(pickedPiece, boardMatrix);
                            break;

                        case (int)DataStructure.Figures.white_rock:
                            possiblePossitions = rock.FindPossibleMoves(pickedPiece, boardMatrix);
                            break;

                        case (int)DataStructure.Figures.white_knight:
                            possiblePossitions = knight.FindPossibleMoves(pickedPiece, boardMatrix);
                            break;

                        case (int)DataStructure.Figures.white_bishop:
                            possiblePossitions = bishopcs.FindPossibleMoves(pickedPiece, boardMatrix);
                            break;

                        case (int)DataStructure.Figures.white_queen:
                            possiblePossitions = queen.FindPossibleMoves(pickedPiece, boardMatrix);
                            break;

                        case (int)DataStructure.Figures.white_king:
                            possiblePossitions = king.FindPossibleMoves(pickedPiece, boardMatrix);
                            break;

                        default:
                            break;
                        }
                        DrawBackground();
                        HighligtSelectedPiece(oBox);
                    }
                }
                else
                {
                    pickedPiece.i = myCollection[oBox.Name].i;
                    pickedPiece.j = myCollection[oBox.Name].j;
                    if (Validation.FigureColor(pickedPiece, boardMatrix) == DataStructure.Color.black)
                    {
                        pickedFlag = true;
                        switch (boardMatrix[myCollection[oBox.Name].i, myCollection[oBox.Name].j])
                        {
                        case (int)DataStructure.Figures.black_pawn:
                            possiblePossitions = pawn.FindPossibleMoves(pickedPiece, boardMatrix);
                            break;

                        case (int)DataStructure.Figures.black_rock:
                            possiblePossitions = rock.FindPossibleMoves(pickedPiece, boardMatrix);
                            break;

                        case (int)DataStructure.Figures.black_knight:
                            possiblePossitions = knight.FindPossibleMoves(pickedPiece, boardMatrix);
                            break;

                        case (int)DataStructure.Figures.black_bishop:
                            possiblePossitions = bishopcs.FindPossibleMoves(pickedPiece, boardMatrix);
                            break;

                        case (int)DataStructure.Figures.black_queen:
                            possiblePossitions = queen.FindPossibleMoves(pickedPiece, boardMatrix);
                            break;

                        case (int)DataStructure.Figures.black_king:
                            possiblePossitions = king.FindPossibleMoves(pickedPiece, boardMatrix);
                            break;

                        default:
                            break;
                        }
                        DrawBackground();
                        HighligtSelectedPiece(oBox);
                    }
                }
            }
            else
            {
                //if clicked at the same square as before, "drop figure" and clear highlights
                if (pickedPiece.i == myCollection[oBox.Name].i && pickedPiece.j == myCollection[oBox.Name].j)
                {
                    pickedPiece.i = -1;
                    pickedPiece.j = -1;

                    pickedFlag = false;
                    DrawBackground();
                }
                else
                {
                    DataStructure.Point temp = new DataStructure.Point(myCollection[oBox.Name].i, myCollection[oBox.Name].j);
                    //if it is one of highlighted fields go on. if not than
                    //if it is a empty field or not highlighted enemy piece do nothing if it is a friendly piece select it
                    if (Validation.IsItHighlightedField(possiblePossitions, temp))
                    {
                        MoveFigure(pickedPiece, temp);
                        if (boardMatrix[temp.i, temp.j] == (int)DataStructure.Figures.white_pawn ||
                            boardMatrix[temp.i, temp.j] == (int)DataStructure.Figures.black_pawn)
                        {
                            Pawn.KickingOutEnPassant(pickedPiece, temp, boardMatrix);
                            Pawn.EndofTheColumn(temp, boardMatrix);
                            Pawn.WasDoubleJump(pickedPiece, temp);
                        }
                        if (EnvironmentVariables.Player == DataStructure.Color.white)
                        {
                        }
                        pickedFlag = false;
                        DrawBackground();
                        ChangePlayer();
                    }
                    else if (Validation.IsEmpty(temp, boardMatrix))
                    {
                        //do nothing
                    }
                    else
                    {
                        if (boardMatrix[pickedPiece.i, pickedPiece.j] % 2 == (int)DataStructure.Color.white)
                        {
                            if (!Validation.IsOpponent(temp, boardMatrix))
                            {
                                pickedPiece.i = -1;
                                pickedPiece.j = -1;

                                pickedFlag = false;
                                SelectUnselectPiece(oBox);
                            }
                            //else do nothing
                        }
                        else if (boardMatrix[pickedPiece.i, pickedPiece.j] % 2 == (int)DataStructure.Color.black)
                        {
                            if (!Validation.IsOpponent(temp, boardMatrix))
                            {
                                pickedPiece.i = -1;
                                pickedPiece.j = -1;

                                pickedFlag = false;
                                SelectUnselectPiece(oBox);
                            }
                            //else do nothing
                        }
                    }
                }
            }
        }