2017-04-23 41 views
-4
int movedistance = 5; // Distance to move the board 

@Override 
public byte getLive(int x, int y) { 
    return board[y][x]; // Arraylist board.get(y).get(x) ? 
} 

public void patternRight(){ 
     byte[][] testBoard = new byte[getHeight()][getWidth()]; 
     // testBoard = new Arraylist<> ? 
     for (int x = 0; x < getHeight(); x++) { 
      for (int y = 0; y < getWidth(); y++){ 
       if (getLive(y, x) == 1) testBoard[x][y + movedistance] = 1; 
      } 
     } 
} 

我正在尝试使我的游戏模式在我的棋盘(人生游戏)上移动。此移动方法我目前已与字节[]。我想要做与ArrayList完全相同的方法。如何将我的byte []方法转换为arraylist []方法?

+1

欢迎来到Stack Overflow!请[参观](http://stackoverflow.com/tour)以查看网站的工作原理和问题,并相应地编辑您的问题。另请参阅:[为什么“有人可以帮我吗?”不是一个实际的问题?](http://meta.stackoverflow.com/q/284236) –

+2

为什么你想用'ArrayList'来解决什么? –

回答

0

List<List<Byte>>代替byte[][]没什么意义,但是这里是如何去做的。

不过,首先你的代码是与数组索引的顺序不一致的:声明为x,y

  • getLive()参数,但你if (getLive(y, x) == 1)调用它。
  • getLive(),您使用board[y][x],但你用testBoard[x][y + movedistance] = 1;
  • 不过也可以使用getHeight()x,并getWidth()y,所以也许这(不小心?) “增加了”。

我将假设方法应该总是x,y,阵列应[y][x],那x是“宽度”和y为“高度”。

此外,[y + movedistance]将导致ArrayIndexOutOfBoundsException,因为您的循环使用全范围的值。我会假设你想在溢出时“环绕”。

public byte getLive(int x, int y) { 
    return board.get(y).get(x); 
} 

public void patternRight(){ 
    List<List<Byte>> testBoard = new ArrayList<>(); 
    for (int y = 0; y < getHeight(); y++) { 
     List<Byte> row = new ArrayList<>(); 
     for (int x = 0; x < getWidth(); x++) 
      row.add((byte) 0); 
     testBoard.add(row); 
    } 
    for (int x = 0; x < getWidth(); x++) { 
     for (int y = 0; y < getHeight(); y++) { 
      if (getLive(x, y) == 1) 
       testBoard.get((y + movedistance) % getHeight()).set(x, (byte) 1); 
     } 
    } 
} 
相关问题