2014-03-11 3 views
0

Мы пытаемся создать форму и поместить ее на 2d консольную плату. Формы будут состоять из нескольких точек в массиве 2d. Таким образом, треугольник, например, будет выглядеть, как это вход 4x3x3 пользователя ...Как создать/перемещать сразу несколько объектов в 2-мерном массиве?

1 
1 1 1 
1 1 1 1 

Форма будет иметь возможность двигаться и расти/сокращаться. У нас есть формы, способные отображать их размеры уже, а также сделанную плату. Но фактически положить их на доску и перемещать их (все точки в целом) оказывается сложным. Какие-либо предложения? Вот наш код до сих пор ...

код совета ...

public class Board { 

private int size; 

public Board(int boardSize){ 
    this.size = boardSize; 
} 

public String toString() { 

    Playable[][] grid = new Playable [getSize()][getSize()]; 

    int k = 1; 
    while (k <= (grid.length+2)) { 
     System.out.print('-'); 
     k++; 
    } 

    System.out.println(); 

    for (Playable[] row : grid) { 
     System.out.print("|"); 
     for (Playable item : row) { 
      System.out.print((item == null ? " " : item)); 
     } System.out.print("| \n"); 
    } 

    k = 1; 
    while (k <= (grid.length+2)) { 
     System.out.print('-'); 
     k++; 
    } 
    return ""; 
} 

public int getSize() { 
    return size; 
} 

public void setSize(int size) { 
    this.size = size; 
} 
} 

Форма Код ...

public class Rectangle extends Quads implements Playable { 

public Rectangle(int numberOfSides, int numberOfDimensions) { 
    super(4, 2); 
    // TODO Auto-generated constructor stub 
} 

public double calcPerimeter() { 
    return ((this.getDimensions()[0] + this.getDimensions()[1]) * 2); 
} 

public double calcArea() { 
    double area; 
    area = this.getDimensions()[0] * this.getDimensions()[1]; 
    return area; 
} 

public String showDimensions() { 
    String display = ""; 
    display += "For this " + this.getColor() + " " 
      + this.getClass().getSimpleName() + ": \n"; 

    display += this.getDIMENSION_LABELS()[0] + ": " 
      + this.getDimensions()[0] + "\n"; 
    display += this.getDIMENSION_LABELS()[1] + ": " 
      + this.getDimensions()[1] + "\n"; 
    display += this.getDIMENSION_LABELS()[2] + ": " 
      + this.getDimensions()[0] + "\n"; 
    display += this.getDIMENSION_LABELS()[3] + ": " 
      + this.getDimensions()[1] + "\n"; 
    display += "The perimeter is " + this.calcPerimeter() + ", \n"; 
    display += "The area is " + this.calcArea() + ", \n"; 
    display += "And the seniority is " + this.getSeniority() + "\n"; 
    return display; 
} 

ответ

0

Вы определяете новую сетку каждый раз, когда вы печатаете его сделать Playable[][] grid в поле поэтому вы можете сохранить состояние, а затем сделать это

grid[0][0] = new Playable(){...};//create 
grid[1][0] = grid[0][0];//copy to new location 
grid[0][0] = null; //remove from old location 
+0

Что бы вы ожидали от созданного тела? Я получаю то, что вы здесь делаете, но не уверен, что именно вы ожидаете от меня. – Stevo

+0

Я сделал Playable анонимным, чтобы вы знали, что должна быть какая-то реализация. У вас уже есть Rectangle и Quads, поэтому я думаю, что вы ожидаете большего количества реализаций этого интерфейса. – Bitman

Смежные вопросы