Data structures - project advice

Hi, I am not looking for help with a certain piece of code. But I am looking for some advice in regards to a project I want to start. I am somewhat new to programming, and I want to try and make a larger project compared to the smaller stupid ones I have been creating recently.

So I decided to create a text based game that would run only in the console window. My issue is I have not worked with any data structure yet, but I imagine these would be used for this type of project. I am unsure what is best to use for what though.

I plan on having a player inventory and a very basic store which the player can buy items from. I think these two systems (inventory & store) would use a data structure, but I am not sure what type, maybe a list? Or is there another structure I should look into? I was also wanting to implement a system which allows the player go pick certain paths for them to take. I think I could do this with an if statement,(ie- if player goes right, then that leads to path 2 and boss 2, if left then path 1, boss 1 etc) but that seems very unpractical, I guess I could also do this with a switch statement, but I believe there must be a better way to deal with this, again though I am unsure what way is best for this.

If anyone could give me some advice on what would be best to use for these circumstances, I would greatly appreciate it.
Last edited on
A three room adventure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <string.h>

enum room_id { NONE, KITCHEN, HALL, STAIRS };
enum { NORTH, SOUTH, WEST, EAST };

struct room {
    char    *name;
    int      next[4];   // N,S,W,E from this location
    // Things to add here
    // A long description
    // A list of objects ("There is a key here")
    // A list of Non Player Characters (NPC's)
    // Anything else that may be in each room
} rooms[] = {
    { "None" },
    { "Kitchen",    { NONE,    HALL,   NONE, NONE } },
    { "Hall",       { KITCHEN, STAIRS, NONE, NONE } },
    { "Stairs",     { HALL,    NONE,   NONE, NONE } },
};

int move ( int thisRoom, char *dir ) {
    int direction;
    switch ( *dir ) {
        case 'n': direction = NORTH; break;
        case 's': direction = SOUTH; break;
        case 'w': direction = WEST; break;
        case 'e': direction = EAST; break;
    }
    if ( rooms[thisRoom].next[direction] == NONE ) {
        printf( "The way is blocked!\n" );
    } else {
        thisRoom = rooms[thisRoom].next[direction];
        printf( "You are now in the %s\n", rooms[thisRoom].name );
    }
    return thisRoom;
}

int main ( ) {
    char buff[BUFSIZ];
    int  room = KITCHEN;

    while ( fgets( buff, sizeof buff, stdin ) != NULL ) {
        char    cmd[BUFSIZ];
        int     pos;
        char    *params;
        sscanf( buff, "%s %n", cmd, &pos );
        params = &buff[pos];
        
        if ( strcmp( cmd, "move" ) == 0 ) {
            room = move( room, params );
        } else
        if ( strcmp( cmd, "describe" ) == 0 ) {
            printf( "You are in the %s\n", rooms[room].name );
        }
        if ( strcmp( cmd, "quit" ) == 0 ) {
            break;
        }
    }

    return 0;
}


$ ./a.exe
describe
You are in the Kitchen
move n
The way is blocked!
move s
You are now in the Hall
quit

Your adventure is to turn this into C++.

And to answer your other question, yes, lists are good for inventories.

So for example, picking something up involves moving it from the list of objects in a room (there is a key here) to the list of objects in your backpack (you have a key).
Topic archived. No new replies allowed.