I'm doing some challenges over at codefights.com and I'm having trouble figuring out why my code is acting the way it is.
https://codefightssolver.wordpress.com/2016/11/09/minesweeper/
^^^There's a link to someone else blog containing the challenge's prompt verbatim. I'm not sure if you can see the challenge on codefights if you aren't logged in.
int[][] minesweeper(bool[][] matrix)
{
int[][] mineField = new int[matrix.Length][];
int[] mineLine = new int[matrix[0].Length];
for (int i = 0; i < mineLine.Length; i++) { mineLine[i] = 0; }
for (int i = 0; i < mineField.Length; i++) { mineField[i] = mineLine; }
for (int i = 0; i < mineField.Length; i++)
{
for (int j = 0; j < mineField[0].Length; j++)
{
mineField[i][j] = findBorderMines(matrix, i, j);
Console.Write(mineField[i][j]);
}
Console.WriteLine();
}
return mineField;
}
int findBorderMines(bool[][] matrix, int x, int y)
{
int minX = x - 1; if (minX < 0) minX = 0;
int minY = y - 1; if (minY < 0) minY = 0;
int maxX = x + 1; if (maxX > matrix.Length - 1) maxX = matrix.Length - 1;
int maxY = y + 1; if (maxY > matrix[0].Length - 1) maxY = matrix[0].Length - 1;
int borderingMines = 0;
for (int i = minX; i <= maxX; i++)
{
int j = minY;
for (; j <= maxY; j++)
{
//Console.WriteLine(i + " " + j);
if (matrix[i][j] == true && !(i == x && j == y)) { borderingMines++; }
}
}
return borderingMines;
}
^^^This is my full code. I know it isn't the most elegant solution, but I'm not looking for that right now. My problem (I think) concerns the bit of code below vvv
for (int i = 0; i < mineField.Length; i++)
{
for (int j = 0; j < mineField[0].Length; j++)
{
mineField[i][j] = findBorderMines(matrix, i, j);
Console.Write(mineField[i][j]);
}
Console.WriteLine();
}
return mineField;
The writelines display the correct matrix, but when I go to return the resultant matrix, all lines in the matrix are magically transformed into the matirx's last line. What am I missing? I hope that's enough info to make sense of what I'm screwing up, but I never perfect to boiling my goofball issues down to one forum post.