我刚刚开始学习Java,我正在做一个小项目,应该在二维字符串数组中创建一个gameField字段边界标记为"#"。在这个字段中,我想生成一个标记为">"的球员。这个玩家可以向左或向右转(例如向上看),然后可以选择向前移动一个空间。我目前已经创建了gameField,现在想要生成玩家到二维字符串数组中,我已经在类player中创建了一个spawnPlayer方法/函数。现在我想在主方法中添加/打开该方法/函数。这就是我得到错误消息,不知道该做什么。
public class KonsolenWanderer {
public static void main(String[] args) {
Field field1 = new Field();
field1.createField();
Player player1 = new Player();
//Error is here!!!
player1.spawnPlayer();
}
}
public class Field {
private String [][] fieldSize = new String [10][10];
public void createField() {
for(int i = 0; i < fieldSize.length; i++) {
for(int j = 0; j < fieldSize[i].length; j++) {
//Creating Field Border
if(i == 0 || i == 9 || j == 0 || j == 9) {
fieldSize[i][j] = "#";
}
else {
fieldSize[i][j] = " ";
}
System.out.print(fieldSize[i][j]);
}
System.out.println();
}
}
public String[][] getFieldSize() {
return fieldSize;
}
public void setFieldSize(String[][] fieldSize) {
this.fieldSize = fieldSize;
}
}
public class Player {
private static int xPosition;
private static int yPosition;
private String up = "∧";
private String down = "∨";
private String left = "<";
private String right = ">";
private String currentDirection;
Player() {
xPosition = 4;
yPosition = 4;
}
public void spawnPlayer(String[][]fieldSize) {
fieldSize[xPosition][yPosition] = ">";
}
public static void moveForward() {
}
public static void turnPlayerLeft() {
}
public static void turnPlayerRight() {
}
}
您需要设置字段对象内部的位置或从字段类中暴露fieldSize。
public class KonsolenWanderer {
public static void main(String[] args) {
Field field = new Field();
field.createField();
Player player1 = new Player();
//Error is here!!!
player1.spawnPlayer(new String[8][8], field);
field.showField();
}
}
class Field {
private String [][] fieldSize = new String [10][10];
public void createField() {
for(int i = 0; i < fieldSize.length; i++) {
for(int j = 0; j < fieldSize[i].length; j++) {
//Creating Field Border
if(i == 0 || i == 9 || j == 0 || j == 9) {
fieldSize[i][j] = "#";
}
else {
fieldSize[i][j] = " ";
}
System.out.print(fieldSize[i][j]);
}
System.out.println();
}
}
public void showField() {
for(int i = 0; i < fieldSize.length; i++) {
for(int j = 0; j < fieldSize[i].length; j++) {
System.out.print(fieldSize[i][j]);
}
System.out.println();
}
}
public String[][] getFieldSize() {
return fieldSize;
}
public void setFieldSize(String[][] fieldSize) {
this.fieldSize = fieldSize;
}
public void setPlayerPosition(int i, int j) {
i++;
j++;
fieldSize[i][j] = ">";
}
}
class Player {
private static int xPosition;
private static int yPosition;
private String up = "∧";
private String down = "∨";
private String left = "<";
private String right = ">";
private String currentDirection;
Player() {
xPosition = 4;
yPosition = 4;
}
public void spawnPlayer(String[][] fieldSize, Field field) {
for(int i = 0; i < fieldSize.length; i++) {
if (i == fieldSize.length -1){
for(int j = 0; j < fieldSize[i].length; j++) {
if (j == fieldSize.length - 1 )
field.setPlayerPosition(i, j);
}
}
}
}
public static void moveForward() {
}
public static void turnPlayerLeft() {
}
public static void turnPlayerRight() {
}
}