Don't have an account? Register now to chat, post, use our tools, and much more.
So its been a while...

https://www.cemetech.net/img/ppd/8954.png

yeah...

but I am finally back

I don't think I will be doing a lot of big calc projects anymore, I'm thinking about trying some other new things and expanding my programming knowledge.

So I participated in a tech camp last week where I learned a lot about Windows C++. I worked on a project involving a Cookie Clicker style idle game and while I was working on 2D arrays, I realized that they could be used to generate a map like 2D Birdseye view dungeon games or whatever they are called.

for example, char map[9][9] = { { '#', '#', '#', '#', '#', '#', '#', '#', '#' }
{ '#', ' ', ' ', '#', ' ', '#', ' ', ' ', '#'}
{more stuff here and so on}};

I thought it would be fun to make a map thing like that and be able to control a character to move around. The funnest part would be checking to see if the player could move to a certain location or if it was blocked.
Then of course I decided it would be a wonderful idea to generate a map randomly. and I don't know how to do that.

Question time!

So an algorithm would be the way to go to generate this map right?
And if so, how should this work? I was thinking of choosing a random place on the map and creating a wall from that place to the nearest wall. But after that where do I go?

Any ideas?

*Bump* <--Thats how you do it right?

so heres the code right now, it works pretty well but is there any easier way of doing this? Can I somehow create the array as all spaces without having to store each one?

Also I'm sure there must be a way to make my code more efficient so please do help me

So my plan is 'X' all around the outside and '#' for walls.
Still working on the algorithm, I am thinking of some sort of random point selecting, line drawing thing.

Anyway, code right now:

Code:
```#include <iostream> #include <string> using namespace std; void main() {    char map[9][9];    char possiblePieces[3] = { 'X', '#', ' ' };    bool mapGenerated = false;    //set map to be all spaces    for (int i = 0; i <= 9; i++)    {       for (int j = 0; j <= 9; j++)       {          map[i][j] = ' ';       }    }    //store first two lines    for (int i = 0; i <= 9; i++)    {       map[0][i] = 'X';       map[9][i] = 'X';       map[i][0] = 'X';       map[i][9] = 'X';    }    //display map, just for testing now    for (int i = 0; i <= 9; i++)    {       for (int j = 0; j <= 9; j++)       {          cout << map[i][j];       }       cout << endl;    }        return;    while (mapGenerated == false)    {       //Randomly stores things, makes walls, etc.    } }```

And the output right now is...oh wait whats this? A new error D:
Not sure what's going on.
You're writing outside the bounds of the (stack-allocated) array.

Code:
`for (int i = 0; i <= 9; i++)`

You're hitting indices 0 through 9, which is 10 locations. The maximum legal index in either dimension of the array is 8 (9 locations as you've declared it).
Thanks Tari, you get a cookie! Well I look like a newbie now xD (Stupid Mistake #1991368284 Recorded)
Would it be beneficial to put this all in a function so I can just call this from the main? Is there any reason to not do that?

Also is the code the most efficient it can be for now?

Update on the algorithm:
1) Generate Random Point
2) Go random direction from that point for random distance
3) Repeat above until hits a wall

EDIT:
I made the generate map thing a function and I was trying to make the dimensions easily changed. Unfortunately this returns an error at "char map[d][d];"
The error is that d is not a constant variable. This section was working before when I declared the variable in the function but I moved it out so that it could be changed by the user in the main function.
I've also tried adding something like "const int d = d;" before that and also changing the name of the variable leading to something like

"char generateMap(const int i)
{
const int d = i;
...etc etc"

Code:
```char generateMap(const int d) {    char map[d][d];    char possiblePieces[3] = { 'X', '#', ' ' };    bool mapGenerated = false; }```

Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

»
» All times are GMT - 5 Hours

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum