'How can I add different objects in array at the same time and store them for the next move?
I am learning Java and working on game project for my finals. I am trying to add different values to my array but when I print array every time it shows different objects on the map I will share the output here. This is the main class:
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");
}
}
This is my Person class:
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();
}
}
This is my Map class:
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();
}
}
}
One of my friend told me to extract the map but I dont know what should I do exactly. these are objects image image
Solution 1:[1]
Why does Person extends Map? That implies that every Person IS a Map, and furthermore that each person has its own INDIVIDUAL copy of every field inside of the Map class, including gameMap. Instead, you should pass the map you instantiate in the main method to each person as a constructor parameter, and have map be a field of Person.
public Person(String name, int posX, int posY, Map map) {
this.name = name;
this.posX = posX;
this.posY = posY;
this.map = map;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rubydesic |
