我们正在为第二个semeseter java的东西做一个小项目,并试图修复一个错误,现在就像1小时。我们做不到。我们的问题如下:
这就是我们的功能:
/**
* Calculates a standard Start area as seen in the task definition dependend on the number of
* figures we want according to the number of players
* @return list of cells
*/
public ArrayList<Cell> calculateArea(int modifier) {
ArrayList<Cell> cells = new ArrayList<Cell>();
for (int x = 0; x <= 4 - modifier; x++) {
for (int y = 0; y <= 4 - modifier; y++) {
if (y <= 1){
cells.add(new Cell(x, y));
}
else if (y == 2) {
if(x <= 3 - modifier) {
cells.add(new Cell(x, y));
}
else {
break;
}
}
else if (y == 3) {
if (x <= 2 - modifier) {
cells.add(new Cell(x, y));
}
else {
break;
}
}
else if (y == 4) {
if(x <= 1) {
cells.add(new Cell(x, y));
}
else {
break;
}
}
}
}
return cells;
}
这将输出一组单元格,这些单元格被认为是左上角玩家的起始区域(方板)。
之后我们尝试根据电路板尺寸以及当前播放器正在播放的位置旋转计算的起始区域:
/**
* Rotates a given area of Cells to the right according to board size
* is used to calculate Starting and Target areas for each player
* @param area
* @return
*/
private ArrayList<Cell> rotateRight(ArrayList<Cell> area, int modifier) {
for(Cell c : area) {
if(modifier == 0) {
// 19 Figures
if(c.getY() <= 1) {
c.setX(c.getX() + Board.getSize() - 5);
}
else if(c.getY() == 2) {
c.setX(c.getX() + Board.getSize() - 4);
}
else if(c.getY() == 3) {
c.setX(c.getX() + Board.getSize() - 3);
}
else {
c.setX(c.getX() + Board.getSize() - 2);
}
}
else {
// 13 Figures
if(c.getY() <= 1) {
c.setX(c.getX() + Board.getSize() - 4);
}
else if(c.getY() == 2) {
c.setX(c.getX() + Board.getSize() - 3);
}
else if(c.getY() == 3) {
c.setX(c.getX() + Board.getSize() - 2);
}
}
}
return area;
}
/**
* Rotates a given area of Cells down according to board size
* is used to calculate Starting and Target areas for each player
* @param area
* @return
*/
private ArrayList<Cell> rotateDown(ArrayList<Cell> area, int modifier) {
for(Cell c : area) {
if(modifier == 0) {
// 19 Figures
if(c.getX() <= 1) {
c.setY(c.getY() + Board.getSize() - 5);
}
else if(c.getX() == 2) {
c.setY(c.getY() + Board.getSize() - 4);
}
else if(c.getX() == 3) {
c.setY(c.getY() + Board.getSize() - 3);
}
else {
c.setY(c.getY() + Board.getSize() - 2);
}
}
else {
// 13 Figures
if(c.getX() <= 1) {
c.setY(c.getY() + Board.getSize() - 4);
}
else if(c.getX() == 2) {
c.setY(c.getY() + Board.getSize() - 3);
}
else if(c.getX() == 3) {
c.setY(c.getY() + Board.getSize() - 2);
}
}
}
return area;
}
有两个问题: - 似乎每个玩家的所有位置(开始和目标)共享相同的值。这意味着每个播放器中的所有arrayLists都链接到堆上的同一个对象。我想我们必须在每次实际旋转之前以某种方式深度复制ArrayList?
非常感谢!