Hello everyone, pleased to be on the forum.

I'm currently undertaking some game development in Visual Studio, in my spare time. I've been programming for a few months, in Java, and now am having a go at C#. I have however run into a little snag with lists. I'm trying to create a board game, in which the characters move one step at a time around the board.

What I would like to know (as I've been searching for quite a while), is how to cycle through a List, and how to edit the contents of a list. In Java, I could use List.size() I believe, what is the C# equivalent?

Here is a snippet of the code I am using:

//create a list of properties
            List <Properties>properties = new List<Properties>();

            //initialize all the properties
            properties.Add(new Properties([B](pos1)[/B], "GO", false, false, 0, 0, 0, 0, false, false));
            properties.Add(new Properties([B](pos2)[/B], "Derelict Building", false, false, 200, 6, 60, 200, false, false));
            properties.Add(new Properties([B](pos3)[/B], "Shooting Range", false, false, 200, 8, 80, 200, false, false));
            properties.Add(new Properties([B](pos4)[/B], "Youth Hostel", false, false, 0, 100, 250, 0, false, false));

I want to cycle through the list, and say that if a variable, player1pos, is equal to pos(i) (the emboldened items), then to decrement the value of a dice roll and move to the next item in the list.

Here's a vague attempt at the code that obviously drew up syntactic errors...

if(player1)
            {
                int x = diceRoll.Roll(1,6);
         
                if (x != 0)
                {
                    for (int i; i < properties.size(); i++)
                    {
                        if (player1pos == properties.pos(i)) 
                        {
                            player1pos = properties.pos(i + 1);
                            x = x - 1;               
                        }
                    }
                }
    

            }

Each position (pos1, pos2, pos3, player1pos) is a co-ordinate of the type Vector2.

So basically, I want to cycle through a list, and to isolate the position variables.

jonsca commented: A well-crafted first post +1

I was initially thinking to tell you to use a foreach (should look them up anyway they are useful) but since you are accessing the next element in the list it's not a valid option. properties.Count will give you the information for the size of the list so you can use that in a regular for loop. You're not doing any list editing in this loop but the need to remove items mid-loop is another reason to stick with the regular for loop.

To obtain the size of your list: properties.Count . To access a member of Property named pos in a loop:

for (int i=0; i<properties.Count; i++)
{
    if (player1pos == properties[i].pos) 
    {
          // but, this will error (using i+1) if "i" is already at Count-1:
         player1pos = properties[i+1].pos;
         x = x - 1;               
    }
  }

Another way to find an element in a List is to use the .Find() method.
The syntax can be a little daunting if your new to C# but its fairly simple once you get used to it :) you can check out the msdn page for predicate search, it shows a nice little example using a method as a delegate. You can also create a delegate search method inline like so:

List <Properties>properties = new List<Properties>();

            //initialize all the properties
            properties.Add(new Properties((pos1), "GO", false, false, 0, 0, 0, 0, false, false));
            properties.Add(new Properties((pos2), "Derelict Building", false, false, 200, 6, 60, 200, false, false));
            properties.Add(new Properties((pos3), "Shooting Range", false, false, 200, 8, 80, 200, false, false));
            properties.Add(new Properties((pos4), "Youth Hostel", false, false, 0, 100, 250, 0, false, false));

Properties desiredProperty = properties.Find(delegate (Properties p) {return p.pos==player1pos; });

If you are using .net 3.5 you can do it with lambda expressions which are even more streamlined: Properties desiredProperty = properties.Find( p => p.pos == player1pos); Sorry if its code overload, i'd recommend reading up on both the loops AND the predicate methods, there are a few methods in c# that use a predicate so it helps to get your head around it :) Now that i have a handle on them i must admit i abuse them no end...i prefer a single line search to a chunky foreach loop every time i need to locate a List item.

Thanks very much for the replies, very informative and helpful. I've modified my loop to the following, which I thought would be fine and dandy, however it doesn't quite work:

if(player1)
            {
                int x = diceRoll.Roll(1,6);
                if (x != 0)
                {
                    for (int i = 0; i < properties.Count - 1; i++)
                    {
                        if (player1pos == properties[i].pos)
                        {
                            x = x - 1;
                            player1pos = properties[i + 1].pos; //properties[i+1].pos;
                        }
                    }
                }
                player1 = false;
                player2 = true;
            }

So basically, this loop is for moving the character around the board. I have a character linked to player1pos, as you may have already guessed.

1) The dice is rolled (I'm going to change this to a user prompt soon).
2) If the diceRoll isn't equal to 0, then decrement the diceroll by 1 and move the player to the next property.


The problem however, is from the initalized player1pos, which is pos1, I ran the program and it ended up in the final position, pos24. player1pos should have been a value between pos1 and pos7.

Any ideas?

Well, the way you have your for loop set up to see at which property the player resides, you are perhaps moving it to the last property regardless of where it started. Perhaps once a move is made you should break; from the for loop and proceed on to the next turn.

EDIT: Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.