1

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.

Tom Buck
  • 41
  • 4
  • 1
    Try to output the complete matrix after computing one line. What does happen? – Korrat Jun 29 '18 at 07:11
  • All lines are the first line, which is correct. – Tom Buck Jun 29 '18 at 07:24
  • 1
    Should that happen? – Korrat Jun 29 '18 at 07:27
  • No... clearly it's replacing the whole matrix with the newest line... but I don't know why... I know I'm going to hate myself when I see it, but I don't know why. – Tom Buck Jun 29 '18 at 07:32
  • 2
    Take a look at where you initialize those lines. What might be the problem? – Korrat Jun 29 '18 at 07:34
  • Please tell me this is rhetorical. Do you know? I'm looking for it either way. – Tom Buck Jun 29 '18 at 07:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174011/discussion-between-korrat-and-tom-buck). – Korrat Jun 29 '18 at 07:38
  • 2
    Arrays are reference types. This line `mineField[i] = mineLine` sets all "sub arrays" of mineField to the same thing. Making a change to one sub array will reflect in them all. You'll have to rethink how you initialize the array (don't do what you're doing) – pinkfloydx33 Jun 29 '18 at 08:40
  • @pinkfloydx33 You literally sent that seconds after I asked Korrat if that was my problem in a private chat. How do I fix it...? It's worth mentioning I am just starting out in my quest to be a programming master. (I'm mildly proud that I figured it out before I saw your message.) – Tom Buck Jun 29 '18 at 08:47
  • 2
    Get rid of the first loop, then use `for (int i = 0; i < mineField.Length; i++) { mineField[i] = new int[matrix.Length]; }` You don't need to initialize the elements to zero because an array of ints will automatically be initialized as such. Also you may consider a multidimensional array `int[,]` instead of a jagged array `int[][] ` since a minesweeper grid is rectangular (at least in your case) and I think using a jagged array just makes it more confusing for you – pinkfloydx33 Jun 29 '18 at 08:53
  • I believe you've solved my problem, but... the challenge asks for a jagged array to be returned. Not trying to sound snobby, but did you read the challenge I linked? Is it possible to return a multidimensional array when they are asking for a jagged one? If so, how? By the way, I can't figure out why they ask for specifically a jagged array. – Tom Buck Jun 29 '18 at 08:59
  • @pinkfloydx33 The test examples were rectangular, but not necessarily square so I ended up needing to use matrix[0].Length instead of matrix.Length. Either way, problem solved and understood. I still need to learn about how to use and interchange jagged and multidimensional arrays, but that might be for another day. – Tom Buck Jun 29 '18 at 09:08
  • @Korrat I wanted to shout you out as well. Thanks for the help, guys. #betterCoderEveryDay – Tom Buck Jun 29 '18 at 09:10
  • They don't need to be square to use a two dimensional array. Just rectangular (and not the case where one line has for example five items and the next has seventeen). `var mineField = new int[height, width];` – pinkfloydx33 Jun 29 '18 at 09:10

0 Answers0