2

Hi I'm working on a simple L-game. I want to flip an array from left to right horizontally. For example,

{ 'x',' ',' ',' ' },

{ 'x',' ',' ',' ' },

{ 'x','x',' ',' ' },

{ ' ',' ',' ',' ' }

I want to flip it to

{ ' ','x',' ',' ' },

{ ' ','x',' ',' ' },

{ 'x','x',' ',' ' },

{ ' ',' ',' ',' ' }

And this is my current code

    public void flipArray() {
    int rows = cells.length;
    int cols = cells[0].length;
    char temp[][] = new char[rows][cols];
    for (int i = rows-1; i>=0; i--) {
        for (int j = cols-1; j>=0; j--) {
            temp[rows-1-i][cols-1-j] = cells[i][j];
        }
    }
    for (int i=0; i<rows; i++) {
        for (int j=0; j<cols; j++) {
            System.out.print(temp[i][j] + " ");
        }
    }
    }

Thank you so much any help is much appreciated. This is my desired outcome.



    rand_seed = 14427                                                       rand_seed = 14427
$ LGame.main({})                                                        $ LGame.main({})
A i i                                                                   A i i   
  o i                                                                     o i   
  o i                                                                     o i   
  o o B                                                                   o o B 
Move: o101                                                              Move: o101
A i i                                                                   A i i   
  o i                                                                |  o   i   
  o i                                                                |  o   i   
o o   B                                                                 o o   B 

11
  • 6
    is it really flipped? Commented Oct 27, 2013 at 14:30
  • Draw a diagram of your current output so we can compare with your desired output Commented Oct 27, 2013 at 14:31
  • Sorry but it doesn't seem to flip the way it should be. I'm not sure how to implement the correct flip. Commented Oct 27, 2013 at 14:33
  • @Batty.. If i am not wrong, since this is a L-game, the input is a 3x2 L shaped figure and the output is a flipped "L" (mirror image). @ author.. Am I correct? Commented Oct 27, 2013 at 14:35
  • The case you show do not flip your array it changes the positions of the elements. If it was a flip (2d flip) the first 3 x would be at last position of the array. So a better explanation would be aprecciated Commented Oct 27, 2013 at 14:35

1 Answer 1

2

Your code is so convoluted I'm not even trying to make sense of it. The problem gets a lot simpler and easier to grasp if you split it into sub problems.

First the basic building block, reversing a single array:

 static void flip(char[] array) {
     int left = 0;
     int right = array.length - 1;
     while (left < right) {
         char temp = array[left];
         array[left] = array[right];
         array[right] = temp;
         ++left;
         --right;
     }
 }

Now you can just walk your rows array and call flip for each row:

static void flip(char[][] rows) {
    for (char[] row : rows) {
        flip(row);
    } 
}

You see its pretty simple when you split it up into smaller problems.

Edit: Finding the bounding box of the "L" in the two-dimensional array can again be split into smaller problems. You can just walk the rows, check if they are completely empty and if they are not, find the min and max index where a cell is "set". For simplicity I do it in one method in two nested loops:

 static int[] getBoundingBox(char[][] rows) {
     int minY = Integer.MAX_VALUE;
     int maxY = Integer.MIN_VALUE;
     int minX = Integer.MAX_VALUE;
     int maxX = Integer.MIN_VALUE;
     for (int y=0; y<rows.length; ++y) {
         // find the rows min/max populated index
         char[] row = rows[y];
         int rowMinX = Integer.MAX_VALUE;
         int rowMaxX = Integer.MIN_VALUE;
         for (int x=0; x<row.length; ++x) {
             if (row[x] == 'x') {
                 rowMinX = Math.min(rowMinX, x);
                 rowMaxX = Math.max(rowMaxX, x);
             }                 
         }
         // check if the row is empty (min > max)
         if (rowMinX > rowMaxX) {
              // empty row, skip
              continue;
         }
         // update bounding box variables
         minY = Math.min(minY, y);
         maxY = Math.max(maxY, y);
         minX = Math.min(minX, rowMinX);
         maxX = Math.max(maxX, rowMaxX);
     }          
     // result is an array containing the bounds
     return new int[] { minX, minY, maxX, maxY };
}

You should really be able to connect the pieces now.

Edit2: All thats left to do is modify the flip(rows[][]) to take the bounds, and call flip(row[]) for just the rows in between minY and maxY. The row flip then needs to be passed the min/max X from the bounds and use the passed values instead of 0/length for left/right. Just think it through, it will be obvious if you think a little about it.

Off topic, but probably you're still in the process of learning that: The reason you split your code into small methods that solve only a sub-problem each is that you can a) re-use them to solve the same problem from a different place and b) the less code there is in a method the easier it is to verify what it does and if it works. Packing everything in one big method makes it harder to understand and when you need to solve part of the problem elsewhere you would duplicate code (and effort).

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you so much for this response. But wouldn't this just reverse the position of the array from say 0 to 3 for all rows? I would to locate where the curve of L shape is (where there is 2x of (x,x) in one row. Then just flip the L from left to right on the same position.
@user2741226 Thats a bit different from how I understood your question, in that case you will first need to detect the bounding box of the 'L'. That would be done in two passes, first find min(x, y) and max(x, y) coordinates of any cells that are "set". Then flip just within that rectangle. You could easily adapt the code I showed to limit the flipped rows and columns by passing additional parameters to the methods.
I added some more desired outcome to demonstrate what I wanted. Thank you so so much for your help. But I have no idea how to implement that method you suggested.
Downvoted because this in not an answer to OP's question. Please edit your answer to include the comment (which is really helpful, but was posted after I've downvoted).
@MichałRybak The OP asked one thing in words (which I answered) and another thing by the "sample" array he showed showed. So I answered what he asked for explicity. I ignored the implicit part, I'm fine with you downvoting for this reason, although I personally never attribute the ambiguity of the askers question as a negative point to the answerers answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.