As you can see from the screenshot there are occasions when errors happen
My code should at those moments be observing the points above the white blocks, checking either side of them to see if there are any other empty sections, and if not the circled areas should be empty, not blocks.
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//initiate lists (1 list for object), (2 list for occupied nearby)
int[] list1 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list2 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list3 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list4 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list5 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list6 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list7 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list8 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list9 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list10 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list11 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list12 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list13 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list14 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list15 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list16 = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[][] lists = new int[][] { list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11, list12, list13, list14, list15, list16 };
int[] list1a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list2a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list3a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list4a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list5a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list6a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list7a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list8a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list9a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list10a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list11a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list12a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list13a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list14a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list15a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[] list16a = new int[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int[][] listsa = new int[][] { list1a, list2a, list3a, list4a, list5a, list6a, list7a, list8a, list9a, list10a, list11a, list12a, list13a, list14a, list15a, list16a };
//assign walls
Random random = new Random();
//full
// open = 2, unnasigned = 0, block = 1
for (int i = 0; i <= 15; i++)
{
for (int j = 0; j <= 15; j++)
{
// % chance of block spawning
int r = 2;
int p = random.Next(1, 11);
if (p > 5) { r = 1; }
else if (p < 6) { r = 2; }
//do we have any surrounding open* blocks? (above,sides) *(not unassigned, OPEN)
int[] openspaces = new int[3] { 0, 0, 0 }; //for recording which spaces are open
var open = "no"; //assumed to be no
//check space to sides and above, but not below as always unassigned
try { if (lists[j][i - 1] == 2) { open = "yes"; openspaces[0] = 1; } } catch (IndexOutOfRangeException) { } //above
try { if (lists[j + 1][i] == 2) { open = "yes"; openspaces[1] = 1; } } catch (IndexOutOfRangeException) { } //right
try { if (lists[j - 1][i] == 2) { open = "yes"; openspaces[2] = 1; } } catch (IndexOutOfRangeException) { } //left
if (open == "no")
{
lists[j][i] = r; // 50/50 chance as completely singular square
}
else if (open == "yes")
{ // if the spaces are open, do they have any other open or undecided blocks near them (under, sides)
int[] otheropen = new int[3] { 0, 0, 0 }; //for recording which nearby open block have other nearby open spaces
if (openspaces[0] == 1) //above
{
//check space to sides, but not above as not relavent, and below would be checking itself, and none are invalid
try { if ((lists[j + 1][i - 1] == 0 || lists[j + 1][i - 1] == 2) && listsa[j + 1][i - 1] != 1) { otheropen[0] = 1; } } catch (IndexOutOfRangeException) { }
try { if ((lists[j - 1][i - 1] == 0 || lists[j - 1][i - 1] == 2) && listsa[j - 1][i - 1] != 1) { otheropen[0] = 1; } } catch (IndexOutOfRangeException) { }
}
else if (openspaces[0] == 0) { otheropen[0] = 2; } // if the block is not open it is ignored
if (openspaces[1] == 1) //right
{
//check space to right and below, but not above as not relavent, and left would be checking itself, and none are invalid
try { if ((lists[j + 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j + 1][i + 1] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { }
try { if ((lists[j + 2][i] == 0 || lists[j + 1][i] == 2) && listsa[j + 2][i] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { }
}
else if (openspaces[1] == 0) { otheropen[1] = 2; } // if the block is not open it is ignored
if (openspaces[2] == 1) //left
{
//check space to left and below, but not above as not relavent, and right would be checking itself, and none are invalid
try { if ((lists[j - 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j - 1][i + 1] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { }
try { if ((lists[j - 2][i] == 0 || lists[j - 1][i] == 2) && listsa[j - 2][i] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { }
}
else if (openspaces[2] == 0) { otheropen[2] = 2; } // if the block is not open it is ignored
if (otheropen[0] == 0 || otheropen[1] == 0 || otheropen[2] == 0) { lists[j][i] = 2; } //sets self as open, as a nearby open block has no other open blocks
else if (otheropen[0] != 0 && otheropen[1] != 0 && otheropen[2] != 0)
//yes all the near open block have a possible other route
{
lists[j][i] = r; // 50/50 as this block will not create a dead end
if (r == 1)//if original block closed then nearby open blocks will become invalid for next check, i.e. considered closed
{
try { if (otheropen[0] == 1) { listsa[j][i - 1] = 1; } } catch (IndexOutOfRangeException) { }//top
try { if (otheropen[1] == 1) { listsa[j + 1][i] = 1; } } catch (IndexOutOfRangeException) { }//right
try { if (otheropen[2] == 1) { listsa[j + 1][i] = 1; } } catch (IndexOutOfRangeException) { }//left
}
}
}
}
}
for (int j = 0; j <= 15; j++)
{
Console.WriteLine();
for (int i = 0; i <= 15; i++)
{
if (lists[j][i] == 1) { Console.Write("██"); }
if (lists[j][i] == 2) { Console.Write("░░"); }
}
}
}
}
I can't figure out why this error is happening, could it be due to the try checking. I know this is a large code dump, and probably pretty difficult to interpret from what I've posted, and I'm sure I could do stuff more efficiently as I'm new to C#, but any help would be incredible.
I really want for every square to be either accessible or to be a wall, but as you can see wall cross the whole area at points.
EDIT: Process explination:
So it iterates through a large array. First, it looks to see if there are any "open blocks" (of value 2) above and to the sides of the current specific value in the 2d array. If there aren't it randomly decides if the block is "open" or "filled" (value of 2 or 1 respectively).
If there is an open block, it will check each of those blocks surrounding blocks (the side and below) to see if there are any other open(2) blocks that could act as paths. If there are then it acknowledges this and randomly chooses again. If one of these blocks has no surrounding open blocks, then the current value should become an open block.
If a random choice is to be a 'filled' block after the latter process, then it will assign the near block as no longer being valid as open to the same process again, so that when another block is considering if it can use that as a path it knows it cannot.
Alright, after reformatting your code to make it much easier I found what I believe to be the error (at least I cannot get the error to reproduce after these changes. (The code below is not reformatted to make it easier for you to find in your code)
In this section
if (openspaces[1] == 1) //right
{
//check space to right and below, but not above as not relavent, and left would be checking itself, and none are invalid
try { if ((lists[j + 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j + 1][i + 1] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { }
try { if ((lists[j + 2][i] == 0 || lists[j + 1][i] == 2) && listsa[j + 2][i] != 1) { otheropen[1] = 1; } } catch (IndexOutOfRangeException) { }
}
else if (openspaces[1] == 0) { otheropen[1] = 2; } // if the block is not open it is ignored
if (openspaces[2] == 1) //left
{
//check space to left and below, but not above as not relavent, and right would be checking itself, and none are invalid
try { if ((lists[j - 1][i + 1] == 0 || lists[j][i - 1] == 2) && listsa[j - 1][i + 1] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { }
try { if ((lists[j - 2][i] == 0 || lists[j - 1][i] == 2) && listsa[j - 2][i] != 1) { otheropen[2] = 1; } } catch (IndexOutOfRangeException) { }
}
If you look at where it says try { if ... you will notice that the second condition in your or statement is different then the first condition. They should be the same. It looks like you fell victim to a copy paste error.
As #Tipx suggested reformatting your code would have made this much easier to find. As you are new to C#, learn from the beginning to make your code readable. You will thank yourself later.
Note, your code still allows chunks of path to be separated from the main path (set your random seed to 0 and look at the top right corner), but you shouldn't have any more individual squares like you were having.
Related
I created a static class to keep all my "level" arrays in (I'm writing a game in monogame, using XNA).
The problem is that i can't use the class anywhere, it just won't show up in intellisense, and returns an error The name 'Rooms' does not exist in the current context. Any idea what may cause this?
namespace Marked_V0._2
{
public static class Rooms
{
static public int[,] Level1 = new int[,]
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1 }
};
}
}
Error:
CS1002 ; Expected
CS1001 Identifier expected
CS0103 The name 'Rooms' does not exist in the current context.
The namespace for Rooms most likely does not match the namespace of the rest of the project. You can either change the namespace or include the extra namespace with using Marked_V0._2.
I'm using the EmguCV 3.0.0 wrapper to OpenCV 3.0. The EmguCV PCACompute method wraps the OpenCV PCA::operator() method.
The following code compiles and runs. The comments should explain the intent just fine.
// Populate the 5 row by 8 column input array (5 samples of 8 dimensions).
// The sample dimensions (columns) vary like this:
// - low variance: 0, 1, 4, 5, 6, 7
// - high variance: 2, 3
Matrix<double> input = new Matrix<double>(5, 8);
var r = new Random();
for (int row = 0; row < 5; row++) {
input.Data[row,0] = r.Next(0, 10); // low variance
input.Data[row,1] = r.Next(0, 20); // low variance
input.Data[row,2] = r.Next(80, 210); // high variance
input.Data[row,3] = r.Next(0, 240); // highest variance
input.Data[row,4] = r.Next(20, 21); // very low variance
input.Data[row,5] = r.Next(0, 10); // low variance
input.Data[row,6] = r.Next(0, 10); // low variance
input.Data[row,7] = r.Next(200, 210); // low variance
}
// create output array for PCACompute()
var eigenvectors = new Matrix<double>(8, 8);
// create *empty* mean array so that PCACompute() calculates its own means
var means = new Mat();
// HERE IS THE MAGIC.
CvInvoke.PCACompute(input, means, eigenvectors);
But the magic is broken. eigenvectors is all zeros after all that. This pretty print code:
// print each eigenvector on its own line
for (int vectorIdx = 0; vectorIdx < eigenvectors.Rows; vectorIdx++) {
string vectorStr = "";
for(int dimension = 0; dimension < eigenvectors.Cols; dimension++) {
vectorStr += eigenvectors.Data[vectorIdx, dimension].ToString() + ", ";
}
Console.WriteLine("{ " + vectorStr.Substring(0, vectorStr.Length - 2) + " }");
}
gives this output:
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
In fact, if I set a member of eigenvectors before passing it to PCACompute:
eigenvectors.Data[1,1] = 42;
CvInvoke.PCACompute(input, means, eigenvectors);
the pretty print shows that eigenvectors was completely untouched by PCACompute:
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 42, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0, 0, 0 }
Is this a bug, or am I doing this wrong?
Despite being compatible according to the type system, the 3rd parameter passed to PCACompute cannot be of Matrix<> type. Everything works fine if it is a Mat type, though!
Matrix<double> input = new Matrix<double>(5, 8);
var r = new Random();
for (int row = 0; row < 5; row++) {
input.Data[row,0] = r.Next(0, 10); // low variance
input.Data[row,1] = r.Next(0, 20); // low variance
input.Data[row,2] = r.Next(80, 210); // high variance
input.Data[row,3] = r.Next(0, 240); // highest variance
input.Data[row,4] = r.Next(20, 21); // very low variance
input.Data[row,5] = r.Next(0, 10); // low variance
input.Data[row,6] = r.Next(0, 10); // low variance
input.Data[row,7] = r.Next(200, 210); // low variance
}
// CHANGE HERE
// Matrix<> was not changed by PCACompute(), so change eigenvectors to a Mat
var eigenvectors = new Mat(8, 8, DepthType.Cv64F, 1);
// create *empty* mean array so that PCACompute() calculates its own means
var means = new Mat();
// Now the magic works fine.
CvInvoke.PCACompute(input, means, eigenvectors);
Now eigenvectors contains more reasonable data (though it takes different accessor code in the pretty print function)
> eigenvectors.GetValueRange().Max
0.999418
> eigenvectors.GetValueRange().Min
-0.6326281
I'm essentially trying to do the Game Of Life. All seems well, for the most part it works as I want. But for some reason when I go to print it out and have it play, it prints two incomplete parts of the array side by side. Half of the cells, about every other, on on the right, the other half are on the left. I have no idea what I'm doing wrong, I've used similar methods to print 2d arrays before with no trouble, so this is really baffling me. Code below.
class Program
{
static void Main(string[] args)
{
GameOfLife myGame = new GameOfLife();
myGame.Start();
}
}
class Game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, 0, 0, 0, 0, 0, 0}};
private int[,] tempGrid;
public GameOfLife()
{
tempGrid = new int[grid.GetLength(0), grid.GetLength(1)];
}
public void Start()
{
Setup();
do
{
Thread.Sleep(30);
UpdateGrid();
CopyTempGridToGrid();
PrintGrid();
} while (!Console.KeyAvailable);
Console.WriteLine();
}
private void UpdateGrid()
{
for(int r = 0; r < grid.GetLength(0); r++)
{
for(int c = 0; c < grid.GetLength(1); c++)
{
tempGrid[r, c] = UpdateCell(r, c);
}
}
}
private void CopyTempGridToGrid()
{
Array.Copy(tempGrid, grid, tempGrid.Length);
}
private void PrintGrid()
{
Console.CursorTop = 2;
for (int r = 0; r < grid.GetLength(0); r++)
{
for (int c = 0; c < grid.GetLength(1); c++)
{
if (grid[r, c] == 0)
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write(".");
}
else
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(".");
}
}
}
}
private int GetNeighbors(int row, int col)
{
int neighbors = 0;
try
{
if (grid[row - 1, col - 1] == 1)
neighbors++;
if (grid[row - 1, col] == 1)
neighbors++;
if (grid[row - 1, col + 1] == 1)
neighbors++;
if (grid[row, col - 1] == 1)
neighbors++;
if (grid[row, col + 1] == 1)
neighbors++;
if (grid[row + 1, col - 1] == 1)
neighbors++;
if (grid[row + 1, col] == 1)
neighbors++;
if (grid[row + 1, col + 1] == 1)
neighbors++;
}
catch (IndexOutOfRangeException) { }
return neighbors;
}
private int UpdateCell(int row, int col)
{
int n = GetNeighbors(row, col);
if (n <= 1)
return 0;
if (n == 2)
return grid[row, col];
if (n == 3)
return 1;
else
return 0;
}
private void Setup()
{
Console.Clear();
Console.CursorVisible = false;
Console.WriteLine("Press any key to stop");
PrintGrid();
}
}
Although the loops within PrintGrid() have inner and an outer nested loops, they are not outputting a newline after each row. I'd try a Console.Writeln("") between the inner and outer loops.
I am trying to have a loop read a multidimensional array defined in a separate class to place objects at the start of a game.
This is the Start function code from the LevelStart class:
void Start () {
//Get Level
Levels levels = ScriptableObject.CreateInstance<Levels>();
level = levels.GetLevel (levelName);
x = level.GetLength(1);
y = level.GetLength (2);
temp = GameObject.Find ("temp").GetComponent<Temp>();
lPos = temp.GetLevelNum() - 1; //Chosen Level
//Read array and place objects
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (level[lPos,i,j] == 1) {
Debug.Log ("Spawn Point");
}
else if (level[lPos,i,j] == 2) {
Debug.Log ("Hole");
}
else if (level[lPos,i,j] == 3) {
Debug.Log ("Obstacle");
}
else
Debug.Log ("Open Space");
}
}
}
And the class holding the arrays is Levels.cs:
public int[,,] cave = new int[,,] {
{ //Level 1
{ 0, 0, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0},
{ 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 3, 0, 0},
{ 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 3, 0, 0, 0},
{ 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 3, 0},
{ 0, 0, 3, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0},
{ 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0},
{ 0, 0, 3, 0, 3, 0, 3, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{ 0, 0, 0, 0, 2, 0, 0, 0, 3, 0, 3, 0, 0, 0}
},
{ //Level 2
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 },
{ 0, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0 },
{ 0, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 3, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0 },
{ 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 3, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0 },
{ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0 }
}
};
It reads most of the array correctly but positions such as [0,0,0] and [0,0,12] returns 3's when it should be 0
Thanks in advance
Seems to me to be pretty likely that you're simply using the wrong level number - code that is not shown. I suggest using the print statement to ensure temp.GetLevelNum() and lPos are the numbers you think they should be.
I am using C# in a console application.
I need to load data from a text file and load it into a 2d array.
This is what I tried, but when I try to print out the contents of what gets returned nothing gets printed.
public static int[,] LoadMap()
{
const string path = #"1.txt";
string[] fileLines = File.ReadAllLines(path);
int[,] map = new int[fileLines.Length, 15];
string line;
for (int i = 0; i < fileLines.Length; ++i)
{
line = fileLines[i];
for (int j = 0; j < line.Length; ++j)
{
map[i, j] = (int)(line[j] - '0');
}
}
return map;
}
But when I hardcode the data like that, then everything gets displayed perfectly.
private static int[,] Map = new int[MapX, MapY]
{
{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
The data in the text file looks like that :
0,0,0,1,1,1,1,1,1,1,1,1,1,1,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
Any help will be appreciated whether you fix what I tried or propose something completely different, thanks.
string[] fileLines = File.ReadAllLines(path);
int[,] map = new int[fileLines.Length,fileLines[0].Split(',').Length];
for (int i = 0; i < fileLines.Length; ++i)
{
string line = fileLines[i];
for (int j = 0; j < map.GetLength(1); ++j)
{
string[] split = line.Split(',');
map[i, j] = Convert.ToInt32(split[j]);
}
}
return map;
}
You could use LINQ to parse the lines:
var lines = File.ReadAllLines(path);
int[,] map = new int[fileLines.Length, 25];
for (int i = 0; i < fileLines.Length; ++i)
{
var data = lines[i].Split(',').Select(c => Convert.ToInt32(c)).ToList();
for(int j =0; j<25; ++j)
map[i,j] = data[j];
}
return map;
If you could use a jagged array instead of a 2D array, this becomes simpler:
public static int[][] LoadMap()
{
return File.ReadLines(path)
.Select(l => l.Split(',').Select(Convert.ToInt32).ToArray())
.ToArray();
}
If your text file has commas separating the values, replace this line:
for (int j = 0; j < line.Length; ++j)
With:
for (int j = 0; j < line.Length; j += 2)
That's assuming your values will always be only 1 char long.
When the data comes in from the text file. If it is separated by a comma you can separate it using string.split. Then you load what you get into an array and access it like you normally would do an array.
Like below:
string[] lines = System.IO.File.ReadAllLines(#"path");
foreach (string line in lines)
{
string[] first= line.Split(comma);
}