private IEnumerator SetSquareColors(bool delay, bool solve = false)
    {
        if (delay)
        {
            yield return(new WaitForSeconds(Rnd.Range(1.5f, 2f)));
        }

        var sequence = _updateIndices != null?_updateIndices.ToList().Shuffle() : Enumerable.Range(0, 16).ToList().Shuffle();

        for (int i = 0; i < sequence.Count; i++)
        {
            Scaffold.SetButtonColor(sequence[i], _colors[sequence[i]]);
            yield return(new WaitForSeconds(.03f));
        }

        if (solve)
        {
            ModulePassed();
            _activeCoroutine = null;
        }
        else
        {
            _activeCoroutine = StartCoroutine(BlinkLastSquare());
        }

        _updateIndices = null;
    }
 private void ProcessCurrentSquare()
 {
     while (true)
     {
         if (_flowchart[_flowchartPosition].Contains(_colors[_modulePosition]))
         {
             _flowchartPosition = _pointsAtY[_flowchartPosition];
             Log("{0} color is {1}, which is in the flowchart cell, so I expect you to press it. Flowchart position now {2}.", convertCoord(_modulePosition, 4), _colors[_modulePosition], convertCoord(_flowchartPosition, 6));
             return;
         }
         else
         {
             var next = NextSquare(_modulePosition);
             if (next == null)
             {
                 var possibleLastColors = _flowchart[_flowchartPosition].ToArray();
                 if (possibleLastColors.Length == 0)
                 {
                     Log("You ran into the rare case where the last square has you on the empty flowchart cell. Module solves prematurely.");
                     ModulePassed();
                     return;
                 }
                 _colors[_modulePosition] = possibleLastColors[Rnd.Range(0, possibleLastColors.Length)];
                 Scaffold.SetButtonColor(_modulePosition, _colors[_modulePosition]);
                 continue;
             }
             _flowchartPosition = _pointsAtN[_flowchartPosition];
             Log("{0} color is {1}, which is NOT in the flowchart cell. Moving on to {2}. Flowchart position now {3}.", convertCoord(_modulePosition, 4), _colors[_modulePosition], convertCoord(next.Value, 4), convertCoord(_flowchartPosition, 6));
             _updateColors.Add(_modulePosition);
             _modulePosition = next.Value;
         }
     }
 }
    protected override void ButtonPressed(int index)
    {
        if (_stage == 0)
        {
            // Preliminary stage in which the player presses the four “live” colors in any order of their choice
            if (_colors[index] == SquareColor.White)    // ignore re-presses
            {
                return;
            }
            if (_colors[index] == _neutralColor)
            {
                Log("During the preliminary stage, you pressed a square that wasn’t one of the singular colors. Strike.");
                Strike();
                SetInitialState();
                return;
            }
            PlaySound(index);
            _rememberedColors[_subprogress]    = _colors[index];
            _rememberedPositions[_subprogress] = index;
            _subprogress++;

            // If all colors have been pressed, initialize stage 1
            if (_subprogress == 4)
            {
                Log("You pressed them in this order: {0}", Enumerable.Range(0, 4).Select(ix => string.Format("{0} ({1})", coord(_rememberedPositions[ix]), _rememberedColors[ix])).Join(", "));
                Scaffold.SetAllButtonsBlack();
                SetStage(1);
            }
            else
            {
                _colors[index] = SquareColor.White;
                Scaffold.SetButtonColor(index, SquareColor.White);
            }
            return;
        }

        if (index != _expectedPresses[_subprogress])
        {
            Log("Expected {0}, but you pressed {1}. Strike. Module resets.", coord(_expectedPresses[_subprogress]), coord(index));
            Strike();
            SetInitialState();
            return;
        }

        PlaySound(index);
        _subprogress++;
        _colors[index] = SquareColor.White;
        Scaffold.SetButtonColor(index, SquareColor.White);
        Log("{0} was correct.", coord(index));
        if (_subprogress == _expectedPresses.Count)
        {
            SetStage(_stage + 1);
        }
    }
    private IEnumerator BlinkLastSquare()
    {
        bool lit = false;

        while (_lastPress != -1)
        {
            Scaffold.SetButtonColor(_lastPress, lit ? SquareColor.White : _colors[_lastPress]);
            lit = !lit;
            yield return(new WaitForSecondsRealtime(0.5f));
        }

        _activeCoroutine = null;
    }
예제 #5
0
    protected override void ButtonPressed(int index)
    {
        if (!_permissiblePatterns.Any(p => p.Contains(index)))
        {
            Log("Button {0}{1} was incorrect at this time. Resetting module.", "ABCD"[index % 4], "1234"[index / 4]);
            Strike();
            SetStage(isStart: true);
        }
        else
        {
            PlaySound(index);
            _permissiblePatterns.RemoveAll(lst => !lst.Contains(index));
            _squaresPressedThisStage.Add(index);
            _colors[index] = SquareColor.White;
            Scaffold.SetButtonColor(index, SquareColor.White);

            if (_permissiblePatterns.Count == 1 && _squaresPressedThisStage.Count == _permissiblePatterns[0].Count)
            {
                SetStage(isStart: false);
            }
        }
    }
    protected override void ButtonPressed(int index)
    {
        if (_activeCoroutine != null)
        {
            StopCoroutine(_activeCoroutine);
        }

        if (_lastPress != -1)
        {
            Scaffold.SetButtonColor(_lastPress, _colors[_lastPress]);
        }

        if (_lastPress == -1 && _allowedPresses.Contains(index))
        {
            PlaySound(index);
            _lastPress        = index;
            _startingPosition = index;
            _allowedPresses   = CalculateNewAllowedPresses(index);

            Log("Button #{0} pressed successfully. Current color is now {1}. Next color is {2}.", index, _currentColor, _nextColor);
            _activeCoroutine = StartCoroutine(BlinkLastSquare());
        }
        else if (!_allowedPresses.Contains(index))
        {
            Log("Button #{0} ({1}) was incorrect at this time.", index, _colors[index]);
            Strike();
            SetInitialState();
        }
        else
        {
            PlaySound(index);
            _lastPress = index;

            _updateIndices = new HashSet <int>();
            LogDebug("Calling SpreadColor({0}, {1}, {2})", _currentColor, _colors[index], _startingPosition);
            SpreadColor(_currentColor, _colors[index], _startingPosition);
            if (_updateIndices.SetEquals(_lastArea))
            {
                _pressesWithoutChange++;
            }
            else
            {
                _lastArea             = _updateIndices;
                _pressesWithoutChange = 0;
            }

            _currentColor = _colors[index];

            if (_pressesWithoutChange >= 3)
            {
                var currentColor = _colors[index];
                while (currentColor == _colors[index])
                {
                    _colors[index] = _colorCandidates[Rnd.Range(0, _colorCandidates.Length)];
                }
                _pressesWithoutChange = 0;
                Scaffold.SetButtonBlack(index);
                Scaffold.Audio.PlaySoundAtTransform("colorreset", Scaffold.Buttons[index].transform);
            }

            if (_colors.All(c => c == _colors[0]))
            {
                Log("Module passed.");
                _allowedPresses  = null;
                _activeCoroutine = StartCoroutine(SetSquareColors(delay: false, solve: true));
            }
            else
            {
                _allowedPresses = CalculateNewAllowedPresses(index);

                Log("Button #{0} pressed successfully. Current color is now {1}. Next color is {2}.", index, _currentColor, _nextColor);
                _activeCoroutine = StartCoroutine(SetSquareColors(delay: false));
            }
        }
    }
    protected override void ButtonPressed(int index)
    {
        if (_expectedPresses == null)
        {
            return;
        }

        if (!_allowedPresses.Contains(index))
        {
            Log(@"Button #{0} ({1}) was incorrect at this time.", index, _colors[index]);
            Strike();
            SetInitialState();
        }
        else
        {
            PlaySound(index);
            _expectedPresses.Remove(index);
            _colors[index] = SquareColor.White;
            Scaffold.SetButtonColor(index, SquareColor.White);
            if (_expectedPresses.Count == 0)
            {
                var whiteCount = _colors.Count(c => c == SquareColor.White);
                if (whiteCount == 16)
                {
                    _expectedPresses = null;
                    _allowedPresses  = null;
                    ModulePassed();
                }
                else
                {
                    _allowedPresses.Clear();

                    var nonWhite = Enumerable.Range(0, 16).Where(i => _colors[i] != SquareColor.White).ToArray();
                    foreach (var i in nonWhite)
                    {
                        Scaffold.SetButtonBlack(i);
                        _colors[i] = (SquareColor)Rnd.Range(1, 6);
                    }

                    // Move to next stage.
                    var nextStage = _table[whiteCount - 1][_lastStage is SquareColor ? (int)(SquareColor)_lastStage - 1 : _lastStage.Equals(true) ? 5 : 6];
                    Log("{0} lit: next stage is {1}.", whiteCount, nextStage.Equals(true) ? "Row" : nextStage.Equals(false) ? "Column" : ((SquareColor)nextStage).ToString());
                    if (nextStage.Equals(true))
                    {
                        // Row
                        var firstRow = Enumerable.Range(0, 4).First(row => Enumerable.Range(0, 4).Any(col => _colors[4 * row + col] != SquareColor.White));
                        for (int col = 0; col < 4; col++)
                        {
                            if (_colors[4 * firstRow + col] != SquareColor.White)
                            {
                                _allowedPresses.Add(4 * firstRow + col);
                                _expectedPresses.Add(4 * firstRow + col);
                            }
                        }
                    }
                    else if (nextStage.Equals(false))
                    {
                        // Column
                        var firstCol = Enumerable.Range(0, 4).First(col => Enumerable.Range(0, 4).Any(row => _colors[4 * row + col] != SquareColor.White));
                        for (int row = 0; row < 4; row++)
                        {
                            if (_colors[4 * row + firstCol] != SquareColor.White)
                            {
                                _allowedPresses.Add(4 * row + firstCol);
                                _expectedPresses.Add(4 * row + firstCol);
                            }
                        }
                    }
                    else
                    {
                        // A specific color
                        // Make sure at least one square has that color
                        var color = (SquareColor)nextStage;
                        _colors[nonWhite[Rnd.Range(0, nonWhite.Length)]] = color;
                        for (int i = 0; i < 16; i++)
                        {
                            if (_colors[i] == color)
                            {
                                _allowedPresses.Add(i);
                                _expectedPresses.Add(i);
                            }
                        }
                    }
                    _lastStage = nextStage;
                    Scaffold.StartSquareColorsCoroutine(_colors, SquaresToRecolor.NonwhiteOnly, delay: true);
                }
            }
        }
    }