View Single Post
  #1 (permalink)  
Old June 4th, 2018, 09:12 AM
oda356 oda356 is offline
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 25%
Activity: 25% Activity: 25% Activity: 25%
 
Join Date: Jun 2018
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 7: GameMoves.cs

Hello, I'm learning chapeter 7, which talks about the array, and emuerator. I can't understand how the GameMove.cs works. Could you help me?
Code:
    
public class GameMoves
    {
        private IEnumerator _cross;
        private IEnumerator _circle;
        public GameMoves()
        {
            _cross = Cross();
            _circle = Circle();
        }
        private int _move = 0;
        const int MaxMoves = 9;
        public IEnumerator Cross()
        {
            while (true)
            {
                WriteLine($"Cross, move {_move}");
                if (++_move >= MaxMoves)
                    yield break;
                yield return _circle;
            }
        }
        public IEnumerator Circle()
        {
            while (true)
            {
                WriteLine("Circle, move {0}", _move);
                if (++_move >= MaxMoves)
                    yield break;
                yield return _cross;
            }
        }
    }
Code:
 
            var game = new GameMoves();
            IEnumerator enumerator = game.Cross();
            while (enumerator.MoveNext())
            {
                enumerator = enumerator.Current as IEnumerator;
            }
In the initialization of the game object, the
Code:
_cross = Cross();
calls the Cross() function, which return the _cross IEnumerator, but it is not initialized at that time. What makes it work?

When the
Code:
enumerator.MoveNext()
is called, does the enumerator.Current change to the _circle IEnumerator? If so, when the
Code:
enumerator.MoveNext()
is called again what mechanism associate the _circle and Circle(), to make the Circle() function called then?

Thank you.
Reply With Quote