private void CalculateBestRoute(ITrack track, ICar car, IList <Point> routeGridPoints, Point middleGridPoint)
        {
            IList <Point> candidateGridPoints         = GetCandidatePoints(track, car, routeGridPoints);
            IList <Point> candidateGridPointsFiltered = ComputerDriverHelper.FilterCandidatePoints_RemoveInvalids
                                                            (track, candidateGridPoints, routeGridPoints);

            if (candidateGridPointsFiltered.Count == 0)
            {
                // backtrack, because we are at a dead end
                routeGridPoints.RemoveAt(routeGridPoints.Count - 1);
                return;
            }

            // we only take candidate grid points with a quadrant valid direction
            IList <Point> candidateGridPointsFilteredQuadrantDir = ComputerDriverHelper.FilterCandidatePoints_RemoveUnfavorableDir
                                                                       (candidateGridPointsFiltered, routeGridPoints, middleGridPoint);

            for (int index = 0; index < candidateGridPointsFilteredQuadrantDir.Count; index++)
            {
                Point newPoint = candidateGridPointsFilteredQuadrantDir[index];

                if (track.CheckIfRaceIsFinished(routeGridPoints, newPoint))
                {
                    // we have found one complete route that finishes
                    routeGridPoints.Add(newPoint);
                    // now compare if this is better than the previous one
                    if (IsThisBetterThanCurrentBestRoute(routeGridPoints))
                    {
                        // yes it is better -> overwrite
                        mCurrentBestRoute = routeGridPoints.ToList();
                    }
                    else
                    {
                        // ignore it
                        routeGridPoints.RemoveAt(routeGridPoints.Count - 1);
                    }
                }
                else
                {
                    // continue route to finish the race
                    routeGridPoints.Add(newPoint);
                    CalculateBestRoute(track, car, routeGridPoints, middleGridPoint);
                }
            }
        }
Example #2
0
        /// <summary>
        /// The movement vector is valid, now deal with it and
        /// - paint the movement vector
        /// - increase counter
        /// - detect if race is finished
        /// </summary>
        /// <param name="gridPointClicked"></param>
        private void HandleValidMovementVector(Point gridPointClicked)
        {
            // paint the movement vector
            IList <UIElement> uiElemsMovVector = MovementVectorPainter.PaintMovementVector
                                                     (xMyCanvas.Children, mRouteGridPoints.Last(), gridPointClicked);

            for (int index = 0; index < uiElemsMovVector.Count; index++)
            {
                mRouteElements.Add(uiElemsMovVector[index]);
            }

            // verify if the race is finished, it is simpler to do this before adding the grid point
            bool raceFinished = mActiveTrack.CheckIfRaceIsFinished(mRouteGridPoints, gridPointClicked);

            mRouteGridPoints.Add(gridPointClicked);
            xNrVectors.Text = GetNrOfVectors().ToString();

            if (raceFinished)
            {
                FinishRace();
            }
        }