/// <summary>
        /// Gets the minimax clone.
        /// </summary>
        /// <returns>
        /// A clone to be used in the minimax algoritm
        /// </returns>
        public object GetMinimaxClone()
        {
            CheckersSquareUserControl clone = new CheckersSquareUserControl();

            clone.CheckersPoint = (CheckersPoint)this.CheckersPoint.GetMinimaxClone();

            return(clone);
        }
        /// <summary>
        /// Method runs each time a checker button is clicked. This is for user control
        /// </summary>
        /// <param name="sender">Button sender.</param>
        private void ButtonClickWork(object sender)
        {
            Button button = (Button)sender;
            CheckersSquareUserControl checkerSquareUC = (CheckersSquareUserControl)((Grid)button.Parent).Parent;

            Logger.Info("Row: " + checkerSquareUC.CheckersPoint.Row + " Column: " + checkerSquareUC.CheckersPoint.Column);
            DisableAllButtons();
            if (currentMove == null)
            {
                currentMove = new CheckersMove();
            }

            if (currentMove.SourcePoint == null)
            {
                currentMove.SourcePoint = checkerSquareUC.CheckersPoint;
                SetBackgroundColor(checkerSquareUC, Brushes.Green);

                //starting a move, enable spaces where a valid move is present
                currentAvailableMoves = checkerSquareUC.CheckersPoint.GetPossibleMoves(checkerBoard);

                //Add self move to act as cancel
                currentAvailableMoves.Add(new CheckersMove(checkerSquareUC.CheckersPoint, checkerSquareUC.CheckersPoint));

                ColorBackgroundOfPoints(currentAvailableMoves, Brushes.Aqua);

                EnableButtonsWithPossibleMove(currentAvailableMoves);
            }
            else
            {
                currentMove.DestinationPoint = checkerSquareUC.CheckersPoint;
                SetBackgroundColor(checkerSquareUC, Brushes.Green);

                //get move from the list that has this point as its destination
                MakeMoveReturnModel returnModel = MakeMove(GetMoveFromList(checkerSquareUC.CheckersPoint));
                if (returnModel.WasMoveMade && returnModel.IsTurnOver && Settings.IsAIGame)
                {
                    //Disable buttons so the user cant click anything while the AI is thinking
                    DisableAllButtons();

                    //AI needs to make a move now
                    CheckersMove aiMove = AIController.MinimaxStart(checkerBoard);
                    if (aiMove != null)
                    {
                        while (aiMove != null)
                        {
                            MakeMove(aiMove);
                            aiMove = aiMove.NextMove;
                            Thread.Sleep(Settings.TimeToSleeepBetweenMoves);
                        }
                    }
                    else
                    {
                        //AI could not find a valid move. Is the game over? are we in a dead lock?
                        //Show error to user
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Makes the move on the board.
        /// </summary>
        /// <param name="moveToMake">The move to make.</param>
        /// <param name="swapTurn">if set to <c>true</c> [swap turn].</param>
        /// <returns>true if the current turn was finished</returns>
        public bool MakeMoveOnBoard(CheckersMove moveToMake, bool swapTurn)
        {
            CheckersPoint moveSource      = moveToMake.SourcePoint;
            CheckersPoint moveDestination = moveToMake.DestinationPoint;

            //was this a cancel?
            if (moveSource != moveDestination)
            {
                CheckersPoint realDestination = this.BoardArray[moveDestination.Row][moveDestination.Column].CheckersPoint;
                CheckersPoint realSource      = this.BoardArray[moveSource.Row][moveSource.Column].CheckersPoint;

                realDestination.Checker = (CheckerPiece)realSource.Checker.GetMinimaxClone();
                realSource.Checker      = CheckerPieceFactory.GetCheckerPiece(CheckerPieceType.nullPiece);

                //was this a jump move?
                CheckersPoint jumpedPoint = moveToMake.JumpedPoint;
                if (jumpedPoint != null)
                {
                    //delete the checker piece that was jumped
                    CheckersSquareUserControl jumpedSquareUserControl = this.BoardArray[jumpedPoint.Row][jumpedPoint.Column];
                    jumpedSquareUserControl.CheckersPoint.Checker = CheckerPieceFactory.GetCheckerPiece(CheckerPieceType.nullPiece);
                    jumpedSquareUserControl.UpdateSquare();
                }

                //Is this piece a king now?
                if (!(realDestination.Checker is KingCheckerPiece) &&
                    (realDestination.Row == 7 || realDestination.Row == 0))
                {
                    //Should be a king now
                    if (realDestination.Checker is IRedPiece)
                    {
                        realDestination.Checker = new RedKingCheckerPiece();
                    }
                    else
                    {
                        realDestination.Checker = new BlackKingCheckerPiece();
                    }
                }

                //Is this players turn over?
                if (moveToMake.NextMove == null && swapTurn)
                {
                    //Swap the current players turn
                    SwapTurns();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            return(false);
        }
        /// <summary>
        /// Enables the buttons with a valid move.
        /// </summary>
        private void EnableButtonsWithMove()
        {
            List <CheckersMove> totalPossibleMoves = checkerBoard.GetMovesForPlayer();

            foreach (CheckersMove move in totalPossibleMoves)
            {
                CheckersPoint source = move.SourcePoint;
                int           col    = source.Column;
                int           row    = source.Row;

                CheckersSquareUserControl sourceUserControl = checkerBoard.BoardArray[row][col];

                Application.Current.Dispatcher.BeginInvoke(
                    DispatcherPriority.Background,
                    new Action(() => sourceUserControl.button.IsEnabled = true));
            }
        }
        /// <summary>
        /// Makes a move on the User Interface.
        /// </summary>
        /// <param name="moveToMake">The move to make.</param>
        /// <returns>Make Move Model Return. This model has two boolean properties that represent what happened this move</returns>
        private MakeMoveReturnModel MakeMove(CheckersMove moveToMake)
        {
            bool          moveWasMade = false;
            bool          isTurnOver  = false;
            CheckersPoint source      = moveToMake.SourcePoint;
            CheckersPoint destination = moveToMake.DestinationPoint;

            Logger.Info("Piece1 " + source.Row + ", " + source.Column);
            Logger.Info("Piece2 " + destination.Row + ", " + destination.Column);

            //was this a cancel?
            if (source != destination)
            {
                isTurnOver = checkerBoard.MakeMoveOnBoard(moveToMake);

                CheckersSquareUserControl sourceUC = checkerBoard.BoardArray[source.Row][source.Column];
                CheckersSquareUserControl destUC   = checkerBoard.BoardArray[destination.Row][destination.Column];

                //Use dispatcher to run the Update method on the UI thread
                sourceUC.UpdateSquare();
                destUC.UpdateSquare();

                moveWasMade = true;

                //check for a winner
                object winner = checkerBoard.GetWinner();
                if (winner != null && winner is PlayerColor winnerColor && !Settings.RunningGeneticAlgo)
                {
                    MessageBox.Show("Winner Winner Chicken Dinner: " + winnerColor);
                }
            }

            ColorBackgroundOfPoints(currentAvailableMoves, Brushes.Black);
            SetTitle(string.Format("Checkers! {0}'s turn!", checkerBoard.CurrentPlayerTurn));
            EnableButtonsWithMove();
            currentMove = null;

            return(new MakeMoveReturnModel()
            {
                IsTurnOver = isTurnOver,
                WasMoveMade = moveWasMade
            });
        }
Esempio n. 6
0
        /// <summary>
        /// Makes the board.
        /// </summary>
        /// <param name="routedEventHandler">The routed event handler.</param>
        public void MakeBoard(RoutedEventHandler routedEventHandler)
        {
            int count = 0;

            for (int row = 0; row < 8; row++)
            {
                BoardArray.Add(new List <CheckersSquareUserControl>());
                for (int column = 0; column < 8; column++)
                {
                    CheckersSquareUserControl checkerSquareUC;
                    if (row % 2 == 0)
                    {
                        if (column % 2 == 0)
                        {
                            checkerSquareUC = new CheckersSquareUserControl(
                                Brushes.White,
                                new CheckersPoint(row, column, CheckerPieceType.nullPiece),
                                routedEventHandler);
                        }
                        else
                        {
                            if (row < 3)
                            {
                                checkerSquareUC = new CheckersSquareUserControl(
                                    Brushes.Black,
                                    new CheckersPoint(row, column, CheckerPieceType.BlackPawn),
                                    routedEventHandler);
                            }
                            else if (row > 4)
                            {
                                checkerSquareUC = new CheckersSquareUserControl(
                                    Brushes.Black,
                                    new CheckersPoint(row, column, CheckerPieceType.RedPawn),
                                    routedEventHandler);
                            }
                            else
                            {
                                checkerSquareUC = new CheckersSquareUserControl(
                                    Brushes.Black,
                                    new CheckersPoint(row, column, CheckerPieceType.nullPiece),
                                    routedEventHandler);
                            }
                        }
                    }
                    else
                    {
                        if (column % 2 == 0)
                        {
                            if (row < 3)
                            {
                                checkerSquareUC = new CheckersSquareUserControl(
                                    Brushes.Black,
                                    new CheckersPoint(row, column, CheckerPieceType.BlackPawn),
                                    routedEventHandler);
                            }
                            else if (row > 4)
                            {
                                checkerSquareUC = new CheckersSquareUserControl(
                                    Brushes.Black,
                                    new CheckersPoint(row, column, CheckerPieceType.RedPawn),
                                    routedEventHandler);
                            }
                            else
                            {
                                //empty middle spot
                                checkerSquareUC = new CheckersSquareUserControl(
                                    Brushes.Black,
                                    new CheckersPoint(row, column, CheckerPieceType.nullPiece),
                                    routedEventHandler);
                            }
                        }
                        else
                        {
                            checkerSquareUC = new CheckersSquareUserControl(
                                Brushes.White,
                                new CheckersPoint(row, column, CheckerPieceType.nullPiece),
                                routedEventHandler);
                        }
                    }

                    count++;
                    BoardArray[row].Add(checkerSquareUC);
                }
            }
        }