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