我如何同时在数组中添加不同的对象并为下一步移动存储它们?



我正在学习Java并为期末考试做游戏项目。我试图添加不同的值到我的数组,但当我打印数组,每次它在地图上显示不同的对象,我将在这里共享输出。这是主类:

import MAP.*;
import Person.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Map map = new Map();
Scanner sc = new Scanner(System.in);
Person character = new Person("ahmet");
Person npc = new Person("mehmet");
npc.placeObject();
character.setPosX(character.random1);
character.setPosY(character.random2);
character.placeCharacter(character.posX, character.posY);
System.out.println(character.name);
for (int i = 0 ; i <5 ;i++) {
String a = sc.nextLine();
character.MoveCharacter(a);
}
System.out.println("n");
}
}

这是我的Person类:

import MAP.Map;
import java.util.Random;
public class Person extends Map {
public int posX;
public int posY;

public int getPosX(Random rnd){
return posX;
}
public int getPosY(){
return posY;
}
public void setPosX(int posX){
this.posX=posX;
System.out.println(posX);
}
public void setPosY(int posY){
this.posY=posY;
System.out.println(posY);
}
public Person(String name) {
this.name = name;
}
public Person(String name,int posX,int posY) {
this.name = name;
this.posX= posX;
this.posY= posY;
}  
public void placeCharacter(int x, int y){
System.out.println(x+" "+y);
gameMap[b[x]][a[y]] = "⚫";
showMap();
}
}

这是我的Map类:

package MAP;
import Person.Person;
import java.security.PublicKey;
import java.util.Random;
public class Map {
public String[][] gameMap = {
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
{ "|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|","|"," ","|",},
{ "+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+","+---+",},
};
Random rnd = new Random();
public int random1 = rnd.nextInt(9);
public int random2 = rnd.nextInt(9);
public int [] a={1,4,7,10,13,16,19,22,25,28};
public int [] b={1,3,5,7,9,11,13,15,17,19};
public String tree="+";
public void placeObject(){
for(int i = 0; i<10;i++) {
int random3 = rnd.nextInt(5);
if (random3 < 3) {
int random1 = rnd.nextInt(9);
int random2 = rnd.nextInt(9);
gameMap[b[random1]][a[random2]] = tree;
System.out.println(random1 + " " + random2);
}
if(i==9)
showMap();
}
}
public void showMap() {
for (int r=0; r<gameMap.length; r++){
for (int c=0; c<gameMap[r].length; c++){
System.out.print(gameMap[r][c]+" ");
}
System.out.println();
}
}
} 

我的一个朋友告诉我提取地图,但我不知道我到底该怎么做。这些是物体图像图片

Person extends Map?这意味着每个人都是一个Map,而且每个人在Map类(包括gameMap)中都有自己的每个字段的INDIVIDUAL副本。相反,您应该将在main方法中实例化的映射作为构造函数参数传递给每个人,并将map作为Person的字段。

public Person(String name, int posX, int posY, Map map) {
this.name = name;
this.posX = posX;
this.posY = posY;
this.map = map;
} 

相关内容

  • 没有找到相关文章

最新更新