I am creating a magicsquare in java and have been given the following pseudocode:
> Create a 2 dimensional array of size n by n and set all values to be 0
Set x = 1, y = (n+1)/2 (row 1 and column (n+1)/2 ).
Insert 1 at x, y
  for i = 2 to n^2 do
   if element x − 1,y − 1 is empty (i.e. = 0) then
     x = x − 1, y = y − 1
   else
     x = x + 1, y = y
   end if
  Insert i at x, y
end for
My code is this:
class main1 {
    static void squarematrix(int n) {
        int[][] magicsquare = new int[n][n];
        
        int x = 1; //row
        int y = ((n+1)/2); //column
        magicsquare[x][y] = 1; //inserting 1 and (1, 5)
        for (int i = 2; i < (n*n); i++) {
            System.out.println(i);
            System.out.println(x);
            System.out.println(y);
            if (magicsquare[x-1][y-1] == 0) {
                x = x-1;
                y = y-1;
            } else {
                x = x+1;
                y = y;
            }
            magicsquare[x][y] = i;
        }
I understand that the x value goes lower than 0 giving me the out of bounds error, but I am honestly unsure how to fix it. Any help would be great thank you.
 
    