0

Is there an easier way to pass an array through a recursive method than to convert it to a string and re assembling it again inside the method to keep the previous instance of that array for use inside a gametree.

the method only loops till desired depth is reached and bubbles up till all possible game states are mapped.

Example c#:

public class Gametree
{
  private char[] mapArr;

  private void execute(char[] map){
    mapArr = map;

    //do stuff to mapArr//

    var child = new Gametree();
    child.execute(mapArr);
  }
}
Sean
  • 60,939
  • 11
  • 97
  • 136
Kortgat
  • 3
  • 1

1 Answers1

2

You want to copy or clone your array, instead of reference it.

child.execute(mapArr.Clone());

or

char[] secondMap = new char[mapArr.length]
Copy(mapArr, secondMap, mapArr.length);
child.execute(secondMap);
Master117
  • 660
  • 6
  • 21
  • Alternatively Array.Copy and Array.Clone as described here http://stackoverflow.com/questions/198496/difference-between-the-system-array-copyto-and-system-array-clone. – PhillipH Jun 14 '16 at 11:22
  • I did try child.execute(mapArr.toList().toArray()); but somewhere along the line it still alters the wrong instance :( – Kortgat Jun 14 '16 at 13:02
  • @Kortgat use Clone or Copy, I'm not 100% sure about the toListtoArray thing. – Master117 Jun 14 '16 at 13:02
  • Thank you, with some playing around with Array.Copy() I got it to work correctly shaving off more than 30% of processing time :) – Kortgat Jun 15 '16 at 09:07