好的我制作了2D地图阵列。它里面有狐狸和野兔。他们可以四处走动并死亡。我将狐狸和兔子存放在一个数组列表中,以便跟踪剩下的数量。我的问题是我如何能够改变狐狸或兔子在阵列上的位置。每一次狐狸和野兔都必须移动一个点与他们以前的地点相邻。
继承人到目前为止我的代码:如何更改2D阵列中物体的位置
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Map {
protected int numberOfRows; // number of rows in my map
protected int numberOfColumns; // number of columns in my map
protected int randomRow;
protected int randomColumn;
protected ArrayList<Fox> fox = new ArrayList<Fox>();
protected ArrayList<Hare> hare = new ArrayList<Hare>();
Animal map [][]; // creates an instance of my map
public Map(int rows, int columns) { // creates a constructor
numberOfRows = rows;
numberOfColumns = columns;
map= new Animal[rows][columns]; // puts my number of rows and columns in array
for(int i = 0; i < rows; i++) {
for(int k = 0; k < columns; k++) {
map[i][k] = new Animal(); // feels my map with null
}
}
generateFoxAndHares();
}
public void print() {
// nested for loop to print my map
for(int i =0; i < numberOfRows; i++) {
for(int j = 0; j < numberOfColumns; j++) {
System.out.print(map[i][j].getDisplayChar());
}
System.out.println("");
}
}
public void generateFoxAndHares() {
int ranRow; // random row for hare or fox to go
int ranCol; // random column for hare or fox to go
Scanner myScanner = new Scanner(System.in); // take user input
int counter = 1; // counts how many times my while loop runs
Random r = new Random();
for(int i = 0; i < 15; i++) { // puts foxs on my map
ranRow = r.nextInt(this.numberOfRows);
ranCol = r.nextInt(this.numberOfColumns);
fox.add(new Fox(ranRow, ranCol));
map[ranRow][ranCol] = fox.get(i);
}
for(int i = 0; i < 15; i++) { // puts hares on map
ranRow = r.nextInt(this.numberOfRows);
ranCol = r.nextInt(this.numberOfColumns);
hare.add(new Hare(ranRow, ranCol));
map[ranRow][ranCol] = hare.get(i);
}
print();
System.out.println("Day: 0, Foxs: " + fox.size() + ", Hares: " + hare.size());
while(hare.size() > 0 || fox.size() > 0) { // keeps running
System.out.println("Press any button to continue");
String textString;
textString = myScanner.nextLine();
print();
System.out.println("Day: " + counter + ", Foxs: " + fox.size() + ", Hares: " + hare.size());
counter++;
}
}
}
public class Animal {
protected char displayChar;
protected int xCord;
protected int yCord;
public char getDisplayChar() {
return displayChar;
}
public char getSymbol() {
return getDisplayChar();
}
public int getxCord() {
return xCord;
}
public int getyCord() {
return yCord;
}
}
public class Fox extends Animal{
public Fox(int x, int y) {
this.xCord = x;
this.yCord = y;
this.displayChar = 'F';
}
}
public class Hare extends Animal{
public Hare(int x, int y) {
this.xCord = x;
this.yCord = y;
this.displayChar = 'H';
}
}
import java.util.ArrayList;
public class FoxAndHaresMain {
public static void main(String[] args) {
Map map1 = new Map(20,20);
map1.generateFoxAndHares();
}
}
我的继承人输出:
FF H H
F H F
HF
F H F
F H F
F F
F HH H
H H
HF HH F
H H F
Day: 0, Hares: 15, Foxes: 15
看起来你的动物是不可变的,所以你应该根据他们的新位置重新创建它们。你需要找到一个你想让他们移动的算法。 (在4个可能的方向上是随机的?边界怎么样?) –
为什么动物存储坐标? (只是问,似乎他们不是真的需要)。假设它们不在被占领的领域上有效吗? (一只狐狸在被野兔占领的田野上移动是有道理的......其他的......不是那么多) – Marco13
等等,你为什么用(ranRow,ranCol)制作狐狸和野兔?在地图上?在地图上的坐标总是只有(0,0)的坐标。 –