2

I'm making a project on a L_Game, now i'm stuck on the move method where my code is:

public void move(int row, int col) {
        char [][] temp= new char [cells.length][]; 
        for (int i= 0; i< cells.length; i++) {
            int destRow = (i+row)%cells.length;
            temp[destRow] = new char [cells[i].length];
            for (int j= 0; j < cells[i].length; j++)
                temp[destRow][(j+col)%cells[i].length] = cells[i][j];
            }
              cells= temp;
        }

My move method doesn't seem to be moving the objects correctly..

So output is suppose to be like the left side, the right side is the output from my code. I know i'm obviously not moving it correctly but i don't know what i'm doing wrong either...

    $ slide.move(0,2)                             $ slide.move(0,2)
    $ slide.cells -> {                            $ slide.cells -> {
    { , ,o, },                                 |  { , , , },
    { , ,o, },                                 |  { , , ,o},
    { , ,o,o},                                 |  { , , ,o},
    { , , , }                                  |  {o, , ,o}
    }                                             }
    $ slide.move(1,2)                             $ slide.move(1,2)
    $ slide.cells -> {                            $ slide.cells -> {
                                                > { ,o,o, },
    { , , , },                                    { , , , },
    { , ,o, },                                 |  { ,o, , },
    { , ,o, },                                 |  { ,o, , }
    { , ,o,o}                                  <
    }                                             }
  • The '|' symbol identifies lines that are different.
  • The '<' symbol points to lines in the left column that are not in the right column.
  • The '>' symbol points to lines in the right column that are not in the left column.

Any idea on how i can fix my move method to get it to move correctly?

Thanks

11
  • What arguments in .move(1,2) mean? Commented Oct 25, 2013 at 22:56
  • @pshemo, the row and col? Commented Oct 25, 2013 at 23:00
  • @tbodt, link added to it. Short wiki page. Commented Oct 25, 2013 at 23:00
  • row and col of what? Is it vector you want to use while moving your L shape? Or is it place you want to move some piece of L shape? If so which piece? Is it top-left piece? You need to be more precise what you are trying to do here. Commented Oct 25, 2013 at 23:16
  • @Pshemo, ah yes thats why i'm not too quite sure either. The instructions on this isn't very clear (University Assignment Task) But i believe it is your 2nd suggestions where i want to move and based on the top-left piece "o". Commented Oct 25, 2013 at 23:22

2 Answers 2

1

Seems like you're moving the L relative to its last position, but in your left example row and col specify the absolute position of the top 'o'.

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

Comments

0

Going off of grexter89's answer, presuming that move(r,c) is supposed to place L at that position (rather than move the L by the specified amount -- correct me if I'm wrong):

Your program is behaving exactly as you told it to behave:

  • Move current state, storing the result in a new temporary state.
  • Set current state to the new moved state.

Your moves are incremental, because that's the program you wrote. For these types of things, think of it like a "translation": Express what you want to do in English (or whatever) then translate it to your code. If the above two steps aren't what you want to do, then you started off with the wrong idea.

It seems like what you really want to do is this. First you define an initial state where the L is in the top left corner, and when you start you set the current state to that initial state. Then move does this:

  • Move initial state, storing the result in a new temporary state.
  • Set current state to new moved state.

See the difference? With the above steps, every move starts at the initial state instead of incrementally being applied to the current state.

Of course, you could simplify the above a bit (you don't need a temporary state, really) but I'm trying to keep the example straightforward.

Your code is fundamentally correct in that it does move a grid correctly, you're just not applying it to the grid (current state) that you intended to apply it to (initial state).

Hopefully that is enough to get you going here.

Comments

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.