School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,406 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,600 people online right now. Registration is fast and FREE... Join Now!




Randomly generated levels

 

Randomly generated levels, Roguelike Programming

frostyraver

28 Jun, 2009 - 07:37 AM
Post #1

D.I.C Head
Group Icon

Joined: 21 Apr, 2009
Posts: 73



Thanked: 2 times
Dream Kudos: 50
My Contributions
I have decided to see if I can make some generator for a level system and to study this I'm currently looking at rogue like programming http://roguebasin.roguelikedevelopment.org...title=Main_Page

I would LOVE to wrap my head around it, Because I CAN'T see a program randomly generating 2 doors, then generating the next ones because this WOULD conflict errors, like just say the first room was North and South, you loop around and the last door would make it so you appear in the last room.

Kinda like:
XXX
X X
SXX


S being the start. You would loop around, wall detection just seams far too hard for this type of work, and FAR too many if statements.

I could see something like, just say you have 9 room textures,

789
456
123

room 1: NORTH and EAST exits
room 2: NORTH and EAST and WEST exits
room 3: NORTH and SOUTH exits
.etc.

then you could generate from point 1, picking a 1-9 room, then if say 4 is selected you would display 4's exits, like NORTH, EAST and SOUTH then the user goes north, because we came from the bottom up you would have to pick IF USER SELECT NORTH randomly pick room 4 5 6 7 8 9.

Something like that? if I have explained myself correctly. That would be pritty good for, say creating new walls because the user 'destroyed' it, replace the room with another appropreate room.

EDIT:

I mocked up something in console written in c#

CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Level_Generator
{
    class Program
    {
        static string a = "┌";
        static string b = "─";
        static string c = "┐";
        static string d = "│";
        static string e = "┘";
        static string f = "└";
        static string g = " ";
        static string Space = "";
        static string Space1 = "";
        static string Space2 = "";
        static string Space3 = "";
        static string Space4 = "";
        static string Space5 = "";
        static string Space6 = "";
        static string Space7 = "";
        static string Space8 = "";
        static string Space9 = "";
        static int genlevel;
        static int prevgenlevel;
        static Random randy = new Random((int)DateTime.Now.Ticks);

        static void Main(string[] args)
        {
            string end = "no";
            do
            {
                Space = "";
                Space1 = "";
                Space2 = "";
                Space3 = "";
                Space4 = "";
                Space5 = "";
                Space6 = "";
                Space7 = "";
                Space8 = "";
                Space9 = "";

                //789
                //456
                //123
                //** Do Space 7
                SpaceGen();
                Space7 = Space;
                Console.Write(Space);

                // If space 7 is right then do this
                if (Space7 == b || Space7 == c || Space7 == e)
                {
                    RightGen();
                }

                //** Since space 8 has no more connections with space 7 we can continue to the space 8 code and generate a space for it
                // Do Space 8
                SpaceGen();
                Space8 = Space;
                // Show the generated level
                Console.Write(Space);

                // If space 7 is right then do this
                if (Space8 == b || Space8 == c || Space8 == e)
                {
                    RightGen();
                }

                //** Since space 9 has no more connections with space 8 we can continue to the space 9 code and generate a space for it
                // Do Space 9
                SpaceGen();
                Space9 = Space;
                // Show the generated level
                Console.WriteLine(Space); //Start at 4 now

                // If space 9 is right then do this
                if (Space9 == b || Space9 == c || Space9 == e)
                {
                    RightGen();
                }
                //
                //** Start space 4 now check space 7 if it's pointing down
                // If space 1 is right then do this
                if (Space7 == d || Space7 == e || Space7 == f)
                {
                    DownGen();
                    Space4 = Space;
                }
                // Else generate somehing else
                else
                {
                    SpaceGen();
                    Space4 = Space;
                }
                // Show the generated level
                Console.Write(Space);

                //** Do Space 5, if space 8 is down check it
                if (Space8 == d || Space8 == e || Space8 == f)
                {
                    DownGen();
                    Space5 = Space;
                }
                // Else if space 4 is right
                else if (Space8 == a || Space8 == b || Space8 == f)
                {
                    LeftGen();
                    Space5 = Space;
                }
                // Else just generate!
                else
                {
                    SpaceGen();
                    Space5 = Space;
                }
                // Show the generated level
                Console.Write(Space);

                //** Do Space 6, if space 9 is down check it
                if (Space9 == d || Space9 == e || Space9 == f)
                {
                    DownGen();
                    Space6 = Space;
                }
                // Else if space 4 is right
                else if (Space9 == a || Space9 == b || Space9 == f)
                {
                    LeftGen();
                    Space6 = Space;
                }
                // Else just generate!
                else
                {
                    SpaceGen();
                    Space6 = Space;
                }
                // Show the generated level
                Console.WriteLine(Space); //Start at 1 now

                //
                //** Start space 1 now check space 4 if it's pointing down
                // If space 1 is right then do this
                if (Space4 == d || Space4 == e || Space4 == f)
                {
                    DownGen();
                    Space1 = Space;
                }
                // Else generate somehing else
                else
                {
                    SpaceGen();
                    Space1 = Space;
                }
                // Show the generated level
                Console.Write(Space);

                //** Do Space 2, if space 5 is down check it
                if (Space5 == d || Space5 == e || Space5 == f)
                {
                    DownGen();
                    Space2 = Space;
                }
                // Else if space 4 is right
                else if (Space5 == a || Space5 == b || Space5 == f)
                {
                    LeftGen();
                    Space2 = Space;
                }
                // Else just generate!
                else
                {
                    SpaceGen();
                    Space2 = Space;
                }
                // Show the generated level
                Console.Write(Space);

                //** Do Space 3, if space 6 is down check it
                if (Space6 == d || Space6 == e || Space6 == f)
                {
                    DownGen();
                    Space3 = Space;
                }
                // Else if space 4 is right
                else if (Space6 == a || Space6 == b || Space6 == f)
                {
                    LeftGen();
                    Space3 = Space;
                }
                // Else just generate!
                else
                {
                    SpaceGen();
                    Space3 = Space;
                }
                // Show the generated level
                Console.WriteLine(Space); // New Line and end the generator
                Console.WriteLine("");
                Console.WriteLine("Generate again?");
                Console.WriteLine("(1) Generate");
                Console.WriteLine("(2) Generate and Clear");
                Console.WriteLine("(3) Exit");
                Console.Write(">");
                int Choice = Convert.ToInt16(Console.ReadLine());
                if (Choice == 2)
                {
                    Console.Clear();
                }
                if (Choice == 3)
                {
                    end = "yes";
                }
                Console.WriteLine("");
            } while (end == "no");
        }
        /// <summary>
        /// Generate right
        /// </summary>
        public static void RightGen()
        {
            // Randomize
            genlevel = randy.Next(1, 4);

            // 3 Spot picker
            if (genlevel == 1)
            {
                Space = b;
            }
            if (genlevel == 2)
            {
                Space = c;
            }
            if (genlevel == 3)
            {
                Space = e;
            }
        }
        /// <summary>
        /// Generate Left
        /// </summary>
        public static void LeftGen()
        {
            // Randomize
            genlevel = randy.Next(1, 4);

            // 3 Spot picker
            if (genlevel == 1)
            {
                Space = a;
            }
            if (genlevel == 2)
            {
                Space = b;
            }
            if (genlevel == 3)
            {
                Space = f;
            }
        }
        /// <summary>
        /// Generate Up
        /// </summary>
        public static void UpGen()
        {
            // Randomize
            genlevel = randy.Next(1, 4);

            // 3 Spot picker
            if (genlevel == 1)
            {
                Space = a;
            }
            if (genlevel == 2)
            {
                Space = c;
            }
            if (genlevel == 3)
            {
                Space = d;
            }
        }
        /// <summary>
        /// Generate Down
        /// </summary>
        public static void DownGen()
        {
            // Randomize
            genlevel = randy.Next(1, 4);

            // 3 Spot picker
            if (genlevel == 1)
            {
                Space = d;
            }
            if (genlevel == 2)
            {
                Space = e;
            }
            if (genlevel == 3)
            {
                Space = f;
            }
        }
        /// <summary>
        /// Generate Blank space, used for a starter
        /// </summary>
        public static void SpaceGen()
        {
            // Randomize
            genlevel = randy.Next(1, 8);

            // 9 Spot picker
            if (genlevel == 1)
            {
                Space = a;
            }
            if (genlevel == 2)
            {
                Space = b;
            }
            if (genlevel == 3)
            {
                Space = c;
            }
            if (genlevel == 4)
            {
                Space = d;
            }
            if (genlevel == 5)
            {
                Space = e;
            }
            if (genlevel == 6)
            {
                Space = f;
            }
            if (genlevel == 7)
            {
                Space = g;
            }
        }
    }
}


All it does is scan the previously generated spots, I suppose I could fix it up a little bit and make a make type of thing i'm SURE there's gotta be an easier way for this, I'll update as I find more information out.

Edit2:
http://nrkn.com/1kRl/v3/
Roguelike c# under 1kb smile.gif.

This post has been edited by frostyraver: 28 Jun, 2009 - 09:59 AM

User is offlineProfile CardPM
+Quote Post


DoubleFission

RE: Randomly Generated Levels

28 Jun, 2009 - 05:44 PM
Post #2

D.I.C Head
Group Icon

Joined: 20 Sep, 2008
Posts: 189



Thanked: 6 times
My Contributions
Well how about having a room object?

Each room has an array of 4 exits
- North
- East
- South
- West

Lets pretend they're integers: 1 means wall 0 means doorway

If you store your rooms in a 2 dimensional array, then you just have to check the rooms you've already generate.

If there is a doorway there, put one in your current room on the corresponding side
Else, put a wall on the corresponding side.
For the rest of the possible door locations, generate a random number to either be a door or not. (For 'outside' walls you can have a check and not put doors leading outside)

User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: Randomly Generated Levels

28 Jun, 2009 - 07:48 PM
Post #3

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,910



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
Googling rogue source code I found this link:

http://rogueclone.sourceforge.net/

It is in C++ but it is similar to C#, thought it might help. smile.gif
User is offlineProfile CardPM
+Quote Post

frostyraver

RE: Randomly Generated Levels

28 Jun, 2009 - 10:32 PM
Post #4

D.I.C Head
Group Icon

Joined: 21 Apr, 2009
Posts: 73



Thanked: 2 times
Dream Kudos: 50
My Contributions
QUOTE(DoubleFission @ 28 Jun, 2009 - 05:44 PM) *

Well how about having a room object?

Each room has an array of 4 exits
- North
- East
- South
- West

Lets pretend they're integers: 1 means wall 0 means doorway

If you store your rooms in a 2 dimensional array, then you just have to check the rooms you've already generate.

If there is a doorway there, put one in your current room on the corresponding side
Else, put a wall on the corresponding side.
For the rest of the possible door locations, generate a random number to either be a door or not. (For 'outside' walls you can have a check and not put doors leading outside)


That's kinda like what I have set up, It starts from room 7 generates a random room, then moves onto room 8, room 8 checks if room 7 is pointing towards it if so only do the exits entering space 8 from the left other wise generate a space, the same goes for space9 but this time it moves down to space 5 space 5 then checks space 7 if space 7 is entering downwards, space 6 check if space 8 and space 5 are entering if it's space 8 do the down if it's space 5 the do the left.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 11:04PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month