因此我将代码更改为双数组而不是一个数组。但它一直在告诉我
房屋[]无法转换为众议院。而我似乎无法找到问题所在。我想要这个代码
作为双数组而不是一个数组。
package ghetto;
import java.math.*;
import ghetto.Houses;
import ghetto.House;
import ghetto.Marker;
import ghetto.HouseType;
import ghetto.Grids;
import ghetto.Ghetto;
public class Houses {
public int size;
public House[][] houses;
public Houses(int size, int blue, int red) {
if (size == 0)
return;
this.houses = new House[size][size];
House.size = size;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++)
this.houses[i][j] = new House(i, j, HouseType.Vacant);
}
// Create Blue
if (blue != 0) {
for (int totalBlue = 0; totalBlue < blue;) {
int choiceRow = (int) (Math.random() * size);
int choiceCol = (int) (Math.random() * size);
if (this.houses[choiceRow][choiceCol].who == HouseType.Vacant) {
this.houses[choiceRow][choiceCol].who = HouseType.Blue;
System.out.println("Blue house at " + choiceRow + " "
+ choiceCol);
totalBlue = totalBlue + 1;
}
}
}
// Create Red
if (red != 0) {
for (int totalRed = 0; totalRed < red;) {
int choiceRow = (int) (Math.random() * size);
int choiceCol = (int) (Math.random() * size);
if (this.houses[choiceRow][choiceCol].who == HouseType.Vacant) {
this.houses[choiceRow][choiceCol].who = HouseType.Red;
System.out.println("Red house at " + choiceRow + " "
+ choiceCol);
totalRed = totalRed + 1;
}
}
}
}
public HouseType getHouseType(int row, int col) {
System.out.println("house[" + row + "][" + col + "]= "
+ this.houses[row][col].who);
return this.houses[row][col].who;
// return HouseType.Red;
}
public void step() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
// Who are my neighbors
House[][] ns = neighbors(houses[i][j]);
// How many are red, blue
int countRed = 0;
int countBlue = 0;
for (int n = 0; n < 8; n++) {
if (ns[j][j].who == HouseType.Red) {
countRed = countRed + 1;
}
if (ns[j][j].who == HouseType.Blue) {
countBlue = countBlue + 1;
}
}
// Decide to stay or move
if (houses[i][j].decide(countRed, countBlue)) {
houses[i][j].move(ns);
}
}
}
}
public House[][] neighbors(House victim) {
House[][] n = new House[2][4];
int row = victim.address / size;
int col = victim.address % size;
if (row != 0 && row != (size - 1) && col != 0 && col != (size - 1)) {
n[0][0] = houses[victim.address - 1];
n[0][1] = houses[victim.address + 1];
n[0][2] = houses[victim.address - size];
n[0][3] = houses[victim.address - size - 1];
n[0][4] = houses[victim.address - size + 1];
n[0][5] = houses[victim.address + size];
n[0][6] = houses[victim.address + size - 1];
n[0][7] = houses[victim.address + size + 1];
return n;
}
if (row == 0 && col != 0 && col != (size - 1)) {
n[0] = houses[victim.address - 1];
n[1] = houses[victim.address + 1];
n[2] = houses[victim.address + size];
n[3] = houses[victim.address + size - 1];
n[4] = houses[victim.address + size + 1];
n[5] = houses[victim.address + (size * (size - 1))];
n[6] = houses[victim.address + (size * (size - 1)) + 1];
n[7] = houses[victim.address + (size * (size - 1)) - 1];
return n;
}
if (row == (size - 1) && col != 0 && col != (size - 1)) {
n[0] = houses[victim.address - 1];
n[1] = houses[victim.address + 1];
n[2] = houses[victim.address - (size * (size - 1))];
n[3] = houses[victim.address - (size * (size - 1)) + 1];
n[4] = houses[victim.address - (size * (size - 1)) - 1];
n[5] = houses[victim.address - size];
n[6] = houses[victim.address - size - 1];
n[7] = houses[victim.address - size + 1];
return n;
}
if (col == 0) {
n[0] = houses[victim.address + (size - 1)];
n[1] = houses[victim.address + 1];
n[2] = houses[victim.address - size];
n[3] = houses[victim.address - size - 1];
n[4] = houses[victim.address - size + 1];
n[5] = houses[victim.address - (size * (size - 1))];
n[6] = houses[victim.address - (size * (size - 1)) + 1];
n[7] = houses[victim.address - (size * (size - 1)) - 1];
return n;
}
if (col == (size - 1)) {
n[0] = houses[victim.address - 1];
n[1] = houses[victim.address + 1];
n[2] = houses[victim.address - size];
n[3] = houses[victim.address - size - 1];
n[4] = houses[victim.address - size + 1];
n[5] = houses[victim.address - (size * (size - 1))];
n[6] = houses[victim.address - (size * (size - 1)) + 1];
n[7] = houses[victim.address - (size * (size - 1)) - 1];
return n;
}
if (col == (size + 1)) {
n[0] = houses[victim.address - 1];
n[1] = houses[victim.address + 1];
n[2] = houses[victim.address - (size * (size - 1))];
n[3] = houses[victim.address - (size * (size - 1)) + 1];
n[4] = houses[victim.address - (size * (size - 1)) - 1];
n[5] = houses[victim.address - size];
n[6] = houses[victim.address - size - 1];
n[7] = houses[victim.address - size + 1];
return n;
}
return n;
}
}
答案 0 :(得分:1)
在neighbors(House victim)
方法中,您尝试将Houses []分配给House对象
n[0][0] = houses[victim.address - 1]
&lt; == n [0] [0]表示House对象,而house [victim.address-1]表示数组。您必须从House
访问houses[][]
对象我不明白您的逻辑,但您可以尝试这样的事情
if (row != 0 && row != (size - 1) && col != 0 && col != (size - 1)) {
n[0][0] = houses[victim.address - 1][victim.j];
n[0][1] = houses[victim.address + 1][victim.j];
n[0][2] = houses[victim.address - size][victim.j];
n[0][3] = houses[victim.address - size - 1][victim.j];
n[0][4] = houses[victim.address - size + 1][victim.j];
n[0][5] = houses[victim.address + size][victim.j];
n[0][6] = houses[victim.address + size - 1][victim.j];
n[0][7] = houses[victim.address + size + 1][victim.j];
return n;
}
注意:正如我所提到的,我不了解您的逻辑,因此您可能需要决定victim(which is a House Object)
哪个属性要访问houses[row][column]
的列值
答案 1 :(得分:0)
错误发生在邻居方法的这些行上:
n[0][0] = houses[victim.address - 1];
n[0][1] = houses[victim.address + 1];
n[0][2] = houses[victim.address - size];
n[0][3] = houses[victim.address - size - 1];
n[0][4] = houses[victim.address - size + 1];
n[0][5] = houses[victim.address + size];
n[0][6] = houses[victim.address + size - 1];
n[0][7] = houses[victim.address + size + 1];
(其中houses
和n
都声明为House[][]
)。
在每个作业的左侧,您有一个House
。在每个人的右侧,你有一个House[]
。您不能将数组分配给标量。您应该删除左侧的[]
索引之一,或者在右侧添加另一个以平衡它。我无法分辨哪些因为我不知道你要做什么。
此外,在Houses
构造函数中,行House.size = size;
看起来很可疑。它分配给House
的静态字段,而忽略Houses
的实例字段。我想你想要this.size = size;
。