0

Why I'm doing this:

So I'm trying to make an application for a game called clash royale, after winning the games there you get a "random" chest, which is actually not random... When you create your account you get a digit assigned to you from 0 to 239, and after that it follows a pattern for the chest drops. The applciation I'm making would take a user's entries and compare it to the pattern, thus being able to predict how soon the next chests of a higher quality would drop.

The help I need with the code:

Is it possible to make an array kind of... loop within itself.. So for example when going through the array in a loop, if "i" is 239, then adding +1 would take it back to the beginning, or #0 (239 not necessarily being the limit).

The class (and it's container that I want to loop):

class Chest
    {
        public int ID { get; set; }
        public string Type { get; set; }

        public Chest()
        {

        }
        public Chest(int id, string type)
        {
            ID = id;
            Type = type;
        }
    }
class ChestContainer
    {
        private Chest[] ChestList = new Chest[240];
        public int Count { get; set; }

        public ChestContainer(int size)
        {
            ChestList = new Chest[size];
        }
        public void Add(Chest chest)
        {
            ChestList[Count++] = chest;
        }
        public Chest Get(int index)
        {
            return ChestList[index];
        }
    }

Also wouldn't mind any tips to improve my class / container class, at the moment this is what I've been doing for pretty much my entire "career" as this is what we were thought in uni (minus the string override for the class).

1
  • 2
    Look into modular arithmetic Commented Mar 17, 2017 at 21:50

3 Answers 3

1

You could use Modulo % in order to get a loop kind of thing.

If you replace the Container.Add method with the one below, the index will be "reset" (for lack of better words).

public void Add(Chest chest)
{
    ChestList[Count++%(ChestList.Length)] = chest;
}

After updating the method, if you want an example, you can try the code below:

var container = new ChestContainer(240);

for (int i = 0; i < 1000; i++)
    container.Add(new Chest(i, $"{i}"));

Edit In order to have the Get method working as well, modifying it as mentioned below will ensure your container works as expected:

public Chest Get(int index)
{
    return ChestList[index%(ChestList.Length)];
}

To test it out, you can use the code below:

var container = new ChestContainer(240);

for (int i = 0; i < 1000; i++)
{
    container.Add(new Chest(i, $"{i}"));
    var value = container.Get(i);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Damn that's the full package :D I'll test it out when I have access to the computer on which I have the code, and reply to this answer if something comes up. Cheers mate!
0

You can overload the [] operator to define it's behaviour.

Something like this:

public static Chest operator [] (int index) {
    return ChestList[index%240];
}

Comments

0
 public Chest Get(int index)
 {
     return ChestList[index%240]; //put your limit here
 }

How it works: % is the modulo operator. It returns the remainder of a devision. Example: 5/2 = 2, remaining 1 => 5%2 = 1

In your case, when numbers higher than 239 are entered, with modulo it just wraps around.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.