0
public class MagicSquare
{
    public static int[][] grid = new int[3][3];
    public static int i = 0;
    public static int j = 0;

    public static void main(String[] args) {

       int x = 1;
       int y = 2;
       int z = 0;

       while(z < 9)
       {
             int holdx = x;
             int holdy = y;
             z++;
             x++;
             y--;

       if(x == 3)
       {
             x = 0;
       }

       if(y == -1)
       {
             y = 2;
       }

       if(y == 3)
       {
             y = 0;
       }

       if(grid[x][y] == 0)
       {
            grid[x][y] = z;
       }

       else
       {
            holdy++;

            if(holdy == 3)
            {
                 holdy = 0;
            }

            grid[holdx][holdy] = z;
            x = holdx;
            y = holdy;
       }
       }       

       for(int i = 0; i < 3; i++)
       {
            System.out.print(grid[i][0]+", ");
       }

       System.out.println(" ");

       for(int i = 0; i < 3; i++)
       {
            System.out.print(grid[i][1]+", ");
       }

       System.out.println(" ");

       for(int i = 0; i < 3; i++)
       {
            System.out.print(grid[i][2]+", ");
       }
}

THE OUTPUT LOOKS LIKE THIS:

2, 4, 9,
6, 8, 1,
7, 3, 5,

Hello, I wrote a Black Magic code that is able to fill in the grids of the square up and the right of it, but if it is filled with a number then the next number would be put in the square that is below its current spot.

Then, go one square up and to the right and put the next integer there, and if I also go off the grid, then the next number will wrap around to the bottom and/or left. This program run until all the squares are filled.

I was wondering if it is possible to condense my code starting at my while loop to the end of my for loop into a shorter code?

Someone said that I would be able to code this USING JUST 2 lines, and I think that is bizarre... but they said it's doable!

Any hints, help, or pointer would be appreciated!

Thank you so much!

  • What do x,y, and z represent? – OneCricketeer Nov 11 '16 at 22:11
  • Tell him that you did it in one line `System.out.println("2, 4, 9," + System.lineSeparator() + "6, 8, 1," + System.lineSeparator() + "7, 3, 5,");`. :) You can start by removing the useless static variables, then by using a new `for` loop for the printing: `for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { System.out.print(grid[i][j] + ", "); } System.out.println(); }`. – ROMANIA_engineer Nov 11 '16 at 22:26
  • *I was wondering if it is possible to condense my code* -- This sounds like you could use someone to [review your working code](http://codereview.stackexchange.com/tour) – OneCricketeer Nov 11 '16 at 22:29
  • @ROMANIA haha, if only – sushiprograms Nov 11 '16 at 22:50
  • @cricket_007 thanks for the heads up, but i don't post much on here so i wouldn't know – sushiprograms Nov 11 '16 at 22:50
  • No worries. Regarding the "two-line" comment. I have no idea. But, I really don't think you would be able to validate that you actually did make a magic square in two lines. Like I have [an answer](http://stackoverflow.com/a/33625476/2308683) that *tests for* a magic square, and that seemed condense, but validation while generating seems different. – OneCricketeer Nov 11 '16 at 22:55

2 Answers2

2

Not sure about less than 3 lines (at least without sacrificing readability), but you can condense these if statements for sure.

if(x == 3) {
     x = 0;
}

if(y == -1) {
     y = 2;
}

if(y == 3) {
     y = 0;
}

Down into simply

x = x % 3;
y = (y + 3) % 3;

And you could pull those into the previous x++ and y--

x = (x + 1) % 3;
y = ((y - 1) + 3) % 3;

Similarly with holdy (if you actually need that value, I do not know).

Then, if you just want to print the array, the for loops can be shortened.

for(int[] row : grid) {
    System.out.println(Arrays.toString(row));
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

About 10 years ago, I wrote the following code for computing the entries of a magic square. This, in some sense, boils down to a single line of code, and works for arbitrary odd edge lengths:

class M
{
    public static void main(String args[])
    {
        int n = 5;
        int a[][] = new int[n][n];
        f(n,1,n/2,n-1,a);
        print(a);
    }

    static int f(int j,int i,int k,int l,int I[][])
    {
        return i>j*j?i-1:(I[k][l]=f(j,i+1,(k+(i%j==0?0:1))%j,(l+(i%j==0?-1:1))%j,I))-1;
    }

    public static void print(int a[][])
    {
        for (int i=0; i<a.length; i++)
        {
            for (int j=0; j<a[i].length; j++)
            {
                System.out.print((a[j][i]<10?" ":"")+a[j][i]+" ");
            }
            System.out.println();
        }
    }
}

Of course, it is somehow inspired by the IOCCC and would be better suited for Programming Puzzles & Code Golf, but might show how much you can press into a single line of code when you (ab)use the ternary operator and recursion appropriately...

Community
  • 1
  • 1
Marco13
  • 53,703
  • 9
  • 80
  • 159
  • @cricket_007 Sure, this should not be taken too serious. (In fact, originally I used variable names `i`, `ì`, `í` and `î`, to make it *entirely* unreadable ;-)). But the question about what is "doable" when "condensing" code and the questionable concept of "lines of code" may be a reason to show the odd outcomes when going to far with this. The bottom line should probably only be **readability is more important than compactness**. – Marco13 Nov 12 '16 at 01:27