/* Highlighting of all possible moves
         */
        private void HighlightPossibleTurns(BaseChecker currentSelectedChecker)
        {
            PathPoint turns = currentSelectedChecker.GetPossibleTurns();

            if (!turns.IsDeadEnd())
            {
                foreach (TurnDirection direction in DirectionHelper.GetAllDirections())
                {
                    foreach (PathPoint point in turns[direction])
                    {
                        board[point.Position.X, point.Position.Y].Highlighted = true;
                    }
                }
            }
        }
Пример #2
0
        /* Calculating the computer's move
         */
        public void GetAiTurn(out BaseChecker selectedChecker, out PathPoint toPosition)
        {
            selectedChecker = null;
            toPosition      = null;
            List <PathPoint> possibleTurns = new List <PathPoint>(); // List of possible moves

            // Building a list of possible moves
            foreach (BaseChecker checker in Side == CheckerSide.White ? Game.gameplayController.state.WhiteCheckers :
                     Game.gameplayController.state.BlackCheckers)
            {
                PathPoint turn = checker.GetPossibleTurns();
                if (!turn.IsDeadEnd()) // If not a dead-end point, add a route to the list
                {
                    possibleTurns.Add(turn);
                }
            }

            // If no moves are found - a loss
            if (possibleTurns.Count == 0)
            {
                selectedChecker = null;
                toPosition      = null;
                return;
            }

            // Trying to find aggressive moves
            foreach (PathPoint chain in possibleTurns)
            {
                List <int> eatableDirections = chain.TryGetAggresiveDirections();
                if (eatableDirections.Count > 0)
                {
                    // Aggressive move found
                    toPosition = chain;

                    // We find out the path belonging to the checker and leave the method
                    foreach (BaseChecker checker in Side == CheckerSide.White ? Game.gameplayController.state.WhiteCheckers :
                             Game.gameplayController.state.BlackCheckers)
                    {
                        PathPoint turn = checker.GetPossibleTurns();
                        if (turn.Position == chain.Position)
                        {
                            selectedChecker = checker;
                            return;
                        }
                    }
                }
            }
            // There are no "edible" moves, we are looking for peaceful ones in a random way
            Random rnd = new Random();

            toPosition = possibleTurns[rnd.Next(possibleTurns.Count - 1)];
            // Matching the move with the checker
            foreach (BaseChecker checker in Side == CheckerSide.White ? Game.gameplayController.state.WhiteCheckers :
                     Game.gameplayController.state.BlackCheckers)
            {
                PathPoint turn = checker.GetPossibleTurns();
                if (turn.Position == toPosition.Position)
                {
                    selectedChecker = checker;
                }
            }

            return;
        }
        /* When you click on the game board, this method is called
         */
        public void OnClick(MouseEventArgs mouseArgs)
        {
            bool        anyoneSelected            = false;
            BaseChecker previouslySelectedChecker = null;
            BaseChecker currentSelectedChecker    = null;

            // Trying to find the selected checker
            anyoneSelected = TryFindSelectedCheckers(mouseArgs, ref previouslySelectedChecker, ref currentSelectedChecker);

            // If the checker was not selected
            if (!anyoneSelected)
            {
                // If the checker was selected at the last step
                if (previouslySelectedChecker != null)
                {
                    // Attempt to move to a given position
                    Point toBoardPosition = new Point()
                    {
                        X = mouseArgs.X / cellSize.Width,
                        Y = mouseArgs.Y / cellSize.Height
                    };
                    DoMovement(previouslySelectedChecker, toBoardPosition);

                    // Checking the conditions of victory and defeat
                    CheckVictoryConditions();
                }
                else
                {
                    // Clicked on an empty field
                    DeselectAllCheckers();
                    state.ActiveChecker          = null;
                    state.ActiveCheckerPathPoint = null;
                }
            }
            else
            {
                // The checker is selected
                // Prevent the move by another checker (other than the selected one)
                if (state.ActiveChecker != null)
                {
                    if (state.ActiveChecker != currentSelectedChecker)
                    {
                        currentSelectedChecker = null;
                        return;
                    }
                }

                List <BaseChecker> turnList = new List <BaseChecker>();
                bool aggressive             = false;
                bool turnFound = false;
                foreach (BaseChecker checker in currentSelectedChecker.Side == CheckerSide.White ? state.WhiteCheckers : state.BlackCheckers)
                {
                    PathPoint turns = checker.GetPossibleTurns();
                    if (turns.IsDeadEnd())
                    {
                        continue;
                    }
                    if (turns.TryGetAggresiveDirections().Count > 0)
                    {
                        turnList.Add(checker);
                        aggressive = true;
                    }
                }

                // Filtering moves (if you can eat something, block "inedible" moves)
                if (!aggressive)
                {
                    ClearHighlighting();
                    HighlightPossibleTurns(currentSelectedChecker); // Highlight it
                }
                else
                {
                    foreach (BaseChecker checker in turnList)
                    {
                        if (checker == currentSelectedChecker)
                        {
                            turnFound = true;
                        }
                    }

                    ClearHighlighting();
                    if (turnFound)
                    {
                        HighlightPossibleTurns(currentSelectedChecker);
                    }
                    else
                    {
                        DeselectAllCheckers();
                    }
                }
            }

            // Redraw the scene
            Game.drawingController.PrioritizedDraw();

            // Clearing memory from objects generated by active computation of edible options
            GC.Collect();
        }