Jump to content
  • Advertisement
Sign in to follow this  
daedalus0x1a4

[C#] Summing a list

This topic is 3174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey whats good? im implementing a d20 dice system where i have a method that rolls the dice and adds the results to a list<int> type im trying to decide what the best way to sum the results would be. i have read about a sum method that is an extension of ienumerable types added to lists and arrays by LINQ but in the example i saw he references system.linq; while the closest match i have is system.data.linq; so i implemented it like this:
        public int Roll(int NumberOfDice, int Faces)
        {
            int Sum = 0;
            List<int> Rolls = new List<int>();

            for (int i = 0; i < NumberOfDice; i++)
            {
                Rolls.Add(Die.Next(0, Faces));
            }

            IEnumerator<int> Enumerator = Rolls.GetEnumerator();

            while (Enumerator.MoveNext())
            {
                Sum += Enumerator.Current;
            }

            return Sum;
        }
it would have been nice to simply say Rolls.Sum(); i guess i could have used a for loop as well. whats really the best way to accomplish this, in your opinions?

Share this post


Link to post
Share on other sites
Advertisement
If you're using .NET 3.5, you can use the extension method like you mentioned. It's included in the System.Core DLL, and is located under the System.Linq namespace as a member of the static Enumerable class as an extension method to IEnumerable<T>.

Share this post


Link to post
Share on other sites
The main Linq assembly is System.Core, I believe. Try adding a reference to that and you should be able to use the Sum method.


edit: damnit, long dogged by 14 secs! damn you, hard cat! [razz]

Share this post


Link to post
Share on other sites
Am I missing something? Why not this:
public int Roll(int numDice, int numFaces){
int Sum = 0;
for(int i=0; i<numDice; ++i){
Sum += Die.Next(0, numFaces);
}
return Sum;
}
Or at least:
public int Roll(int numDice, int numFaces){
int Sum = 0;
List<int> Rolls;
for(int i=0; i<numDice; ++i){
int roll = Die.Next(0, numFaces);
Sum += roll;
Rolls.Add(roll);
}
return Sum;
}
if you intend to actually use all the values later.

Share this post


Link to post
Share on other sites
i wasn't thinking about it that way, in addition to being rusty :) thanks ocelot. thanks mike and chaos too. i didn't realize i needed to reference system.core.
:)

Share this post


Link to post
Share on other sites
okay. okay.



okay.


public int Roll(int NumberOfDice, int Faces)
{
int Sum = 0;
for (int i = 0; i < NumberOfDice; i++)
{
Sum += Die.Next(0, Faces);
}
return Sum;
}

public List<int> Roll(int NumberOfDice, int Faces, out int Sum)
{
List<int> Rolls = new List<int>();
for (int i = 0; i < NumberOfDice; i++)
{
Rolls.Add(Die.Next(0, Faces));
}
Sum = Enumerable.Sum(Rolls);
return Rolls;
}


this'll do
:]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!