public void StartGame(Difficulty difficulty)
        {
            PlayerSettings playerA = new PlayerSettings(false, AlgorithmType.AlphaBeta, 2, false, false, EvaluationType.BetterOne);
            PlayerSettings playerB = null;
            this.difficulty = difficulty;
            switch (difficulty)
            {
                case Difficulty.Easy:
                    playerB = new PlayerSettings(true, AlgorithmType.Random, 1, false, false, EvaluationType.BetterOne);
                    break;

                case Difficulty.Medium:
                    playerB = new PlayerSettings(true, AlgorithmType.AlphaBeta, 1, true, true, EvaluationType.BetterOne);
                    break;

                case Difficulty.Hard:
                    playerB = new PlayerSettings(true, AlgorithmType.AlphaBeta, 2, true, true, EvaluationType.BetterOne);
                    break;
            }

            this.BoardElements = new List<BoardElement>();
            uiConnector.StartGame(playerB, playerA);
            leftMouseButtonPressed = false;
            selectedElementFrom = null;
            selectedElementTo = null;
            SetupBoard();
        }
        public override void Update(GameTime gameTime)
        {
            if (Mouse.GetState().RightButton == ButtonState.Pressed)
                rightMouseButtonPressed = true;
            if (Mouse.GetState().RightButton == ButtonState.Released && rightMouseButtonPressed)
            {
                rightMouseButtonPressed = false;
                //if from and to selected, then do undoMove, else do undo select
                if (selectedElementFrom != null && selectedElementTo != null)
                {
                    undoMove = true;
                }
                else
                {
                    selectedElementFrom = null;
                }
            }
            if (isBusy)
                return;
            //uiConnector.GetBoard().CalculateMovableTiles();
            isSelected = false;
            if (isGameEnded)
            {
                TimeSpan timeSpanCurrent = new TimeSpan(0, 0, 0, gameTime.ElapsedGameTime.Seconds);
                timeSpan.Subtract(timeSpanCurrent);
            }

            if (Mouse.GetState().LeftButton == ButtonState.Pressed)
                leftMouseButtonPressed = true;

            if (leftMouseButtonPressed && Mouse.GetState().LeftButton == ButtonState.Released)
            {
                isSelected = true;
                leftMouseButtonPressed = false;
            }

            Microsoft.Xna.Framework.Point pMouse = new Microsoft.Xna.Framework.Point(Mouse.GetState().X, Mouse.GetState().Y);

            Vector3 nearUnproject = game.ScreenManager.GraphicsDevice.Viewport.Unproject(new Vector3(pMouse.X, pMouse.Y, 0), game.Projection, game.View, Matrix.Identity);
            Vector3 farUnproject = game.ScreenManager.GraphicsDevice.Viewport.Unproject(new Vector3(pMouse.X, pMouse.Y, 1), game.Projection, game.View, Matrix.Identity);

            Vector3 direction = Vector3.Subtract(farUnproject, nearUnproject);

            direction.Normalize();

            Ray ray = new Ray(nearUnproject, direction);

            BoardElement selected = null;
            float? smallestIntersect = float.MaxValue;

            List<BoardElement> intersectList = BoardElements.ToList();
            if (PossiblePlaces != null)
                foreach (BoardElement b in PossiblePlaces)
                    intersectList.Add(b);

            foreach (BoardElement boardElement in intersectList)
            {
                float? intersect = boardElement.Intersect(ray);
                if (intersect != null)
                {
                    if (smallestIntersect > intersect)
                    {
                        if (uiConnector.CurrentPlayerNumPieces() < 6 && boardElement is Tile && selectedElementFrom == null)
                        {
                        }
                        else if (boardElement.DefaultColor != Color.Red.ToVector3())
                        {
                            selected = boardElement;
                            smallestIntersect = intersect;
                        }
                    }
                }

                if (selectedElementFrom != boardElement && boardElement != selectedElementTo)
                {
                    boardElement.IsSelected = false;
                    boardElement.IsMouseOver = false;
                }
            }

            if (selected != null)
            {
                if (isSelected)
                {
                    selected.IsSelected = true;
                    if (selectedElementFrom == null)
                        selectedElementFrom = selected;
                    else if (selectedElementFrom == selected)
                        selectedElementFrom = null;
                    else
                        selectedElementTo = selected;

                    if (FromTileSelected)
                    {

                        if (uiConnector.IsMovable((int)selectedElementFrom.BoardX, (int)selectedElementFrom.BoardY))
                        {
                            GenerateTargetTiles();
                        }
                        else if (selectedElementFrom is Piece)
                        {

                        }
                        else
                        {
                            //wrongMoveSound.PlaySound();
                        }
                    }
                    else
                    {
                        GenerateTargetTiles(false);
                    }
                }
                else
                {
                    selected.IsMouseOver = true;
                }
            }

            if (selectedElementFrom != null && selectedElementTo != null)
            {
                doMoveThread = new Thread(new ThreadStart(this.DoMove));
                doMoveThread.Start();
            }

            UpdateMinMax();

            base.Update(gameTime);
        }
        public void DoMove()
        {
            isBusy = true;
            if (selectedElementFrom != null)
            {
                if (selectedElementFrom is Tile)
                {
                    foreach (BoardElement element in BoardElements)
                    {
                        if (element is Piece && element.BoardX == selectedElementFrom.BoardX && element.BoardY == selectedElementFrom.BoardY)
                            selectedElementFrom = element;
                    }
                }
                Tile fromTile = null;
                if (selectedElementFrom is Piece && selectedElementTo is Tile)
                {
                    selectedElementTo.IsSelected = true;
                    foreach (BoardElement element in BoardElements)
                    {
                        if (element is Tile && element.BoardX == selectedElementFrom.BoardX && element.BoardY == selectedElementFrom.BoardY)
                        {
                            fromTile = (Tile)element;
                            element.IsSelected = true;
                        }
                    }
                }

                int numItems = uiConnector.CurrentPlayerNumPieces();
                if (numItems < 6 && selectedElementFrom.BoardY == 12.5f)
                {
                    FormPoint placePoint = new FormPoint((int)selectedElementTo.BoardX,
                                                                               (int)selectedElementTo.BoardY);
                    if (uiConnector.ValidatePlacePiece(placePoint))
                    {
                        Piece piece = (Piece)selectedElementFrom;
                        float oldBoardX = piece.BoardX;
                        float oldBoardY = piece.BoardY;

                        piece.Move(selectedElementTo.BoardX, selectedElementTo.BoardY);
                        while (piece.AnimatedStarted) ;
                        selectedElementFrom.IsSelected = false;
                        selectedElementFrom.IsMouseOver = false;
                        threadWaiting = true;
                        Thread.Sleep(1000);
                        if (undoMove)
                        {
                            piece.Move(oldBoardX, oldBoardY);
                            while (piece.AnimatedStarted) ;
                            selectedElementFrom = null;
                            selectedElementTo = null;
                            isBusy = false;
                            undoMove = false;
                            return;
                        }
                        //set to false, so no yellow will apear after 1 sec.

                        selectedElementTo.IsSelected = false;
                        selectedElementTo.IsMouseOver = false;

                        AICalculates = true;
                        threadWaiting = false;
                        currentBoard = (BoardPosition[,])uiConnector.GetBoard().BoardSituation.Clone();
                        currentBoard[(int)selectedElementTo.BoardX, (int)selectedElementTo.BoardY] = BoardPosition.WhiteTail;
                        uiConnector.PlacePiece(placePoint);
                        BoardPosition[,] newBoardSituation = (BoardPosition[,])uiConnector.GetBoard().BoardSituation.Clone();

                        AICalculates = false;
                        DoAIMove(currentBoard, newBoardSituation);
                    }
                    else
                        wrongMoveSound.PlaySound();
                }
                else
                {
                    FormPoint fromPoint = new FormPoint((int)selectedElementFrom.BoardX,
                                                                               (int)selectedElementFrom.BoardY);
                    FormPoint toPoint = new FormPoint((int)selectedElementTo.BoardX,
                                                                               (int)selectedElementTo.BoardY);

                    if (uiConnector.ValidateMovePiece(fromPoint, toPoint) && selectedElementFrom is Piece)
                    {
                        Piece piece = (Piece)selectedElementFrom;
                        int moveDistance = 0;
                        currentBoard = (BoardPosition[,])uiConnector.GetBoard().BoardSituation.Clone();
                        BoardPosition changeTo = BoardPosition.Empty;
                        if (Math.Abs(fromPoint.X - toPoint.X) == 1 || Math.Abs(fromPoint.Y - toPoint.Y) == 1)
                            moveDistance = 1;
                        if (Math.Abs(fromPoint.X - toPoint.X) == 2 || Math.Abs(fromPoint.Y - toPoint.Y) == 2)
                            moveDistance = 2;

                        if (moveDistance == 1)
                            changeTo = currentBoard[fromPoint.X, fromPoint.Y];
                        else
                        {
                            piece.HeadUp = false;
                            changeTo = BoardPosition.WhiteTail;
                            if (currentBoard[fromPoint.X, fromPoint.Y] == BoardPosition.WhiteTail)
                            {
                                changeTo = BoardPosition.WhiteHead;
                                piece.HeadUp = true;
                            }
                        }
                        currentBoard[fromPoint.X, fromPoint.Y] = BoardPosition.Tile;
                        currentBoard[toPoint.X, toPoint.Y] = changeTo;

                        float oldBoardX = selectedElementFrom.BoardX;
                        float oldBoardY = selectedElementFrom.BoardY;

                        selectedElementFrom.Move(selectedElementTo.BoardX, selectedElementTo.BoardY);
                        while (selectedElementFrom.AnimatedStarted) ;
                        selectedElementFrom.IsSelected = false;
                        selectedElementFrom.IsMouseOver = false;

                        threadWaiting = true;
                        Thread.Sleep(1000);
                        if (undoMove)
                        {
                            if (moveDistance == 2)
                                piece.HeadUp = !piece.HeadUp;
                            selectedElementFrom.Move(oldBoardX, oldBoardY);
                            while (selectedElementFrom.AnimatedStarted) ;
                            selectedElementFrom = null;
                            selectedElementTo = null;
                            isBusy = false;
                            undoMove = false;
                            return;
                        }

                        selectedElementTo.IsSelected = false;
                        selectedElementTo.IsMouseOver = false;
                        fromTile.IsSelected = false;
                        fromTile.IsMouseOver = false;

                        threadWaiting = false;
                        AICalculates = true;
                        uiConnector.MovePiece(fromPoint, toPoint);

                        BoardPosition[,] newBoardSituation = (BoardPosition[,])uiConnector.GetBoard().BoardSituation.Clone();
                        if (uiConnector.IsWon())
                        {
                            timeSpan = new TimeSpan(0,0,0,0,10000);
                            isGameEnded = true;
                            AICalculates = false;
                            DoAIMove(currentBoard, newBoardSituation);
                            PlayWinningAnimation(uiConnector.GetCurrentPlayer());
                            selectedElementFrom = null;
                            selectedElementTo = null;
                            return;
                        }

                        AICalculates = false;
                        DoAIMove(currentBoard, newBoardSituation);

                    }
                    else if (uiConnector.ValidateMoveTile(fromPoint, toPoint))
                    {
                        currentBoard = (BoardPosition[,])uiConnector.GetBoard().BoardSituation.Clone();
                        currentBoard[fromPoint.X, fromPoint.Y] = BoardPosition.Empty;
                        currentBoard[toPoint.X, toPoint.Y] = BoardPosition.Tile;

                        Tile tile = (Tile)selectedElementFrom;
                        float oldBoardX = tile.BoardX;
                        float oldBoardY = tile.BoardY;

                        tile.Move(toPoint.X, toPoint.Y);
                        while (tile.AnimatedStarted) ;
                        threadWaiting = true;
                        Thread.Sleep(1000);
                        if (undoMove)
                        {
                            tile.Move(oldBoardX, oldBoardY);
                            while (tile.AnimatedStarted) ;
                            selectedElementFrom = null;
                            selectedElementTo = null;
                            isBusy = false;
                            undoMove = false;
                            return;
                        }
                        threadWaiting = false;

                        uiConnector.MoveTile(fromPoint, toPoint);
                        BoardPosition[,] newBoardSituation = (BoardPosition[,])uiConnector.GetBoard().BoardSituation.Clone();
                    }
                    else
                        wrongMoveSound.PlaySound();
                }
            }
            selectedElementFrom = null;
            selectedElementTo = null;
            this.UpdateMinMax();
            isBusy = false;
        }