Home > Back-end >  Java Two-Dimensional Array Syntax Wrapping Borders
Java Two-Dimensional Array Syntax Wrapping Borders

Time:03-13

I am creating a two-dimensional array in Java that reads a number from the user and makes a grid that size. To start the array, I would like to place a 1 in the middle of the top row. We will call this K. After K has been placed, I would like to place K 1 to the right and up wrapping around the borders. However, if the position to the right and up has already been filled, OR it is in the upper right-hand corner, then you must move to the position straight down instead. Here is an example of a 7x7 grid that follows these steps.

30 39 48 1 10 19 28

38 47 7 9 18 27 29

46 6 8 17 26 35 37

5 14 16 25 34 36 45

13 15 24 33 42 44 4

21 23 32 41 43 3 12

22 31 40 49 2 11 20

I tried a couple of different things, and I was unable to come up with the correct solution. I have a feeling that divs and mods are used in this somehow but I can not figure it out. If someone would like a good challenge, this is it hahah!

CodePudding user response:

I've written the code to YOUR specifications, AND shown what the grid looks like at each point in the process. It does not come out as your expected result...so either you haven't described the rules correctly, or you've got the "expected output" written incorrectly.

Here's my code:

import java.util.Scanner;
import java.text.DecimalFormat; 

class Main {
 
  public static void main(String[] args) {   
    Scanner sc = new Scanner(System.in);
    System.out.print("Size of grid? ");
    int size = sc.nextInt();
    if (size > 0) {
      int[][] grid = new int[size][size];  
      int max = (int)Math.pow(size, 2);
      int length = Integer.toString(max).length();      
      int col = size / 2;
      int row = 0;
      grid[row][col]=1;
      displayGrid(grid, length, row, col);
      for (int i=2; i<=max; i  ) {
        int newCol = (col 1) == size ? 0 : (col 1); // to the right, wrapping
        int newRow = (row-1) >= 0 ? (row-1) : size-1; // up, wrapping
        // if the new position is already taken or is the top right corner
        if (grid[newRow][newCol]!=0 || (newRow==0 && newCol==(size-1))) {
          // then move straight down, wrapping, instead
          newRow = (row 1) < size ? (row 1) : 0;
          newCol = col;
        }
        // put the current number in its spot and update row, col
        grid[newRow][newCol]=i;  
        row = newRow;
        col = newCol;
        displayGrid(grid, length, row, col);
      }
    }    
  }

  public static void displayGrid(int[][] grid, int padLength, int curRow, int curCol) {
    String format = "";
    for(int i=0; i<padLength; i  ) {
      format = format   "0";
    }
    DecimalFormat df = new DecimalFormat(format);
    for(int r=0; r<grid.length; r  ) {
      for(int c=0; c<grid[r].length; c  ) {
        System.out.print(df.format(grid[r][c]));
        System.out.print(r==curRow && c==curCol ? "*" : " ");
      }
      System.out.println();
    }
    System.out.println();
  }
  
}

Resulting output: Asterisk, *, is to the right of the number just placed!

Size of grid? 7
00 00 00 01*00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 

00 00 00 01 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 02*00 00 

00 00 00 01 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 03*00 
00 00 00 00 02 00 00 

00 00 00 01 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 04*
00 00 00 00 00 03 00 
00 00 00 00 02 00 00 

00 00 00 01 00 00 00 
00 00 00 00 00 00 00 
00 00 00 00 00 00 00 
05*00 00 00 00 00 00 
00 00 00 00 00 00 04 
00 00 00 00 00 03 00 
00 00 00 00 02 00 00 

00 00 00 01 00 00 00 
00 00 00 00 00 00 00 
00 06*00 00 00 00 00 
05 00 00 00 00 00 00 
00 00 00 00 00 00 04 
00 00 00 00 00 03 00 
00 00 00 00 02 00 00 

00 00 00 01 00 00 00 
00 00 07*00 00 00 00 
00 06 00 00 00 00 00 
05 00 00 00 00 00 00 
00 00 00 00 00 00 04 
00 00 00 00 00 03 00 
00 00 00 00 02 00 00 

00 00 00 01 00 00 00 
00 00 07 00 00 00 00 
00 06 08*00 00 00 00 
05 00 00 00 00 00 00 
00 00 00 00 00 00 04 
00 00 00 00 00 03 00 
00 00 00 00 02 00 00 

00 00 00 01 00 00 00 
00 00 07 09*00 00 00 
00 06 08 00 00 00 00 
05 00 00 00 00 00 00 
00 00 00 00 00 00 04 
00 00 00 00 00 03 00 
00 00 00 00 02 00 00 

00 00 00 01 10*00 00 
00 00 07 09 00 00 00 
00 06 08 00 00 00 00 
05 00 00 00 00 00 00 
00 00 00 00 00 00 04 
00 00 00 00 00 03 00 
00 00 00 00 02 00 00 

00 00 00 01 10 00 00 
00 00 07 09 00 00 00 
00 06 08 00 00 00 00 
05 00 00 00 00 00 00 
00 00 00 00 00 00 04 
00 00 00 00 00 03 00 
00 00 00 00 02 11*00 

00 00 00 01 10 00 00 
00 00 07 09 00 00 00 
00 06 08 00 00 00 00 
05 00 00 00 00 00 00 
00 00 00 00 00 00 04 
00 00 00 00 00 03 12*
00 00 00 00 02 11 00 

00 00 00 01 10 00 00 
00 00 07 09 00 00 00 
00 06 08 00 00 00 00 
05 00 00 00 00 00 00 
13*00 00 00 00 00 04 
00 00 00 00 00 03 12 
00 00 00 00 02 11 00 

00 00 00 01 10 00 00 
00 00 07 09 00 00 00 
00 06 08 00 00 00 00 
05 14*00 00 00 00 00 
13 00 00 00 00 00 04 
00 00 00 00 00 03 12 
00 00 00 00 02 11 00 

00 00 00 01 10 00 00 
00 00 07 09 00 00 00 
00 06 08 00 00 00 00 
05 14 00 00 00 00 00 
13 15*00 00 00 00 04 
00 00 00 00 00 03 12 
00 00 00 00 02 11 00 

00 00 00 01 10 00 00 
00 00 07 09 00 00 00 
00 06 08 00 00 00 00 
05 14 16*00 00 00 00 
13 15 00 00 00 00 04 
00 00 00 00 00 03 12 
00 00 00 00 02 11 00 

00 00 00 01 10 00 00 
00 00 07 09 00 00 00 
00 06 08 17*00 00 00 
05 14 16 00 00 00 00 
13 15 00 00 00 00 04 
00 00 00 00 00 03 12 
00 00 00 00 02 11 00 

00 00 00 01 10 00 00 
00 00 07 09 18*00 00 
00 06 08 17 00 00 00 
05 14 16 00 00 00 00 
13 15 00 00 00 00 04 
00 00 00 00 00 03 12 
00 00 00 00 02 11 00 

00 00 00 01 10 19*00 
00 00 07 09 18 00 00 
00 06 08 17 00 00 00 
05 14 16 00 00 00 00 
13 15 00 00 00 00 04 
00 00 00 00 00 03 12 
00 00 00 00 02 11 00 

00 00 00 01 10 19 00 
00 00 07 09 18 00 00 
00 06 08 17 00 00 00 
05 14 16 00 00 00 00 
13 15 00 00 00 00 04 
00 00 00 00 00 03 12 
00 00 00 00 02 11 20*

00 00 00 01 10 19 00 
00 00 07 09 18 00 00 
00 06 08 17 00 00 00 
05 14 16 00 00 00 00 
13 15 00 00 00 00 04 
21*00 00 00 00 03 12 
00 00 00 00 02 11 20 

00 00 00 01 10 19 00 
00 00 07 09 18 00 00 
00 06 08 17 00 00 00 
05 14 16 00 00 00 00 
13 15 00 00 00 00 04 
21 00 00 00 00 03 12 
22*00 00 00 02 11 20 

00 00 00 01 10 19 00 
00 00 07 09 18 00 00 
00 06 08 17 00 00 00 
05 14 16 00 00 00 00 
13 15 00 00 00 00 04 
21 23*00 00 00 03 12 
22 00 00 00 02 11 20 

00 00 00 01 10 19 00 
00 00 07 09 18 00 00 
00 06 08 17 00 00 00 
05 14 16 00 00 00 00 
13 15 24*00 00 00 04 
21 23 00 00 00 03 12 
22 00 00 00 02 11 20 

00 00 00 01 10 19 00 
00 00 07 09 18 00 00 
00 06 08 17 00 00 00 
05 14 16 25*00 00 00 
13 15 24 00 00 00 04 
21 23 00 00 00 03 12 
22 00 00 00 02 11 20 

00 00 00 01 10 19 00 
00 00 07 09 18 00 00 
00 06 08 17 26*00 00 
05 14 16 25 00 00 00 
13 15 24 00 00 00 04 
21 23 00 00 00 03 12 
22 00 00 00 02 11 20 

00 00 00 01 10 19 00 
00 00 07 09 18 27*00 
00 06 08 17 26 00 00 
05 14 16 25 00 00 00 
13 15 24 00 00 00 04 
21 23 00 00 00 03 12 
22 00 00 00 02 11 20 

00 00 00 01 10 19 00 
00 00 07 09 18 27 00 
00 06 08 17 26 28*00 
05 14 16 25 00 00 00 
13 15 24 00 00 00 04 
21 23 00 00 00 03 12 
22 00 00 00 02 11 20 

00 00 00 01 10 19 00 
00 00 07 09 18 27 29*
00 06 08 17 26 28 00 
05 14 16 25 00 00 00 
13 15 24 00 00 00 04 
21 23 00 00 00 03 12 
22 00 00 00 02 11 20 

30*00 00 01 10 19 00 
00 00 07 09 18 27 29 
00 06 08 17 26 28 00 
05 14 16 25 00 00 00 
13 15 24 00 00 00 04 
21 23 00 00 00 03 12 
22 00 00 00 02 11 20 

30 00 00 01 10 19 00 
00 00 07 09 18 27 29 
00 06 08 17 26 28 00 
05 14 16 25 00 00 00 
13 15 24 00 00 00 04 
21 23 00 00 00 03 12 
22 31*00 00 02 11 20 

30 00 00 01 10 19 00 
00 00 07 09 18 27 29 
00 06 08 17 26 28 00 
05 14 16 25 00 00 00 
13 15 24 00 00 00 04 
21 23 32*00 00 03 12 
22 31 00 00 02 11 20 

30 00 00 01 10 19 00 
00 00 07 09 18 27 29 
00 06 08 17 26 28 00 
05 14 16 25 00 00 00 
13 15 24 33*00 00 04 
21 23 32 00 00 03 12 
22 31 00 00 02 11 20 

30 00 00 01 10 19 00 
00 00 07 09 18 27 29 
00 06 08 17 26 28 00 
05 14 16 25 34*00 00 
13 15 24 33 00 00 04 
21 23 32 00 00 03 12 
22 31 00 00 02 11 20 

30 00 00 01 10 19 00 
00 00 07 09 18 27 29 
00 06 08 17 26 28 00 
05 14 16 25 34 00 00 
13 15 24 33 35*00 04 
21 23 32 00 00 03 12 
22 31 00 00 02 11 20 

30 00 00 01 10 19 00 
00 00 07 09 18 27 29 
00 06 08 17 26 28 00 
05 14 16 25 34 36*00 
13 15 24 33 35 00 04 
21 23 32 00 00 03 12 
22 31 00 00 02 11 20 

30 00 00 01 10 19 00 
00 00 07 09 18 27 29 
00 06 08 17 26 28 37*
05 14 16 25 34 36 00 
13 15 24 33 35 00 04 
21 23 32 00 00 03 12 
22 31 00 00 02 11 20 

30 00 00 01 10 19 00 
38*00 07 09 18 27 29 
00 06 08 17 26 28 37 
05 14 16 25 34 36 00 
13 15 24 33 35 00 04 
21 23 32 00 00 03 12 
22 31 00 00 02 11 20 

30 39*00 01 10 19 00 
38 00 07 09 18 27 29 
00 06 08 17 26 28 37 
05 14 16 25 34 36 00 
13 15 24 33 35 00 04 
21 23 32 00 00 03 12 
22 31 00 00 02 11 20 

30 39 00 01 10 19 00 
38 00 07 09 18 27 29 
00 06 08 17 26 28 37 
05 14 16 25 34 36 00 
13 15 24 33 35 00 04 
21 23 32 00 00 03 12 
22 31 40*00 02 11 20 

30 39 00 01 10 19 00 
38 00 07 09 18 27 29 
00 06 08 17 26 28 37 
05 14 16 25 34 36 00 
13 15 24 33 35 00 04 
21 23 32 41*00 03 12 
22 31 40 00 02 11 20 

30 39 00 01 10 19 00 
38 00 07 09 18 27 29 
00 06 08 17 26 28 37 
05 14 16 25 34 36 00 
13 15 24 33 35 00 04 
21 23 32 41 00 03 12 
22 31 40 42*02 11 20 

30 39 00 01 10 19 00 
38 00 07 09 18 27 29 
00 06 08 17 26 28 37 
05 14 16 25 34 36 00 
13 15 24 33 35 00 04 
21 23 32 41 43*03 12 
22 31 40 42 02 11 20 

30 39 00 01 10 19 00 
38 00 07 09 18 27 29 
00 06 08 17 26 28 37 
05 14 16 25 34 36 00 
13 15 24 33 35 44*04 
21 23 32 41 43 03 12 
22 31 40 42 02 11 20 

30 39 00 01 10 19 00 
38 00 07 09 18 27 29 
00 06 08 17 26 28 37 
05 14 16 25 34 36 45*
13 15 24 33 35 44 04 
21 23 32 41 43 03 12 
22 31 40 42 02 11 20 

30 39 00 01 10 19 00 
38 00 07 09 18 27 29 
46*06 08 17 26 28 37 
05 14 16 25 34 36 45 
13 15 24 33 35 44 04 
21 23 32 41 43 03 12 
22 31 40 42 02 11 20 

30 39 00 01 10 19 00 
38 47*07 09 18 27 29 
46 06 08 17 26 28 37 
05 14 16 25 34 36 45 
13 15 24 33 35 44 04 
21 23 32 41 43 03 12 
22 31 40 42 02 11 20 

30 39 48*01 10 19 00 
38 47 07 09 18 27 29 
46 06 08 17 26 28 37 
05 14 16 25 34 36 45 
13 15 24 33 35 44 04 
21 23 32 41 43 03 12 
22 31 40 42 02 11 20 

30 39 48 01 10 19 00 
38 47 49*09 18 27 29 
46 06 08 17 26 28 37 
05 14 16 25 34 36 45 
13 15 24 33 35 44 04 
21 23 32 41 43 03 12 
22 31 40 42 02 11 20 

Expected Output from Author's post:

30 39 48 01 10 19 28
38 47 07 09 18 27 29
46 06 08 17 26 35 37
05 14 16 25 34 36 45
13 15 24 33 42 44 04
21 23 32 41 43 03 12
22 31 40 49 02 11 20
  • Related