Example #1
0
        private void Uc_ChessPiece_Click(object sender, EventArgs e)
        {
            // if we are in capturing Mode then exit
            if (Uc_ChessBoard._isCapturingMode)
            {
                return;
            }

            //Get the present Uc_ChessBoard
            Uc_ChessBoard uc_chessboard = (Uc_ChessBoard)this.Parent;

            //If this cell is clicked and highlighted => will be eated by uc_chessboard.PositionChoosen cell
            if (uc_chessboard.PositionChoosen.X != 0 && uc_chessboard.PositionChoosen.Y != 0)
            {
                //if choose this position again -> remove choose
                if (uc_chessboard.PositionChoosen.X == this.Position.X && uc_chessboard.PositionChoosen.Y == this.Position.Y)
                {
                    uc_chessboard.UnHighLightMoves();
                    uc_chessboard.HighLightWhoCheckAndKingChecked();
                    uc_chessboard.PositionChoosen = new Point(0, 0);
                    return;
                }

                foreach (ChessMove p in uc_chessboard.arrCanMove)
                {
                    if (this._position.X == p.MoveTo.X && this._position.Y == p.MoveTo.Y)
                    {
                        uc_chessboard.DoMove(p);
                        uc_chessboard.PositionChoosen = new Point(0, 0); //this cell was eated so there's no Position Choosen

                        //Computer's move
                        if (uc_chessboard.GameMode == eGameMode.VsComputer)
                        {
                            uc_chessboard.IsThinking = true;
                            if (uc_chessboard.GameStatus == eGameStatus.Playing)
                            {
                                uc_chessboard.Computer_Move();
                            }
                        }
                        return;
                    }
                }

                //If this Point wasn't eated => reset PositionChoosen and Unhighlight
                uc_chessboard.UnHighLightMoves();
                uc_chessboard.PositionChoosen = new Point(0, 0);

                //Make this Point -> Position Choosen
                uc_chessboard.arrCanMove.Clear();

                //if this point = position choosen -> return

                if ((uc_chessboard.OwnSide == this._side || uc_chessboard.GameMode == eGameMode.VsHuman) &&
                    uc_chessboard.GameStatus == eGameStatus.Playing)
                {
                    if ((uc_chessboard.WhoTurn == eChessSide.White && this._side == eChessSide.White) ||
                        (uc_chessboard.WhoTurn == eChessSide.Black && this._side == eChessSide.Black))
                    {
                        uc_chessboard.arrCanMove = Chess_Engine.FindAllPosibleMoves(uc_chessboard.arrState, this._position, uc_chessboard.OwnSide);

                        uc_chessboard.PositionChoosen = new Point(_position.X, _position.Y);
                        uc_chessboard.UnHighLightWhoCheckAndKingChecked();
                        uc_chessboard.UnHighLightLastMove();
                        uc_chessboard.HighLightAllPosibleMoves();
                    }
                }
            }
            else
            {
                //If this Point isn't highlighted

                //Make this Point -> Position Choosen
                uc_chessboard.arrCanMove.Clear();

                //if this point = position choosen -> return

                if ((uc_chessboard.OwnSide == this._side || uc_chessboard.GameMode == eGameMode.VsHuman) &&
                    uc_chessboard.GameStatus == eGameStatus.Playing)
                {
                    if ((uc_chessboard.WhoTurn == eChessSide.White && this._side == eChessSide.White) ||
                        (uc_chessboard.WhoTurn == eChessSide.Black && this._side == eChessSide.Black))
                    {
                        uc_chessboard.arrCanMove = Chess_Engine.FindAllPosibleMoves(uc_chessboard.arrState, this._position, uc_chessboard.OwnSide);

                        uc_chessboard.PositionChoosen = new Point(_position.X, _position.Y);
                        uc_chessboard.UnHighLightWhoCheckAndKingChecked();
                        uc_chessboard.UnHighLightLastMove();
                        uc_chessboard.HighLightAllPosibleMoves();
                    }
                }
            }
        }