2

So I'd like to represent a rectangular maze of say dimensions 5x4 (rows x columns) using a 2D array in C language. However I am having trouble specifying what actually needs to be put into the 2D array.

int a[5][4] = {
    {},
    {},
    {},
    {},
    {}, 
};

Here is the skeleton of the 2D array, in each row there will be 4 values, I assume that each of these values is a single integer that tells us the properties of a cell in the maze. My problem is, is that really enough? How does a single value tell a robot weather there are 3 walls, 2 walls etc

Someone please enlighten me D:

Maze

emuterisa
  • 101
  • 1
  • 1
  • 5
  • I'd start using a matrix of a structure that holds info about N,S,W,E walls. – LPs May 18 '16 at 15:28
  • As someone who has worked with robots in a similar scenario, I hope that yours can turn exactly 90 degrees. – schil227 May 18 '16 at 15:34
  • @LPs This info can be stored nicely within 4 bits.. – Eugene Sh. May 18 '16 at 15:35
  • @schil227 Weel, it depends on the platform. On embedded I'd use bitfileds flags. ;) – LPs May 18 '16 at 15:36
  • @EugeneSh. For sure, as pmg already answered. – LPs May 18 '16 at 15:37
  • This isn't really C specific. It's a design problem. The best solution is going to be highly dependent on what your aims are. Both solutions below are nice for different reasons, but it's really worth trying to find a solution yourself, based on context of your problem. Nobody here can give the "best" answer to that. – DanBennett May 18 '16 at 15:37
  • 2
    Note : you can simplify all of the logic quite a bit by storing only the top and left walls of each cell, and adding a 1-cell wide border to the right and the bottom of your map. – Quentin May 18 '16 at 15:38

2 Answers2

7

use specific bits for specific properties of the room

#define ROOM_WALL_ABOVE (1 << 0)
#define ROOM_WALL_LEFT  (1 << 1)
#define ROOM_WALL_BELOW (1 << 2)
#define ROOM_WALL_RIGHT (1 << 3)
#define ROOM_DOOR       (1 << 4)

int a[5][4] = {0};
a[0][0] = ROOM_WALL_ABOVE | ROOM_WALL_LEFT;

if (a[x][y] & ROOM_WALL_RIGHT) printf("Cannot walk right.\n");
pmg
  • 106,608
  • 13
  • 126
  • 198
3

You could use a struct matrix

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

struct walls
{
    bool N; // true = wall false = no wall
    bool S; // true = wall false = no wall
    bool W; // true = wall false = no wall
    bool E; // true = wall false = no wall
};

int main()
{
    struct walls maze[5][4];

    // reset
    memset(maze, 0x00, sizeof(maze));

    // init
    maze[0][0].N = false;
    maze[0][0].S = true;
    maze[0][0].W = true;
    maze[0][0].E = false;

    // YOUR STUFF

   return 0;
}
LPs
  • 16,045
  • 8
  • 30
  • 61
  • Wow thats amazing! makes alot of sense thanks! Only problem I have now is understanding what the reset part does? (new syntax to me T_T) – emuterisa May 18 '16 at 15:44
  • It resets to `0` all bytes of `maze` matrix, no more than that. – LPs May 18 '16 at 15:45
  • Why would you need to do that? I think I'm lacking understanding in the memory department. If that was me, I probably would have excluded, with my current understanding of structs. Thanks! – emuterisa May 18 '16 at 15:49
  • 1
    You need that if you want to start from a clean matrix with all values set to false. Otherwise, due to its local scope (stack allocation), it will holds random values. BTW if your code assure to fill the whole structure with a correct initialization you could avoid it. – LPs May 18 '16 at 15:53