Hello I'm a newbie in programming and I'm blocking on this error :
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at test.Program.Main (System.String[] args) [0x0009b] in <e574cc420fd948179f010210746876b3>:0
Any idea where does this could come from ?
Context: I develop a c# program on the console where the user will write a x,y positions + coins positions and the algorithm have to count if the user walk through coins (after the player write different movement indications that will influence x and y).
And here's the code :
class Program
{
static void Main(string[] args)
{
/* Get data from input file */
var data = new List<String>();
string line;
while ((line = Console.ReadLine()) != null) {
data.Add(line);
}
////////////////////////////////////////////////////
int N,x,y,D,P,pCollecte;
N = Int32.Parse(data[0]);
P = Int32.Parse(data[1]);
//pLieu is the location of the coins
Dictionary<string, string> pLieu = new Dictionary<string, string>();
for (int i = 0; i < P; i++)
{
pLieu.Add( "p" + i, data[2 + i]);
}
string ligne3;
ligne3 = data[3 + P];
string[] ligne3Num = ligne3.Split(',');
x = Int32.Parse(ligne3Num[0]);
y = Int32.Parse(ligne3Num[1]);
D = Int32.Parse(data[4 + P]);
Dictionary<string, string> Instructions = new Dictionary<string, string>();
for (int i = 0; i < D; i++)
{
Instructions.Add("d" + i, data[5 + P + i]);
}
string location;
// pcollecte are the number of coins collected
pCollecte = 0;
foreach (string item in Instructions.Values)
{
if (x >= N || x <= -N || y >= N || y <= -N)
{
break;
}
location = x + "," + y;
if (pLieu.ContainsKey(location))
{
pCollecte++;
pLieu.Remove(location);
}
switch (item.ToLower())
{
case "d":
y--;
break;
case "u":
y++;
break;
case "l":
x--;
break;
case "r":
x++;
break;
case "s":
x = x;
break;
}
}
if (x >= N || x <= -N || y >= N || y <= -N)
{
Console.WriteLine("out");
} else
{
Console.WriteLine(pCollecte);
}
}
}