Example #1
0
        /// <summary>
        /// Returns the game to the state it was in after the specified number of turns.
        /// </summary>
        /// <param name="turnCount">The number of turns.</param>
        /// <returns>A list of the spaces affected by the last move, starting with the space
        /// of the move and including the spaces of all the pieces captured by the move.</returns>
        private IAsyncOperation <IList <ISpace> > ResetAsync(int turnCount)
        {
            if (turnCount < 0 || turnCount > MoveStack.Count)
            {
                throw new ArgumentException("must be 0 or greater and " +
                                            "no higher than MoveStack.Count.", "turnCount");
            }

            if (turnCount == Moves.Count)
            {
                throw new ArgumentException("must be different than " +
                                            "Moves.Count.", "turnCount");
            }

            // Use a lock to prevent this method from modifying the game state at
            // the same time a different thread is in the locked MoveAsync method.
            lock (_lockObject)
            {
                IList <ISpace> newMoves;
                if (turnCount < Moves.Count)
                {
                    // Backward navigation resets the board to its initial state in
                    // preparation for replaying all moves up to the indicated move.
                    SetUpEmptyBoard();
                    newMoves = Moves.Take(turnCount).ToList();
                    Moves.Clear();
                }
                else
                {
                    // Forward navigation retrieves previously-undone moves in
                    // preparation for their replay from the current board state.
                    newMoves = MoveStack.Skip(Moves.Count)
                               .Take(turnCount - Moves.Count).ToList();
                }
                return(MoveAsync(newMoves));
            }
        }