This site requires JavaScript, please enable it in your browser!
Greenfoot back
RedPhoneBooth
RedPhoneBooth wrote ...

2012/4/24

Maze gen using a 2-D array

RedPhoneBooth RedPhoneBooth

2012/4/24

#
Okay, im trying to make a maze generator for my dungeon crawler game, the players location is tracked through rooms by a 2-D array. the value of 1 is nothing, the value of 2 makes the computer "load" the room number that is stored in int room_Num, which is a placeholder for my 2-D array while the player is in that "room" (Because whatever room the player is in, sets that location to 2). The gen im going for is one that will remove blocks to reveal entrances as it clears out a maze. But the problem is is that since im constantly removing and adding objects as I move through the world, theres no real "storage" system as the player moves through the world unless I add a diffrent number to the 2-D array, (lets say 5) add an if statement that says if world == 5, room5();. room5() would then just add objects to the world in the way that I tell them too. This works fine but the problem is is because a maze gen would be using a random selection of paths to create the maze, I would also need corresponding numbers on my 2 d array that match what the maze generator has already done. But because there are about 21 (my math might be wrong) diffrent possibilities for rooms with a max of 4 exits, this quickly becomes inefficient and annoying to code. I'm wondering if there would be a way to do this by writing to a file or something that could accomplish my task of creating a maze easier than having my algroythem assining up to 21 different values to things inside of a 2-D array. Like could I have a file that holds all the rooms in a more specific manner, such as as it generates the maze, each room it goes into, it writes into the file where the room is, and how many exits are open using 4 integers. So that my program when faced with a new room, will go into the file, get the values, and load the room accordingly. This would also make it eaiser to designate treasure rooms, boss rooms, stair rooms, key rooms, and difficulty levels as they could be all stored inside of my file. :| any help or tips?
davmac davmac

2012/4/24

#
Dude, that is the longest single paragraph that I have ever read. I don't really understand what you're trying, what the problem is, nor why you think writing to a file would help (to put it succintly: anything you can write to a file, you can also store in a data structure, and it will generally be easier to do so). What I do understand: You've got a 2d array which somehow represents a maze. I assume that the array indices correspond to map co-ordinates, i.e. the map is a grid which is represented by the array? I don't understand why you need to store '2' in a room to represent the player being there. You could more easily track the player location using separate position variables (playerX, playerY). Are you looking for a way to store which possible direction can be travelled from each room? In that case, rather than have a 2d array of 'int', perhaps you could: - Create a new class called 'Room' - have four instance variables in Room - 'boolean canGoNorth;', 'boolean canGoWest;', etc; - have the 2d array be a 2d array of Room rather than a 2d array of int. Room rooms = new Room; // or whatever dimensions you want - as a benefit you can easily add new variables to the Room class without having to change the meaning or decoding logic of magic numbers in your int array. I hope this makes sense.
RedPhoneBooth RedPhoneBooth

2012/4/25

#
Well, in my game I have "rooms". Rooms are stored in a 5 x 5 2-d array. each room is a screen. You move from screen to screen by moving to the edge of the screen. When you do so, It changes the value of room_Num to the room you were just in (inside the 2-D array), changes the value of the room your moving to to two, and changes the value of room_Num to the room you are currently in. It then runs an if statement that will check to see what room_Num is, if it is one of the possible room's I have programmed into the method, it will run it, for example, If room_Num == 3, run the method Room3(); and in Room3(), I have addObject(new block); or whatever. I then teleport my player to the opposite side that he was on when he activated this whole sequence. So thats basically what happens when I switch rooms, but since theres 16 possible combinations of a room with 4 exits, I would need 16 diffrent numbers representing the possible rooms in my 2-D array that would have to be added by my Maze generator. I don't want to do this because I also want other variables such as a treasure room, boss room, the possibility of different textures, ect... I understand I could just have different variables for one room method, but the thing is it still needs to be stored, because only one room is ever in the world at one time. So what im trying to do is to make a maze generator write to a text document in a specific pattern, then each of the 25 rooms are stored within one document, and then code a praser to recognise specific elements of my text document and set data values to specific parts of the string I ask. For example, my maze gen might write... { Room 2 5 //Tells the location of the room on the 2-D array @N 1 // Tells the north door is open @S 0 //Tells the south door is closed @E 0 @W 1 %C 1 //Tells the room is cleared from enemies and they shouldnt spawn %T 0 //Tells that there is no treasure chests in this room } My praser would then be programmed to look for symbols and use the data on each line to build and destroy each room based on the inputs. I want to go for document creating because then it allows for save files and fun stuff like that. It just seems like a neater way of solving my problem and it also makes it easier to add different variables to the game such as random events. meh, tell me what you think. Personaly I think its a pretty cool idea and to implament it would be great! My eventual hope would be to port it over to a professional environment and publish it so it seems like making save files would be a good direction to go in.
RedPhoneBooth RedPhoneBooth

2012/4/25

#
Oh and sorry about the long paragraphs, lol I like showing off my ideas ;)
davmac davmac

2012/4/25

#
... It changes the value of room_Num to the room you were just in (inside the 2-D array), ..., and changes the value of room_Num to the room you are currently in
You say both that room_Num gets set the value of the room that you were just in, and the room you are currrently in. Which is it? What is the 'value' of the room in this context - do you mean the value from the array, or some sort of room index? (I think you must mean the former, but I'm not certain).
but since theres 16 possible combinations of a room with 4 exits
I think you mean there's 16 possible combinations of a room with up to 4 exits, i.e. 0 to 4 exits, each of the four exits may be present or not, so there are 2^4 = 16 combinations (although one of these implies a room with no exits).
I understand I could just have different variables for one room method, but the thing is it still needs to be stored, because only one room is ever in the world at one time.
You can have an array of 'Room' objects, this is what I suggested previously. You can store this as an instance variable in the world. If you are changing world each time the player enters a new room, you can transmit the array from the old world to the new world when you create it. You could certainly read the array out of an array to begin with, yes.
RedPhoneBooth RedPhoneBooth

2012/4/26

#
yea, yea, I could do that, probably easier and more efficient, but I still want to try out the save to file method, cause ive never done anything like that and the idea of file saves seems really cool, mabey I could combine the two so it creates the data in the file save, but stores it in the game as soon as it starts. I dont know, lol i'll smooth out the kinks later
danpost danpost

2012/4/26

#
I suggest starting with in-code data structures, then afterwards, you can figure out how to export and import the data to and from a file. Keep in mind, however, that you are wanting to do this (it may influence the way you create the data structure. It probably would be best to have everything stored in the same type of variable (integer would be my choice). All four of the doors, AND whether they are open or not can be stored in one integer that is no greater than 255. So, an array of int would store (1) doors (2) enemyCount (3) treasureChestCount. The placement in the array would be the room number (the first 5 rooms would be 0,0 through 0,4, the next 5 rooms would be 1,0 through 1,4, etc. or 'roomY * 5 + roomX' would be the data set for room). For getting the door status to a single integer use (some variables may need to be renamed to match your code)
// if doors are not already in an array, use the following line with the proper door variables
Door[] doors = { doorEast, doorSouth, doorWest, doorNorth };
int allDoors = 0;
for (int i = 0; i < 4; i++)
{
    allDoors = 4 * allDoors;
    if (doors[3 - i] != null) 
    {
        allDoors += 2;
        if (doors[3 - i].isOpen) allDoors++;
    }
}}
There might be an easier way, but this will do the trick.
danpost danpost

2012/4/26

#
To get the data out of the integer
Door[] doors = { null, null, null, null };
for (int i = 0; i < 4; i++)
{
    if (allDoors % 4 > 0)
    {
        doors[i] = new Door();
        doors[i].opened = allDoors % 2 > 0 ? true : false;
    }
    allDoors = allDoors / 4;
}
RedPhoneBooth RedPhoneBooth

2012/4/27

#
Wait, are you suggesting an array for each room? how would this work?
danpost danpost

2012/4/27

#
Are you referring the the Door array or the int array? If the former, Yes. Having the doors in an array would help make coding easier when dealing with them (otherwise you would have four times the code with four different variable names). Also, access to door information would be easier. If the latter, maybe not. I mentioned it to aid in the visualization of what was happening (not to actually have that array present in the code).
davmac davmac

2012/4/27

#
To get the data out of the integer
Danpost, the code you've suggested will arbitrarily leave some doors as 'null'. The other doors will either be open or closed (whatever that means...). Seeing as the maze is a 2d grid, I don't think there's any real need for objects; you can use booleans to record the state (and the presence, if necessary) of each door. Edit: no wait, I see you're using 2 bits to encode each door, including both presence and state. However, I'm not sure that the presence information is needed. Also, there's no need to encode everything as an 'int'. You can use DataInputStream/DataOutputStream to read/write a full range of primitive types. This is probably the easiest way for a novice to deal with structured data I/O. On the other hand, maybe RedPhoneBooth wants to be able to edit the file using a conventional text editor, in which case they will need to write their own parser. RedPhoneBooth: If your rooms are objects, you can have whatever you want in them, including arrays to represent the doors. It really is the best way to go.
RedPhoneBooth RedPhoneBooth

2012/4/27

#
okay so what im thinking is have a class for rooms, and have the prameters of room location, all the doors, and all that jazz. wait, can I store objects in an array? that would be cool cause then I could create 25 new room objects, each with diffrent parameters based on the maze generation code. also, my doors arent actually things, more, the absence of things... I have blocks around the perimeter of my rooms, then if one door is "open," I just remove the blocks next to a section of the edge of the room. when the player hits the edge of the room, he changes rooms
davmac davmac

2012/4/28

#
wait, can I store objects in an array? that would be cool
Yes - that's what I've been telling you, right from the start :)
RedPhoneBooth RedPhoneBooth

2012/4/30

#
Okay, I got this, lol sorry, I was having trouble understanding all your fancy computer talk :D lol jk, thanks a bunch tho
You need to login to post a reply.