Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 20 Apr 2010
Offline Last Active Today, 06:42 AM

#5238273 Text adventure design in C

Posted by LennyLen on 03 July 2015 - 05:07 PM

You'll definitely want to create a stuct that can hold all the data about a specific area (name, description, neighboring areas, items that are there, commands that can be issued there, etc).


To begin with you can have a huge array of all these areas, as this is simple and will let you focus on learning about structs, and you can just have one function that allocates everything at the start of the game. Later on, you can learn how to dynamically allocate everything into a list.


You'll probably want to start with an array of strings (the C type of string) for commands, and one for items, and then your area struct can just store which indexes of these arrays are applicable.  


By having all the areas stored in a struct array, you can now have just one game loop that only need to know the current area and which will work with all areas.  And instead of having to have a menu for each area where he user types a number, they can type a command, and you can match that against the commands allowed for each area.


To help get you started, I've written a short example.  It only has a few areas and a very small set of commands, but it should be enough for you to build on.  As I mentioned earlier, they way this is allocating the data is not ideal (it will be fine for a very small game, but will quickly become cumbersome if you want to keep adding areas), but you can improve on that later.

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

typedef struct area {

    char name[20];
    char description[200];
    int neighbours[4];
    int num_items;
    int *items;
    int num_commands;
    int *commands;

} area;

typedef enum {NORTH, EAST, SOUTH, WEST} directions;

area locations[5];
char items[5][20];
char commands[3][20];  // special commands that can only be performed in some areas

void populate_items();
void populate_commands();
void populate_areas();

int main() {


    int current_area = 0;
    int exit_status = 0;
    char command[20];

    while (exit_status == 0) {

        printf("\nYou are at %s\n", locations[current_area].name);
        scanf("%20s", command);
        if (strncmp(command, "quit", 20) == 0)
            exit_status = 1;
        else if (strncmp(command, "look", 20) == 0)
            printf("%s\n", locations[current_area].description);
        else if (strncmp(command, "search", 20) == 0) {
            if (locations[current_area].num_items > 0) {
                printf("You find:\n");
                for (int n = 0; n < locations[current_area].num_items; n++)
                    printf("%s\n", items[locations[current_area].items[n]]);
                printf("There is nothing here.\n");
        else if (strncmp(command, "north", 20) == 0) {
            if (locations[current_area].neighbours[NORTH] != -1)
                current_area = locations[current_area].neighbours[NORTH];
                printf("You can't go that way.\n");
        else if (strncmp(command, "east", 20) == 0) {
            if (locations[current_area].neighbours[EAST] != -1)
                current_area = locations[current_area].neighbours[EAST];
                printf("You can't go that way.\n");
        else if (strncmp(command, "south", 20) == 0) {
            if (locations[current_area].neighbours[SOUTH] != -1)
                current_area = locations[current_area].neighbours[SOUTH];
                printf("You can't go that way.\n");
        else if (strncmp(command, "west", 20) == 0) {
            if (locations[current_area].neighbours[WEST] != -1)
                current_area = locations[current_area].neighbours[WEST];
                printf("You can't go that way.\n");


    return 0;

void populate_items() {

    strncpy(items[0], "shell", sizeof items[0]);
    strncpy(items[1], "branch", sizeof items[1]);
    strncpy(items[2], "lighter", sizeof items[2]);
    strncpy(items[3], "water bottle", sizeof items[3]);
    strncpy(items[4], "torch", sizeof items[4]);



void populate_commands() {

    strncpy(commands[0], "burn", sizeof commands[0]);
    strncpy(commands[1], "move", sizeof commands[1]);
    strncpy(commands[2], "break", sizeof commands[2]);



void populate_areas() {

    strncpy(locations[0].name, "the beach", sizeof locations[0].name);
    strncpy(locations[0].description, "A sandy beach covered in shells.", sizeof locations[0].description);
    memcpy(locations[0].neighbours, (int[]) { -1, 1, -1, -1 }, sizeof locations[0].neighbours);
    locations[0].num_items = 1;
    locations[0].items = malloc(locations[0].num_items * sizeof(int));
    locations[0].items[0] = 0;
    locations[0].num_commands = 0;
    locations[0].commands = NULL;

    strncpy(locations[1].name, "the forest", sizeof locations[0].name);
    strncpy(locations[1].description, "A dark and spooky forest.", sizeof locations[0].description);
    memcpy(locations[1].neighbours, (int[]) { 2, -1, 4, 0 }, sizeof locations[0].neighbours);
    locations[1].num_items = 1;
    locations[1].items = malloc(locations[0].num_items * sizeof(int));
    locations[1].items[0] = 1;
    locations[1].num_commands = 0;
    locations[1].commands = NULL;

    strncpy(locations[2].name, "the hill", sizeof locations[0].name);
    strncpy(locations[2].description, "A windswept hilltop covered in tussock.", sizeof locations[0].description);
    memcpy(locations[2].neighbours, (int[]) { -1, 3, 1, -1 }, sizeof locations[0].neighbours);
    locations[2].num_items = 2;
    locations[2].items = malloc(locations[0].num_items * sizeof(int));
    locations[2].items[0] = 2;
    locations[2].items[1] = 4;
    locations[2].num_commands = 0;
    locations[2].commands = NULL;

    strncpy(locations[3].name, "the mountain", sizeof locations[0].name);
    strncpy(locations[3].description, "A tall lonely mountain.", sizeof locations[0].description);
    memcpy(locations[3].neighbours, (int[]) { -1, -1, -1, 2 }, sizeof locations[0].neighbours);
    locations[3].num_items = 0;
    locations[3].items = NULL;
    locations[3].num_commands = 0;
    locations[3].commands = NULL;

    strncpy(locations[4].name, "the river", sizeof locations[0].name);
    strncpy(locations[4].description, "A mighty river, too fierce to cross.", sizeof locations[0].description);
    memcpy(locations[4].neighbours, (int[]) { 1, -1, -1, -1 }, sizeof locations[0].neighbours);
    locations[4].num_items = 1;
    locations[4].items = malloc(locations[0].num_items * sizeof(int));
    locations[4].items[0] = 3;
    locations[4].num_commands = 0;
    locations[4].commands = NULL;


#5238175 Creating menus using Codeblocks (C++)

Posted by LennyLen on 03 July 2015 - 08:26 AM

I may be wrong, but I'm guessing from the title of the thread that you want to know how to use Code::Blocks to create system menus.


If this is the case, the answer is that you can't1  If you want a visual tool for creating menus (and if you're targeting Windows), then Visual Studio is a better choice of IDE for you as it has a WYSIWYG editor for GUI elements, unlike Code::Blocks, which is purely a code editor.



[1] You can of course by writing code for creating the menus, but that can be done with any IDe/editor, and isn't something intrinsic to Code::Blocks.

#5237798 How to make 3D art look more like a 2d?

Posted by LennyLen on 30 June 2015 - 10:00 PM

Wait you could make it a spritesheet? How?


You render your 3D model and animate it, then make images of each frame of the animation.  Put these images together and you have a spritesheet you can use in a 2D engine.  This is essentially what Blizzard did with all their older games (as well as many other developers).

#5237796 How can i save player data, and how to prevent cheating?

Posted by LennyLen on 30 June 2015 - 09:55 PM

To add to what L.Spiro said:


If your game gets popular, people will try to  crack it, and eventually they will succeed. This is inevitable.  And once one person has, the internet will see to it that their work is available to anyone playing your game.  You can then waste time changing things, but there will be more of them than there are of you and someone will once again crack it.


Just make your game fun and let people play it however they want to.

#5237103 Your thoughts on the value of profilers and debuggers?

Posted by LennyLen on 27 June 2015 - 09:17 AM

Your eyes are much better at scanning code than a debugger is at tracing through to just the right spot.


Definitely not always the case.  There have been countless times where I've made a typo so innocuous that I've overlooked it time and time again before firing up a debugger which has shown me the problem nearly instantly.


Are debuggers a good replacement for thinking about the problem? - definitely not.  And neither are profilers.  But both, when used effectively and properly can increase your efficiency dramatically.  Really there's no good reason to not use them, and citing not having them when you started programming is certainly not a good reason.  They didn't exist when I started coding either, but I'm certainly glad they do now.

#5236922 delete file on Windows

Posted by LennyLen on 26 June 2015 - 08:33 AM

Thanks, that's what I thought as \ is escape on Unix. Still this doesn't work:


InFileLocHw.replace(InFileLocHw.find("\\\"), 1, "\\");


That's because of the "\\\" part.  Remove one of the slashes. 

#5236763 sprintf

Posted by LennyLen on 25 June 2015 - 11:09 AM

95% sure this works, have to check my code if it doesn't. Let me know


It will work, but using stringstream (or snprintf() at least) is safer.

#5235001 Why didn't somebody tell me?

Posted by LennyLen on 15 June 2015 - 09:28 PM


I'm always surprised when I discover there are adults who are just finding these things out.  But I guess it's because I was quite anti-social as a child,

Anti-social means attacking social norms - kicking guests in the shins.

Asocial means not engaging in social norms - hiding from guests. tongue.png



I did both. ;)

#5234998 Why didn't somebody tell me?

Posted by LennyLen on 15 June 2015 - 09:18 PM

Someday I really hope to figure these out.


It's not "at their beckon call" but "at their beck and call."  


Beck?  Really?  It's confusing because beck meant beckon in the 1800's.  Ah English.


I'm always surprised when I discover there are adults who are just finding these things out.  But I guess it's because I was quite anti-social as a child, and spent most of my time reading rather than talking to people, so I came across these phrases in writing rather than speech.

#5225314 Steam's compensated modding policy

Posted by LennyLen on 24 April 2015 - 02:27 PM

Especially Valve's? If anybody I would expect it would be the developer of the game itself would would care, not Valve. I see no reason why Valve should "step in" here. There doesn't seem to be anything to "step in" about.


Valve are a retailer, and as such, they should be expected to care about the products they sell (Nb: I do believe the original developers are the ones who should care the most).  I'm sure Valve do not want to garner a reputation for selling poor quality mods any more than developers want to gain a reputation for allowing poor quality mods for their games.

#5220447 Trial by Viking - WIP

Posted by LennyLen on 31 March 2015 - 04:11 AM

Good looking game! Visually, it reminds me of Gods.

#5220422 Starting A Company From Scratch - With no Money, Friends, or Education...

Posted by LennyLen on 31 March 2015 - 12:16 AM

$25/hour maybe?  I make more than that in a non-technical field.

#5216767 GUI Ideas for a Tower Defense Game

Posted by LennyLen on 15 March 2015 - 09:12 PM

Attached is a picture how my TD looks like..




One important piece of information you have left out is whether your game is a mobile game or a PC game.  A UI that works well for one doesn't always work well for the other.

#5205278 why C++?

Posted by LennyLen on 19 January 2015 - 07:22 AM

what is C++ i see it on many places but nower to be found what it is.


I hope this doesn't come across too harsh, but if you want to be a programmer, then you need to get a LOT better at researching information.  It's an extremely important skill for a programmer, and most of the information you will need to find on an everyday basis will be more difficult to locate than the answer to your question here.

#5203655 how make games in c++

Posted by LennyLen on 12 January 2015 - 07:04 AM

I also suggest you read this.