public void OnePossiblePathWithTwoDirectionChanges_FindAllPathsWithMaxDirectionChangesSetToOne_ReturnsAllInactivePaths()
        {
            // Arrange
            var startCoordsBlue = CoordsFactory.GetCoords(0, 3);
            var endCoordsBlue   = CoordsFactory.GetCoords(3, 3);

            // "BOOB"
            // " RR "
            // " GG "
            // "    "
            var colourPairBlue = new ColourPair(startCoordsBlue, endCoordsBlue, DotColours.Blue);
            var grid           = new Grid(4,
                                          colourPairBlue,
                                          new ColourPair(CoordsFactory.GetCoords(1, 3), CoordsFactory.GetCoords(2, 3), DotColours.Orange),
                                          new ColourPair(CoordsFactory.GetCoords(1, 2), CoordsFactory.GetCoords(2, 2), DotColours.Red),
                                          new ColourPair(CoordsFactory.GetCoords(1, 1), CoordsFactory.GetCoords(2, 1), DotColours.Green));

            // Act
            var pathFinder       = new PathFinder(CancellationToken.None);
            var initialPathsBlue = PathFinder.InitialPaths(colourPairBlue);
            var paths            = pathFinder.FindAllPaths(grid, endCoordsBlue, initialPathsBlue, 1);

            // Assert
            Assert.That(paths, Has.All.Matches <Path>(p => !p.IsActive));
        }
        public void DrawPath(ColourPair colourPair, Model.Path path)
        {
            var aw = ActualWidth;
            var ah = ActualHeight;
            var sw = (aw - GridLineThickness) / GridSize;
            var sh = (ah - GridLineThickness) / GridSize;

            var pathColour = colourPair.DotColour.ToWpfColour();

            var points = new List <Point>();

            // ReSharper disable LoopCanBeConvertedToQuery
            foreach (var coords in path.CoordsList)
            {
                var x = GridLineHalfThickness + (coords.X * sw) + (sw / 2);
                var y = GridLineHalfThickness + ((GridSize - coords.Y - 1) * sh) + (sh / 2);
                points.Add(new Point(x, y));
            }
            // ReSharper restore LoopCanBeConvertedToQuery

            var polyLineSegment = new PolyLineSegment(points, true);
            var pathFigure      = new PathFigure {
                StartPoint = points.First()
            };

            pathFigure.Segments.Add(polyLineSegment);
            var pathGeometry = new PathGeometry();

            pathGeometry.Figures.Add(pathFigure);
            var polyLinePath = new Path
            {
                Tag              = TagType.Path,
                Stroke           = new SolidColorBrush(pathColour),
                StrokeThickness  = sw / 3,
                StrokeEndLineCap = PenLineCap.Round,
                StrokeLineJoin   = PenLineJoin.Round,
                Data             = pathGeometry
            };

            BoardCanvas.Children.Add(polyLinePath);

            // ReSharper disable LoopCanBeConvertedToQuery
            foreach (var coords in path.CoordsList)
            {
                var cellRect           = new Rect(coords.X * sw + GridLineHalfThickness, (GridSize - coords.Y - 1) * sh + GridLineHalfThickness, sw, sh);
                var highlightRectangle = new Rectangle
                {
                    Tag    = TagType.HighlightRectangle,
                    Width  = cellRect.Width,
                    Height = cellRect.Height,
                    Fill   = new SolidColorBrush(Color.FromArgb(0x80, pathColour.R, pathColour.G, pathColour.B))
                };
                Canvas.SetLeft(highlightRectangle, cellRect.Left);
                Canvas.SetTop(highlightRectangle, cellRect.Top);
                BoardCanvas.Children.Add(highlightRectangle);
            }
            // ReSharper restore LoopCanBeConvertedToQuery
        }
        public void Grid4X4WithFourColourPairs()
        {
            // Arrange
            var startCoordsBlue = CoordsFactory.GetCoords(0, 3);
            var endCoordsBlue   = CoordsFactory.GetCoords(3, 3);

            // "BOOB"
            // " RR "
            // " GG "
            // "    "
            var colourPairBlue = new ColourPair(startCoordsBlue, endCoordsBlue, DotColours.Blue);
            var grid           = new Grid(4,
                                          colourPairBlue,
                                          new ColourPair(CoordsFactory.GetCoords(1, 3), CoordsFactory.GetCoords(2, 3), DotColours.Orange),
                                          new ColourPair(CoordsFactory.GetCoords(1, 2), CoordsFactory.GetCoords(2, 2), DotColours.Red),
                                          new ColourPair(CoordsFactory.GetCoords(1, 1), CoordsFactory.GetCoords(2, 1), DotColours.Green));

            var expectedCoordsList = new[]
            {
                startCoordsBlue,
                CoordsFactory.GetCoords(0, 2),
                CoordsFactory.GetCoords(0, 1),
                CoordsFactory.GetCoords(0, 0),
                CoordsFactory.GetCoords(1, 0),
                CoordsFactory.GetCoords(2, 0),
                CoordsFactory.GetCoords(3, 0),
                CoordsFactory.GetCoords(3, 1),
                CoordsFactory.GetCoords(3, 2),
                endCoordsBlue
            };

            // Act
            var pathFinder       = new PathFinder(CancellationToken.None);
            var initialPathsBlue = PathFinder.InitialPaths(colourPairBlue);
            var paths            = pathFinder.FindAllPaths(grid, endCoordsBlue, initialPathsBlue, 10);

            // Assert
            Assert.That(paths.Count(), Is.EqualTo(1));
            Assert.That(paths, Has.All.Matches <Path>(p => p.IsActive));
            Assert.That(paths, Has.Exactly(1).Matches <Path>(p => p.CoordsList.SequenceEqual(expectedCoordsList)));
        }
        public void OnePossiblePathWithTwoDirectionChanges_FindAllPathsCalledTwiceWithMaxDirectionChangesOneThenTwo_ReturnsTheOnePossiblePath()
        {
            // Arrange
            var startCoordsBlue = CoordsFactory.GetCoords(0, 3);
            var endCoordsBlue   = CoordsFactory.GetCoords(3, 3);

            // "BOOB"
            // " RR "
            // " GG "
            // "    "
            var colourPairBlue = new ColourPair(startCoordsBlue, endCoordsBlue, DotColours.Blue);
            var grid           = new Grid(4,
                                          colourPairBlue,
                                          new ColourPair(CoordsFactory.GetCoords(1, 3), CoordsFactory.GetCoords(2, 3), DotColours.Orange),
                                          new ColourPair(CoordsFactory.GetCoords(1, 2), CoordsFactory.GetCoords(2, 2), DotColours.Red),
                                          new ColourPair(CoordsFactory.GetCoords(1, 1), CoordsFactory.GetCoords(2, 1), DotColours.Green));

            var expectedCoordsList = new[]
            {
                startCoordsBlue,
                CoordsFactory.GetCoords(0, 2),
                CoordsFactory.GetCoords(0, 1),
                CoordsFactory.GetCoords(0, 0),
                CoordsFactory.GetCoords(1, 0),
                CoordsFactory.GetCoords(2, 0),
                CoordsFactory.GetCoords(3, 0),
                CoordsFactory.GetCoords(3, 1),
                CoordsFactory.GetCoords(3, 2),
                endCoordsBlue
            };

            // Act
            var pathFinder       = new PathFinder(CancellationToken.None);
            var initialPathsBlue = PathFinder.InitialPaths(colourPairBlue);
            var paths1           = pathFinder.FindAllPaths(grid, endCoordsBlue, initialPathsBlue, 1);
            var paths2           = pathFinder.FindAllPaths(grid, endCoordsBlue, paths1.ToList(), 2);

            // Assert 2
            Assert.That(paths2, Has.Exactly(1).Matches <Path>(p => p.CoordsList.SequenceEqual(expectedCoordsList) && p.IsActive));
        }
    void GenerateModuleInformation()
    {
        Array colourChoices = Enum.GetValues(typeof(Colours));

        _colourSequence = new ColourPair[NumberOfColoursInSequence];
        for (int colourIndex = 0; colourIndex < NumberOfColoursInSequence; ++colourIndex)
        {
            float sameRoll   = UnityEngine.Random.Range(0.0f, 100.0f);
            int   textIndex  = UnityEngine.Random.Range(0, colourChoices.Length);
            int   valueIndex = textIndex;

            if (sameRoll >= ProbabilityOfSameColourAndValue)
            {
                valueIndex = UnityEngine.Random.Range(0, colourChoices.Length);
            }

            Colours colourText  = (Colours)colourChoices.GetValue(textIndex);
            Colours colourValue = (Colours)colourChoices.GetValue(valueIndex);

            _colourSequence[colourIndex] = new ColourPair(colourText, colourValue);
        }
    }
    void Start()
    {
        StringBuilder logString = new StringBuilder();

        logString.Append("Module generated with the following word-color sequence:\n");
        logString.Append("# | Word    | Color   | Valid Response\n");
        logString.Append("--+---------+---------+----------------\n");

        string blockTitle = "";
        string condition  = "";

        GenerateModuleInformation();

        RuleButtonPressHandler ruleHandler = SetupRules(ref blockTitle, ref condition);

        SetRuleButtonPressHandler(ruleHandler);

        //Logging section
        for (int colourSequenceIndex = 0; colourSequenceIndex < _colourSequence.Length; ++colourSequenceIndex)
        {
            _currentColourSequenceIndex = colourSequenceIndex;
            ColourPair pair     = _colourSequence[_currentColourSequenceIndex];
            string     response = ruleHandler(true) ? "[YES]" : (ruleHandler(false) ? "[NO]" : "---");

            logString.AppendFormat("{0} | {1,-7} | {2,-7} | {3}\n", colourSequenceIndex + 1, pair.ColourText.ToString(), pair.ColourValue.ToString(), response);
        }
        logString.Append("\nThe sequence matched the following block title and condition statement:\n");
        logString.AppendFormat("\"{0}\"\n-> \"{1}\"\n", blockTitle, condition);

        BombModule.Log(logString.ToString());

        _currentColourSequenceIndex = -1;

        BombModule.OnActivate += HandleModuleActive;

        ButtonYes.KMSelectable.OnInteract += HandlePressYes;
        ButtonNo.KMSelectable.OnInteract  += HandlePressNo;
    }
    private void CheckRuleButtonPressHandler(bool yesButton)
    {
        if (_currentColourSequenceIndex < 0)
        {
            BombModule.LogFormat("[{0}] button was pressed at an unknown time!", yesButton ? "YES" : "NO");
        }
        else
        {
            ColourPair pair = _colourSequence[_currentColourSequenceIndex];
            BombModule.LogFormat("[{0}] button was pressed on entry #{1} ('{2}' word in '{3}' color)", yesButton ? "YES" : "NO", _currentColourSequenceIndex + 1, pair.ColourText.ToString(), pair.ColourValue.ToString());
        }

        if (_ruleButtonPressHandler != null && _ruleButtonPressHandler(yesButton))
        {
            BombModule.Log("Valid answer! Module defused!");
            BombModule.HandlePass();
            FinishModule();
        }
        else
        {
            BombModule.Log("Invalid answer! Module triggered a strike!");
            BombModule.HandleStrike();
        }
    }
        public void Grid2X2WithOneColourPairAndMaxDirectionChangesSetToOne()
        {
            // Arrange
            var startCoordsBlue = CoordsFactory.GetCoords(0, 0);
            var endCoordsBlue   = CoordsFactory.GetCoords(1, 1);

            // " B"
            // "B "
            var colourPairBlue = new ColourPair(startCoordsBlue, endCoordsBlue, DotColours.Blue);
            var grid           = new Grid(2, colourPairBlue);

            var expectedCoordsList1 = new[]
            {
                startCoordsBlue,
                CoordsFactory.GetCoords(1, 0),
                endCoordsBlue
            };

            var expectedCoordsList2 = new[]
            {
                startCoordsBlue,
                CoordsFactory.GetCoords(0, 1),
                endCoordsBlue
            };

            // Act
            var pathFinder       = new PathFinder(CancellationToken.None);
            var initialPathsBlue = PathFinder.InitialPaths(colourPairBlue);
            var paths            = pathFinder.FindAllPaths(grid, endCoordsBlue, initialPathsBlue, 1);

            // Assert
            Assert.That(paths.Count(), Is.EqualTo(2));
            Assert.That(paths, Has.All.Matches <Path>(p => p.IsActive));
            Assert.That(paths, Has.Exactly(1).Matches <Path>(p => p.CoordsList.SequenceEqual(expectedCoordsList1)));
            Assert.That(paths, Has.Exactly(1).Matches <Path>(p => p.CoordsList.SequenceEqual(expectedCoordsList2)));
        }
    private RuleButtonPressHandler SetupRulesForBlockB(ref string blockTitle, ref string condition)
    {
        if (_colourSequence.Any((x) => x.ColourText == Colours.Blue && x.ColourValue == Colours.Green))
        {
            condition = "1. If the word Blue is shown in Green color, press Yes on the first time Green is used as the color of the word.";

            return(delegate(bool yesPress)
            {
                return yesPress && _currentColourSequenceIndex == Array.FindIndex(_colourSequence, (x) => x.ColourValue == Colours.Green);
            });
        }

        if (_colourSequence.Any((x) => x.ColourText == Colours.White && (x.ColourValue == Colours.White || x.ColourValue == Colours.Red)))
        {
            condition = "2. (Otherwise,) if the word White is shown in either White or Red color, press Yes on the second time in the sequence where the color of the word does not match the word itself.";

            bool modified = false;
            while (_colourSequence.Count((x) => x.ColourText != x.ColourValue) < 2)
            {
                modified = true;
                ColourPair colourPair = _colourSequence.First((x) => x.ColourText == x.ColourValue);
                colourPair.ColourText = (Colours)(((int)colourPair.ColourText + 1) % Enum.GetValues(typeof(Colours)).Length);
            }

            if (modified)
            {
                //Modification has been made to match this rule's exit condition - check against all pre-existing rules
                return(SetupRules(ref blockTitle, ref condition));
            }

            return(delegate(bool yesPress)
            {
                if (!yesPress)
                {
                    return false;
                }

                int matchCount = 0;
                for (int colourSequenceIndex = 0; colourSequenceIndex < _colourSequence.Length; ++colourSequenceIndex)
                {
                    if (_colourSequence[colourSequenceIndex].ColourText != _colourSequence[colourSequenceIndex].ColourValue)
                    {
                        matchCount++;
                        if (matchCount == 2)
                        {
                            return _currentColourSequenceIndex == colourSequenceIndex;
                        }
                    }
                }

                return false;
            });
        }

        condition = "3. (Otherwise,) count the number of times Magenta is used as either the word or the color of the word in the sequence (the word Magenta in Magenta color only counts as one), and press No on the color in the total's position (e.g. a total of 4 means the fourth color in sequence).";

        //Otherwise, count the number of times Magenta is used as either the word or the colour of the word in the sequence, and press No on the colour in the total's position (i.e. a total of 4 means the fourth colour in sequence)
        if (!_colourSequence.Any((x) => x.HasColour(Colours.Magenta)))
        {
            if (UnityEngine.Random.Range(0, 2) == 0)
            {
                _colourSequence[UnityEngine.Random.Range(0, _colourSequence.Length - 1)].ColourText = Colours.Magenta;
            }
            else
            {
                _colourSequence[UnityEngine.Random.Range(0, _colourSequence.Length - 1)].ColourValue = Colours.Magenta;
            }

            //Modification has been made to match this rule's exit condition - check against all pre-existing rules
            return(SetupRules(ref blockTitle, ref condition));
        }

        return(delegate(bool yesPress)
        {
            return !yesPress && _currentColourSequenceIndex == _colourSequence.Count((x) => x.HasColour(Colours.Magenta)) - 1;
        });
    }
Exemple #10
0
 private void UpdateColours(ColourPair colourPair)
 {
     Console.BackgroundColor = (ConsoleColor)colourPair.Background;
     Console.ForegroundColor = (ConsoleColor)colourPair.Foreground;
 }
Exemple #11
0
 public LineBuffer(int capacity)
 {
     _buffer         = new StringBuilder(capacity);
     _wordBoundaries = new List <int>(5);
     _defaultColour  = new ColourPair(MessageColour.Text.ToANSIColour(), MessageColour.Background.ToANSIColour());
 }