0

I'm trying to create the game 2048 but a little different. Instead of displaying a grid that is usually 4x4, I want to display the grid based on what the user would like. For example, if the user enters the number 6, the game board should be 6x6. The code I have here is for displaying a grid 4x4, but I'm stuck on trying to figure out how to display any grid. I have been stuck on this for hours.

How do I fix this to display the game board based on the user's input?

    String rowDashes = "----------------";
    
    for ( int i = 0; i < board.length; i++) {
        System.out.println(rowDashes);
        System.out.print("|");
        
        for (int j =0; j < board[i].length;j++) {
            String number = "";
            if (board[i][j] != 0)
            {
                number = board[i][j] + "";
            }
            System.out.print("   " + number + "|");
        }
        System.out.println();
    }
    System.out.println(rowDashes);

1 Answers1

1

Edit: I read it wrong. I added dummy values like 100, 1800, 45. You can remove values or initialize them by randomly.

import java.util.Scanner;

public class Game {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter board size: ");
        int boardSize = scanner.nextInt();

        StringBuilder rowLine = new StringBuilder();
        for (int i = 0; i < boardSize; i++) {
            rowLine.append("+-----");
        }
        rowLine.append("+");

        int[][] board = new int[boardSize][boardSize];
        board[0][2] = 100;
        board[1][3] = 1800;
        board[1][2] = 45;

        System.out.println(rowLine);
        for (int i = 0; i < board.length; i++) {
            System.out.print("|");
            for (int j = 0; j < board[i].length; j++) {
                int value = board[i][j];
                if (value > 0) {
                    System.out.printf("%5d", board[i][j]);
                } else {
                    System.out.print("     "); // 5 characters
                }
                System.out.print("|");
            }
            System.out.println();
            System.out.println(rowLine);
        }
    }
}

Output:

Enter board size: 6
+-----+-----+-----+-----+-----+-----+
|     |     |  100|     |     |     |
+-----+-----+-----+-----+-----+-----+
|     |     |   45| 1800|     |     |
+-----+-----+-----+-----+-----+-----+
|     |     |     |     |     |     |
+-----+-----+-----+-----+-----+-----+
|     |     |     |     |     |     |
+-----+-----+-----+-----+-----+-----+
|     |     |     |     |     |     |
+-----+-----+-----+-----+-----+-----+
|     |     |     |     |     |     |
+-----+-----+-----+-----+-----+-----+
Ismail Durmaz
  • 2,521
  • 1
  • 6
  • 19
  • Sorry Im new to java. What does rowLine.append mean?And also for StringBuilder is it the same as : String rowLine = new String(); – Melissa Wakwich Dec 15 '20 at 22:33
  • `StringBuilder` is a class that stores values with appending at the tail of the text. We prefer `StringBuilder` or `StringBuffer` for concatenating operations. It is more readable and memory-optimized way. For more information [Link](https://www.geeksforgeeks.org/string-vs-stringbuilder-vs-stringbuffer-in-java/) – Ismail Durmaz Dec 15 '20 at 22:38