public override void Reset()
        {
            _state = TwisterState.Waiting;

            _digits.Clear();
            _digits.Add(new TwisterDigit(FingerGames.Instance.GameManager.Players[0], 0));
            if (FingerGames.Instance.GameManager.Players.Count > 1)
            {
                _digits.Add(new TwisterDigit(FingerGames.Instance.GameManager.Players[1], 0));
                _digits.Add(new TwisterDigit(FingerGames.Instance.GameManager.Players[0], 1));
                _digits.Add(new TwisterDigit(FingerGames.Instance.GameManager.Players[1], 1));
            }
            else
            {
                _digits.Add(new TwisterDigit(FingerGames.Instance.GameManager.Players[0], 1));
                _digits.Add(new TwisterDigit(FingerGames.Instance.GameManager.Players[0], 2));
            }
            _activeDigit = _digits[0];

            foreach (GamePlayer player in FingerGames.Instance.GameManager.Players)
            {
                player.Score = 0;
            }

            _newIndex = -1;

            base.Reset();
        }
 public FingerTwisterGame(GameManager gameManager)
     : base(gameManager, "moves", ScoreCenter.SortDirection.Descending)
 {
     _digits.Add(new TwisterDigit(gameManager.Players[0], 0));
     if (gameManager.Players.Count > 1)
     {
         _digits.Add(new TwisterDigit(gameManager.Players[1], 0));
         _digits.Add(new TwisterDigit(gameManager.Players[0], 1));
         _digits.Add(new TwisterDigit(gameManager.Players[1], 1));
     }
     else
     {
         _digits.Add(new TwisterDigit(gameManager.Players[0], 1));
         _digits.Add(new TwisterDigit(gameManager.Players[0], 2));
     }
     _activeDigit = _digits[0];
 }
 public FingerTwisterGame(GameManager gameManager)
     : base(gameManager, "moves", ScoreCenter.SortDirection.Descending)
 {
     _digits.Add(new TwisterDigit(gameManager.Players[0], 0));
     if (gameManager.Players.Count > 1)
     {
         _digits.Add(new TwisterDigit(gameManager.Players[1], 0));
         _digits.Add(new TwisterDigit(gameManager.Players[0], 1));
         _digits.Add(new TwisterDigit(gameManager.Players[1], 1));
     }
     else
     {
         _digits.Add(new TwisterDigit(gameManager.Players[0], 1));
         _digits.Add(new TwisterDigit(gameManager.Players[0], 2));
     }
     _activeDigit = _digits[0];
 }
        /// <summary>
        /// Take N samples. Speed is number of alternating taps over that period
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime, ref InGameState gameState, bool justTransitioned)
        {
            TouchCollection touchCollection = TouchPanel.GetState();

            int activeTouches = 0;

            Dictionary<int, TouchLocation> touches = new Dictionary<int, TouchLocation>();
            foreach (TouchLocation touchLocation in touchCollection)
            {
                if (touchLocation.State == TouchLocationState.Pressed || touchLocation.State == TouchLocationState.Moved)
                {
                    touches.Add(touchLocation.Id, touchLocation);
                    activeTouches++;
                }
            }

            if (_state == TwisterState.Waiting)
            {
                _state = TwisterState.PlayerInstruction;
            }
            else
            {
                // If we pull off of a circle (when not transitioning) we lose
                foreach (TwisterCircle circle in _circles)
                {
                    if (circle.Digit != null && !circle.IsBlinking)
                    {
                        if (!touches.ContainsKey(circle.Digit.TouchId))
                        {
                            bool found = false;
                            foreach (TouchLocation touchLoc in touchCollection)
                            {
                                Point searchPoint = new Point((int)touchLoc.Position.X, (int)touchLoc.Position.Y);
                                if (circle.Contains(searchPoint))
                                {
                                    //if (touches.ContainsKey(touchLoc.Id))
                                    //{
                                    //    //
                                    //}
                                    //else
                                    {
                                        circle.Digit.TouchId = touchLoc.Id;
                                        found = true;
                                    }
                                }
                            }

                            if (!found)
                            {
                                circle.MissingCount++;

                                if (circle.MissingCount > 20)
                                {
                                    // game over
                                    _winnerLabel.Text = string.Format("{0} Loses ({1} touches)!", circle.Digit.Player.Name, circle.Digit.Player.Score);
                                    gameState = InGameState.End;
                                }
                            }
                        }
                        else
                        {
                            TouchLocation location = touches[circle.Digit.TouchId];
                            Point searchPoint = new Point((int)location.Position.X, (int)location.Position.Y);
                            if (!circle.Contains(searchPoint))
                            {
                                // game over
                                _winnerLabel.Text = string.Format("{0} Loses ({1} touches)!", circle.Digit.Player.Name, circle.Digit.Player.Score);
                                gameState = InGameState.End;
                            }
                            else
                            {
                                circle.MissingCount = 0;
                            }
                        }
                    }
                }

                if (_state == TwisterState.Feedback)
                {
                    if ((gameTime.TotalGameTime - _messageTime).TotalSeconds > 2)
                    {
                        _state = TwisterState.PlayerInstruction;
                    }
                }
                else if (_state == TwisterState.PlayerInstruction)
                {
                    if (_newIndex < 0)
                    {
                        int circleIndex = 0;
                        List<int> inactiveIndices = new List<int>();
                        foreach (TwisterCircle circle in _circles)
                        {
                            if (null == circle.Digit)
                            {
                                inactiveIndices.Add(circleIndex);
                            }
                            circleIndex++;
                        }
                        _newIndex = inactiveIndices[FingerGames.Randomizer.Next(0, inactiveIndices.Count())];

                        if (null != _activeDigit.ActiveCircle)
                        {
                            _activeDigit.ActiveCircle.IsBlinking = true;
                        }
                        _circles[_newIndex].IsBlinking = true;
                        _circles[_newIndex].MissingCount = 0;
                    }
                    else
                    {
                        Dictionary<int, TwisterDigit> digitTouches = new Dictionary<int, TwisterDigit>();

                        foreach (TwisterDigit digit in _digits)
                        {
                            if (digit.TouchId != 0)
                            {
                                if (!touches.ContainsKey(digit.TouchId))
                                {
                                    // digit picked up his finger -- game over
                                    //return true;
                                }
                                else if (!digitTouches.ContainsKey(digit.TouchId))
                                {
                                    digitTouches.Add(digit.TouchId, digit);
                                }
                            }
                        }
                        foreach (TouchLocation location in touches.Values)
                        {
                            if (!digitTouches.ContainsKey(location.Id))
                            {
                                // new id
                                _activeDigit.TouchId = location.Id;
                                break;
                            }
                        }

                        if (_activeDigit.TouchId != 0 && touches.ContainsKey(_activeDigit.TouchId))
                        {
                            // Our finger is down
                            TouchLocation loc = touches[_activeDigit.TouchId];

                            if (_circles[_newIndex].Contains(new Point((int)loc.Position.X, (int)loc.Position.Y)))
                            {
                                // we got it...
                                if (null != _activeDigit.ActiveCircle)
                                {
                                    _activeDigit.ActiveCircle.Digit = null;
                                    _activeDigit.ActiveCircle.IsBlinking = false;
                                }
                                _circles[_newIndex].Digit = _activeDigit;
                                _circles[_newIndex].IsBlinking = false;
                                _circles[_newIndex].MissingCount = 0;
                                _activeDigit.ActiveCircle = _circles[_newIndex];

                                _activeDigit.Player.Score++;
                                float playerMod = (GameManager.Players.IndexOf(_activeDigit.Player) == 0) ? -0.5f : 0.5f;
                                SoundManager.Play(PopSound, 1.0f, playerMod, playerMod);

                                _newIndex = -1;
                                int index = _digits.IndexOf(_activeDigit);
                                index = (index + 1) % _digits.Count();

                                _activeDigit = _digits[index];

                                _state = TwisterState.Feedback;
                                _message = "Well Done!";
                                _messageTime = gameTime.TotalGameTime;
                            }
                        }
                    }
                }
            }

            foreach (TwisterCircle circle in _circles)
            {
                circle.Update(gameTime);
            }
        }
        public override void Reset()
        {
            _state = TwisterState.Waiting;

            _digits.Clear();
            _digits.Add(new TwisterDigit(FingerGames.Instance.GameManager.Players[0], 0));
            if (FingerGames.Instance.GameManager.Players.Count > 1)
            {
                _digits.Add(new TwisterDigit(FingerGames.Instance.GameManager.Players[1], 0));
                _digits.Add(new TwisterDigit(FingerGames.Instance.GameManager.Players[0], 1));
                _digits.Add(new TwisterDigit(FingerGames.Instance.GameManager.Players[1], 1));
            }
            else
            {
                _digits.Add(new TwisterDigit(FingerGames.Instance.GameManager.Players[0], 1));
                _digits.Add(new TwisterDigit(FingerGames.Instance.GameManager.Players[0], 2));
            }
            _activeDigit = _digits[0];

            foreach (GamePlayer player in FingerGames.Instance.GameManager.Players)
            {
                player.Score = 0;
            }

            _newIndex = -1;

            base.Reset();
        }
        /// <summary>
        /// Take N samples. Speed is number of alternating taps over that period
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime, ref InGameState gameState, bool justTransitioned)
        {
            TouchCollection touchCollection = TouchPanel.GetState();

            int activeTouches = 0;

            Dictionary <int, TouchLocation> touches = new Dictionary <int, TouchLocation>();

            foreach (TouchLocation touchLocation in touchCollection)
            {
                if (touchLocation.State == TouchLocationState.Pressed || touchLocation.State == TouchLocationState.Moved)
                {
                    touches.Add(touchLocation.Id, touchLocation);
                    activeTouches++;
                }
            }

            if (_state == TwisterState.Waiting)
            {
                _state = TwisterState.PlayerInstruction;
            }
            else
            {
                // If we pull off of a circle (when not transitioning) we lose
                foreach (TwisterCircle circle in _circles)
                {
                    if (circle.Digit != null && !circle.IsBlinking)
                    {
                        if (!touches.ContainsKey(circle.Digit.TouchId))
                        {
                            bool found = false;
                            foreach (TouchLocation touchLoc in touchCollection)
                            {
                                Point searchPoint = new Point((int)touchLoc.Position.X, (int)touchLoc.Position.Y);
                                if (circle.Contains(searchPoint))
                                {
                                    //if (touches.ContainsKey(touchLoc.Id))
                                    //{
                                    //    //
                                    //}
                                    //else
                                    {
                                        circle.Digit.TouchId = touchLoc.Id;
                                        found = true;
                                    }
                                }
                            }

                            if (!found)
                            {
                                circle.MissingCount++;

                                if (circle.MissingCount > 20)
                                {
                                    // game over
                                    _winnerLabel.Text = string.Format("{0} Loses ({1} touches)!", circle.Digit.Player.Name, circle.Digit.Player.Score);
                                    gameState         = InGameState.End;
                                }
                            }
                        }
                        else
                        {
                            TouchLocation location    = touches[circle.Digit.TouchId];
                            Point         searchPoint = new Point((int)location.Position.X, (int)location.Position.Y);
                            if (!circle.Contains(searchPoint))
                            {
                                // game over
                                _winnerLabel.Text = string.Format("{0} Loses ({1} touches)!", circle.Digit.Player.Name, circle.Digit.Player.Score);
                                gameState         = InGameState.End;
                            }
                            else
                            {
                                circle.MissingCount = 0;
                            }
                        }
                    }
                }

                if (_state == TwisterState.Feedback)
                {
                    if ((gameTime.TotalGameTime - _messageTime).TotalSeconds > 2)
                    {
                        _state = TwisterState.PlayerInstruction;
                    }
                }
                else if (_state == TwisterState.PlayerInstruction)
                {
                    if (_newIndex < 0)
                    {
                        int        circleIndex     = 0;
                        List <int> inactiveIndices = new List <int>();
                        foreach (TwisterCircle circle in _circles)
                        {
                            if (null == circle.Digit)
                            {
                                inactiveIndices.Add(circleIndex);
                            }
                            circleIndex++;
                        }
                        _newIndex = inactiveIndices[FingerGames.Randomizer.Next(0, inactiveIndices.Count())];

                        if (null != _activeDigit.ActiveCircle)
                        {
                            _activeDigit.ActiveCircle.IsBlinking = true;
                        }
                        _circles[_newIndex].IsBlinking   = true;
                        _circles[_newIndex].MissingCount = 0;
                    }
                    else
                    {
                        Dictionary <int, TwisterDigit> digitTouches = new Dictionary <int, TwisterDigit>();

                        foreach (TwisterDigit digit in _digits)
                        {
                            if (digit.TouchId != 0)
                            {
                                if (!touches.ContainsKey(digit.TouchId))
                                {
                                    // digit picked up his finger -- game over
                                    //return true;
                                }
                                else if (!digitTouches.ContainsKey(digit.TouchId))
                                {
                                    digitTouches.Add(digit.TouchId, digit);
                                }
                            }
                        }
                        foreach (TouchLocation location in touches.Values)
                        {
                            if (!digitTouches.ContainsKey(location.Id))
                            {
                                // new id
                                _activeDigit.TouchId = location.Id;
                                break;
                            }
                        }

                        if (_activeDigit.TouchId != 0 && touches.ContainsKey(_activeDigit.TouchId))
                        {
                            // Our finger is down
                            TouchLocation loc = touches[_activeDigit.TouchId];

                            if (_circles[_newIndex].Contains(new Point((int)loc.Position.X, (int)loc.Position.Y)))
                            {
                                // we got it...
                                if (null != _activeDigit.ActiveCircle)
                                {
                                    _activeDigit.ActiveCircle.Digit      = null;
                                    _activeDigit.ActiveCircle.IsBlinking = false;
                                }
                                _circles[_newIndex].Digit        = _activeDigit;
                                _circles[_newIndex].IsBlinking   = false;
                                _circles[_newIndex].MissingCount = 0;
                                _activeDigit.ActiveCircle        = _circles[_newIndex];

                                _activeDigit.Player.Score++;
                                float playerMod = (GameManager.Players.IndexOf(_activeDigit.Player) == 0) ? -0.5f : 0.5f;
                                SoundManager.Play(PopSound, 1.0f, playerMod, playerMod);

                                _newIndex = -1;
                                int index = _digits.IndexOf(_activeDigit);
                                index = (index + 1) % _digits.Count();

                                _activeDigit = _digits[index];

                                _state       = TwisterState.Feedback;
                                _message     = "Well Done!";
                                _messageTime = gameTime.TotalGameTime;
                            }
                        }
                    }
                }
            }

            foreach (TwisterCircle circle in _circles)
            {
                circle.Update(gameTime);
            }
        }