Structure maze in C

Asked

Viewed 952 times

3

Okay, I’d like to play a game in C "console" but I’m having a question.... Basically the idea is to have a character that advances in a maze thanks to the user, if the user writes "right" the character moves to the right etc..

I imagine that the labyrinth is basically a two-dimensional array, and inside it all zeros and ones are inserted, "1" would be walls and "0" free paths.. (if there are better ideas please say)

Until then I imagine that there is not much difficulty, but I ask, if you want to put an inventory to the character, how do I make so that in a given house of the array have an object there? If for example in the map array[2][3] there is already a 0 "saying it is free path", how can I insert an object there? like a backpack or something?

I appreciate your willingness to help :)

  • 4

    It would be enough to have a separate array of objects. Assuming that knife is code 1, and ladder code 2: objetos = { {1,9,1},{3,4,1},{3,4,2} } (has a knife in position [1,9], another knife in position [3,4], and in this latter position there is also a ladder. Nothing would prevent you from using different values for the map itself if you prefer. Instead of zeroes and ones, you use one for the wall, two for the knife, four for the ladder, eight for the monster, and so on. A room worth 10 definitely has a monster and a knife.

  • This reminded me of a game called Blip that came as a hobby within the Skilled Financial Manager whose stages followed this same style, but even simpler: # were walls, white spaces paths to follow, D was door, W, water and etc.

  • Hmmm, thanks for the answers, already gave me a very interesting idea of how to do :) However I was also advised to learn about "structs" and basically I’m new at this, I was told that it would serve me a lot, I saw one that another tutorial on how to use, but I’m not fitting the structs with my project, I don’t know what I could use it for.... any idea?

  • @Bacco this deserves an answer.

  • I’m sorry, I’m not used to Stack Overflow, but if you don’t receive notification, I answered below the post...

1 answer

1

Suppose you use a character to represent a position on the grid, where a blank is a free path and X is a wall and other objects are other characters:

const char caminho = ' ';
const char parede  = 'X';
const char mochila = 'm';
const char faca    = 'f';
const char jogador = '+';
const char ouro    = '$';
const char monstro = 'v';
const char chave   = ',';
const char porta   = '#'; 

You can represent the labyrinth like this:

#define LARGURA 10
#define ALTURA 10

const char[LARGURA][ALTURA + 1] labirinto = {
    "XXXXXXXXXX",
    "X # XfX $X",
    "X X   XvXX",
    "X XXXXX  X",
    "X X      X",
    "X X XXXX X",
    "X X Xm X X",
    "X   XX X X",
    "X+X    X,X",
    "XXXXXXXXXX",
};

This + 1 at the end is the string terminator. This format has the advantage that you can draw the labyrinth on the screen going through lines and just giving a printf in each row:

int i;
for (i = 0; i < ALTURA; i++) {
    printf("%s", labirinto[i]);
}

It is obvious, that if you have a better way to draw the maze (especially using images, and not just text), there is nothing that forces you to use the printf, you can use the method you think best. But anyway this is useful at least for debug.

You probably won’t want the static or fixed-size maze. Maybe then dynamic memory allocation will be better:

char* labirinto = malloc(sizeof(char) * altura * (largura + 1));
// Gera o labirinto...
// Roda o jogo...
free(labirinto);

This then works as long as you can use a character to represent a thing. If you have a very large variety of objects, then maybe the best thing to do is to use pointer arrays for some kind of structure that describes these objects.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.