0

I'm a beginner in the world of java for android (and java in general) and am trying to make an interactive fiction game app to improve my abilities. I have already made this game in python, and am simply trying to convert to java, but I'm running into a lot of difficulties.

This biggest problem I've had so far is creating the world map. In python, I simply made a variable that contained an 8x4 list of lists with object variables denoting specific rooms in the game world, and zeroes representing places the player could not go, like so (this is only part of the map):

worldMap = [[0, 0, othDorm, 0, 0],
        [0, 0, darkForest, 0, 0],
        [0, observatory, forkPath, resHall, 0],

This essentially created an coordinate plane. Then within my 'Player' class, I had variables that gave his x and y coordinate, and his current position by indexing his location on the world map, like so:

class player:
def __init__(self, name, ycoord, xcoord):
    self.name = name
    self.xcoord = xcoord
    self.ycoord = ycoord
    self.mapposi = worldMap[ycoord][xcoord]

Finally, the player could enter a command to 'move' north, south, east, west, and the player coordinates would update by adding 1 to the appropriate x/y, placing him in a new room.

However, java seems a lot stricter about it's data types, and I've been having trouble converting this system. First I tried to make an ArrayList, but I guess I can't have an integer and object datatype within the same list? Then I tried to make an ArrayList of objects, and where null values would replace the 0 values. To do this I used the code:

    public static Object worldMap[][] = new Object[8][4];
    worldMap[0][0] = mainGate;

where worldMap would become my x/y grid filled with objects from my custom made "Room" class, and "mainGate" being a Room at position 0, 0. This allowed me to compile, but when I checked the player's location at [0][0], Java returned a value of "com.weebly.fishopolis.adesaw.Room@9676a63", which is obviously not what I wanted. I also tried to use Player.mapposi.name to produce the name, but it seems my variables weren't inherited and got a "could not resolve symbol" error. I also tried to make an ArrayList<Room>[][], but I got a "generic array" error going that route.

At this point I really have no idea how to move forward, and I'm starting to think I'm missing a really easy solution I'm just not aware of, because this was so simple in python yet seems so complicated in Java. My end goal here is to be able to call up the game players position in the world, and be able to access the object at that index and all its variables. For example in python, I could do something like

Player.mapposi.name
Player.mapposi.roomitems

and that would return me the name and room items of the object variable at whatever x/y index the player was currently in.

As I said, I'm very new with Java, so any explanations/tips would be really appreciated. And I hope that I gave you enough code to understand what I'm trying to do, but if not just ask and I'll add whatever else is needed

Dr. Fishopolis
  • 79
  • 1
  • 10
  • The fact that it prints ...Room@9676a63 is because your Room class is missing a reasonable @Override for the toString() method. In that sense: your java code is doing the same as the python stuff. – GhostCat Sep 08 '17 at 03:56
  • The "com.weebly.fishopolis.adesaw.Room@9676a63" what Java "returned" is really just the standard way Java converts an object to a string representation. (You probably just printed it out, EG `Sysem.out.println(worldMap[0][0])`... – Usagi Miyamoto Sep 08 '17 at 03:58
  • Also, you should create a simple class wih the coordinates, implement its `equals` method, and use it as key in a `Map` collection... – Usagi Miyamoto Sep 08 '17 at 04:00
  • Wow, thank you for the advice. Actually when I tried to use the toString() method, I received another error. However, when I read the error it made me realize I had incorrectly defined playerLoc as a generic Object, instead of defining it specifically as a Room. After fixing it, I was able to use the index and my playerLoc has inherited all the attributes of Room. So, basically this question can be deleted as the problem I asked about was unrelated to the actual problem my code had. Thank you for steering me in the right direction, and I appreciate your work! – Dr. Fishopolis Sep 08 '17 at 05:28

0 Answers0