I've been trying to complete an assignment for my game design class in C#, one of the issues I have been having is that I cannot load my Save1.GAME text file with all the loading information, I receive a "System.FormatException: 'Input string was not in a correct format.'" error.
At the moment, this assignment includes a small panel where you, the player designs a Sokoban (https://en.wikipedia.org/wiki/Sokoban) map, I have already done this part and now I must load that save file into my program, then generate the "tiles" (the tiles just being little squares where items are) into the different panel where the game will actually be played.
So far I've tried to load the file and write it into a string array, line by line. I've also tried to write the entire file into a string and use the .split(',') function, to no avail. I've tried quite a few ideas that honestly I've forgotten every single one of them.
My load button on the form where the game will be played:
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openLevelDialog = new OpenFileDialog();
openLevelDialog.Title = "Select level to load";
if (openLevelDialog.ShowDialog() == DialogResult.OK)
{
string MyString = System.IO.File.ReadAllText(openLevelDialog.FileName);
int i = 0;
int j = 0;
//My array where I will just dump the whole file into.
int[,] result = new int[10, 10];
//foreach loop where I attempt to go line-by-line and split the individual numbers by ','
foreach (var row in MyString.Split('\n'))
{
j = 0;
foreach (var col in row.Trim().Split(','))
{
result[i, j] = int.Parse(col.Trim()); //Exception happens here.
j++;
}
i++;
}
//Just an attempt to display what the variable values in my form, ignore this part.
for (int a = 0; a < result.GetLength(0); a++)
{
for (int w = 0; w < result.GetLength(1); w++)
{
label1.Text += result[a,w].ToString();
}
}
}
}
And this is the Game1.GAME file
2,2 <--- This is the size of the map, 2X2 = 4 tiles total. ---
0,0,0 <--- This is tile [0,0] and according to my TileTypes enum inside my tile.cs class it should be empty hence, the third 0. ---
0,1,1 <--- This is tile [0,1] and according to my TileTypes enum inside my tile.cs class it should be "The Hero" hence, the 1. ---
1,0,2 <--- This is tile [1,0] and according to my TileTypes enum inside my tile.cs class it should be a wall hence, the 2. ---
1,1,3 <--- This is tile [1,1] and according to my TileTypes enum inside my tile.cs class it should be a box hence, the 3. ---
note: there is a 4th enum with the value of "Destination" but in this particular map I just didn't add any.
How it looks usually
2,2
0,0,0
0,1,1
1,0,2
1,1,3
I was hoping it would just load the string, chop it up into the int array but I cant seem to get past the exception no matter what I do.
Thanks for you're time in advance.
\r(carrier return) or line feed perhaps? can you open this file up in notepad++ , select "View", "Show All Symbols" and screenshot and display that here?openLevelDialog.FileNamea full path?System.IO.File.ReadAllTextreturning what you expect?openLevelDialog.FileNamereturns a full path, The System.IO command also returns the numbers and commas all perfectly formattedReadAllLinesinstead ofReadAllText, replacing.Split('\n')usage.