2

我正在L_Game上制作一个项目,现在我被困在我的代码所在的 move 方法上:

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;
        }

我的移动方法似乎没有正确移动对象..

所以输出应该和左边一样,右边是我的代码的输出。我知道我显然没有正确移动它,但我也不知道我做错了什么......

    $ 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}                                  <
    }                                             }
  • '|' 符号标识不同的行。
  • '<' 符号指向左列中不在右列中的行。
  • '>' 符号指向右列中不在左列中的行。

关于如何修复我的移动方法以使其正确移动的任何想法?

谢谢

4

2 回答 2

1

似乎您正在相对于其最后一个位置移动 L,但在您的左侧示例中rowcol指定顶部“o”的绝对位置。

于 2013-10-25T22:58:50.380 回答
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.

于 2013-10-26T18:36:30.130 回答