0

I'm trying to figure out how to jump to the beginning of an array after i reached the end.

Basically my code has multiple holes filled with stones and if you select a hole all stones from that hole will be taken away and distributed in the following holes 1 at each time.

Now if the last hole is reached it should start at the beginning of the array and put the remaining stones in the holes at the beginning.

Unfortunately right now all i get is an error message stating

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

My code in this specific code so far is:

for (int i = chosenHoleInt; i <= stonesOnChosenField + chosenHoleInt; i++)
{
    board.holes[chosenHoleInt].Stones--;

    board.holes[i].Stones++;

    if (chosenHoleInt > board.holes.Length)
    {
        chosenHoleInt = 0;
    }
}

I thought that i could solve the problem by implementing the board.holes.length part but apparently that didn't work at all. Any help would be appreciated since i'm still learning and quite stuck right now. :)

Tony Abrams
  • 4,505
  • 3
  • 25
  • 32
  • 7
    I suggest you debug through the code, looking at which line is failing and what index is being requested. Unfortunately we don't know how the values of `stonesOnChosenField` and `chosenHoleInt` are derived, making it hard to help further. A [mcve] would fix that - but I suspect that debugging through it will be more valuable. – Jon Skeet Jun 12 '19 at 14:05
  • Although I'd also say that as `if (chosenHoleInt > board.holes.Length)` comes *later* in the loop body than access to `board.holes[chosenHoleInt]`, that if statement isn't going to help you. – Jon Skeet Jun 12 '19 at 14:06
  • 2
    Also note that arrays have lower bounds of zero, so you'd need that test to be `if (chosenHoleInt >= board.holes.Length)` – Jon Skeet Jun 12 '19 at 14:07
  • 1
    https://stackoverflow.com/questions/25894948/make-an-array-work-like-a-circle – Owen Pauling Jun 12 '19 at 14:07
  • When it starts, you decrement `board.holes[chosenHoleInt].Stones` and immediately increment it again, since `i=chosenHoleInt`. So init `i` with `chosenHoleInt+1`. For the "wrap around" inside the array I would recommend the modulo operation: `i % board.holes.Length` – JeffRSon Jun 12 '19 at 14:48

2 Answers2

1

I would do something like this:

int i = chosenHoleInt;
        while(Board.Holes[chosenHoleInt].Stones != 0)
        {
            if (i >= Board.Holes.Length)
            {
                i = 0;
                continue;
            }

            Board.Holes[chosenHoleInt].Stones--;
            Board.Holes[i].Stones++;
            i++;
        }
SSharma
  • 951
  • 6
  • 15
0

This example should work for you:

 for (int i = chosenHoleInt; i < stonesOnChosenField + chosenHoleInt; i++)
 {
        if (chosenHoleInt >= board.holes.Length || i >= board.holes.Length)
        {
            chosenHoleInt = 0;
            continue;
        }

        board.holes[chosenHoleInt].Stones--;


        board.holes[i].Stones++;
 }

as mentioned in the comments, you should first check if your index is within the boundaries of your array, and then try to access it. Also, you need some changes in boundary checking in your loops

crankedrelic
  • 463
  • 1
  • 5
  • 14
  • I strongly suspect this could still fail due to `board.holes[i]`. We don't know much about the variables here. – Jon Skeet Jun 12 '19 at 14:28
  • you 're right, I did edit that, although i don't know if that's the expected functionality. The main point is for them to get why they go out of bounds. – crankedrelic Jun 12 '19 at 14:31