Search code examples
javaarraysminesweeper

Having issue with printing out "labels" for my minesweeper code


I am writing minesweeper and I'm struggling with the following method- you can also look at the code below. I pass in a smaller map and I put in a new array but I'm struggling with the logic of how to get the 0 column and 0 row to print out like in this example. You can see some of my code below. Although I couldn't really get it to work the way I expected it.

/**
 * This prints out the map. This shows numbers of the columns
 * and rows on the top and left side, respectively. 
 * map[0][0] is row 1, column 1 when shown to the user.
 * The first column, last column and every multiple of 5 are shown.
 * 
 * To print out a 2 digit number with a leading space if the number
 * is less than 10, you may use:
 *     System.out.printf("%2d", 1); 
 * 
 * @param map The map to print out.
 */
public static void printMap(char[][] map) {
  char[][] mapWithNum = new char[map.length + 1][map[0].length + 1];
  for (int i = 1; i < mapWithNum.length; i++) {
    for (int j = 1; j < mapWithNum[0].length; j++) {
      mapWithNum[i][j] = map[i - 1][j - 1];
    }
  }
  for (int i = 0; i < mapWithNum.length; i++) {
    for (int j = 0; j < mapWithNum[0].length; j++) {
      if (i == 0 || j == 0) {
        if (i == 0 && j == 0) {
          System.out.print("  ");
        } else if (j == 1 && i == 0) {
          System.out.print(" " + j);
        } else if ((j) % 5.0 == 0) {
          System.out.print(" t1" + (j));
          if (j == map.length - 1 && i == 0) {
            System.out.print(" t2" + (j + 1));
          }
        } else {
          System.out.print("--");
          if (j == map.length - 1 && i == 0) {
            System.out.print(" t2" + (j + 1));
          }
        }


      } else {
        System.out.print(" " + mapWithNum[i][j]);
      }
      /*	if (j == 1) {
      		if (j % 5 == 0) {
      			System.out.print(" " + (i + 1));
      		}
      		else {
      			System.out.print('-');*/
      //}
      //	}
    }
    System.out.println();
  }

  return;
}

Any help would be greatly appreciated.


Solution

  • You don't need to create a new 2D array in your method. Just loop through the original one. The first thing your printMap() method should do is print the initial header line: " 1------ 5--------10". That is straightforward, even if you have to make it dynamic. Next, loop through your 2D array normally. Before you print each row, print the row prefix. For example, the prefix for the first row of your array is " ". The prefix for the second row is " 1 ". And so on. Note that there are different cases for rows 0, 4, 9, ... . The prefix for the other rows is simply " | ". Formatting with printf() will help here. You should print the prefix before entering the inner loop. Also remember not to print a newline character with your prefix. Your inner loop should continue where the prefix left off. Finally, the inner loop should print each element of the array. But remember to add a " " to each element when you print it. I suggest trying to print just the dots first. Then add the header line and finally the row prefixes.