Thread: Editable field in basic C game. I'm stuck.

  1. #1
    Registered User
    Join Date
    Apr 2012
    West London

    Editable field in basic C game. I'm stuck.

    Hi, i'm a student with a simple C project, unfortunately my understanding of C is just as basic.

    We have to create an interactive floor plan navigable in an .exe file. I have the main functionality working, but am trying to add a way for the user to input their name, and for the program to then refer to it throughout the navigation.

    So, when entering the game it would ask "what is your name?"
    and the jump to "welcome to the house [let's assume they wrote] Thomas"
    and continue to call them thomas in each room

    here is the code I have so far, any advice would be much appreciated;

    #include <stdio.h>
    #include <stdlib.h>
    typedef struct {
        char Name[70];
        char Description1[100];            //gives the first line of descriptions, up to 70 characters
        char Description2[70];            
        char Description3[70];            
        int Exit[4];                    //makes a possible of 4 different exits (N/S/E/W)
    Room House[9];                      //there are 9 rooms in the house
    void DescribeRoom (int RoomNumber)
        // Show Current Room Name
        printf("ROOM:\n    %s\n", House[RoomNumber].Name);
        // Show Current Room Description
        printf("A BRIEF DESCRIPTION:\n    %s\n    %s\n    %s\n", House[RoomNumber].Description1, House[RoomNumber].Description2, House[RoomNumber].Description3);
        // Show available exits for current room
        printf("AVAILABLE DOORS:\n");
        if (House[RoomNumber].Exit[0] != -1)
        //using printf the room number onto the screen
            printf("    North to %s\n", House[House[RoomNumber].Exit[0]].Name);    
        if (House[RoomNumber].Exit[1] != -1)
            printf("    South to %s\n", House[House[RoomNumber].Exit[1]].Name);
        if (House[RoomNumber].Exit[2] != -1)
            printf("    East to %s\n", House[House[RoomNumber].Exit[2]].Name);
        if (House[RoomNumber].Exit[3] != -1)
            printf("    West to %s\n", House[House[RoomNumber].Exit[3]].Name);
    int MoveRoom (int RoomNumber)
        char Direction[2];
        char NotUsed;
        int ExitNumber;
        // Give the user a choice of direction to move
        printf("\nChoose a direction (N, S, E or W) or press esc to leave:\n");
        scanf ("%s", &Direction);    //scanf to see what direction user has inputted
        // Check to see if exit number has been pressed (0)
        if(strcmp(Direction, "0") == 0)
            printf("Come back whenever. We'll put the kettle on");
            exit (0);
        // Converting direction choice in the exit number
        else if (strcmp(Direction, "n") == 0 || strcmp(Direction, "N") == 0)
            ExitNumber = 0;
        else if (strcmp(Direction, "s") == 0 || strcmp(Direction, "S") == 0)
            ExitNumber = 1;
        else if (strcmp(Direction, "e") == 0 || strcmp(Direction, "E") == 0)
            ExitNumber = 2;
        else if (strcmp(Direction, "w") == 0 || strcmp(Direction, "W") == 0)
            ExitNumber = 3;
        // If valid change Room direction has been input Describe new Room
        if (House[RoomNumber].Exit[ExitNumber] == 0)
        //error code,if no exit avalible
            printf("\You just walked into a wall. Try the door.\n");        
            return RoomNumber;
            return House[RoomNumber].Exit[ExitNumber];
    //room names and descriptions
    void SetupRooms ()              
        int Location = 1;
        // Setup Room; West Hallway
        strcpy(House[1].Name, "West Hallway");
        strcpy(House[1].Description1, "Welcome to the house");
        strcpy(House[1].Description2, "You're stood in the Hallway.");
        strcpy(House[1].Description3, "You can leave again if you press 0");
        strcpy(House[1].Description3, "");
        House[1].Exit[0] = 0;
        House[1].Exit[1] = 3;
        House[1].Exit[2] = 8;
        House[1].Exit[3] = 2;
        // Setup Room; Office
        strcpy(House[2].Name, "Office");
        strcpy(House[2].Description1, "This is where the work is done");
        strcpy(House[2].Description2, "It's also the technical hub of the house, with all the gadgetry");
        House[2].Exit[0] = 0;
        House[2].Exit[1] = 0;
        House[2].Exit[2] = 1;
        House[2].Exit[3] = 0;
        // Setup Room; Downstairs Toilet
        strcpy(House[3].Name, "Downstairs Toilet");
        strcpy(House[3].Description1, "Not an overly exciting room; there's a toilet and a sink");
        strcpy(House[3].Description2, "There's also an 'Encyclopedia of Everything' on the windowsill");
        strcpy(House[3].Description2, "");
        House[3].Exit[0] = 1;
        House[3].Exit[1] = 0;
        House[3].Exit[2] = 0;
        House[3].Exit[3] = 0;
        // Setup Room; Kitchen & Dining Room
        strcpy(House[5].Name, "Kitchen & Dining Room");
        strcpy(House[5].Description1, "Welcome to the heart of the home");
        strcpy(House[5].Description2, "Large open-plan room seperated by a kitchen counter");
        strcpy(House[5].Description3, "My baby brother's playpen is in the corner, by the dining table");
        House[5].Exit[0] = 0;
        House[5].Exit[1] = 8;
        House[5].Exit[2] = 9;
        House[5].Exit[3] = 0;
        // Setup Room; Bathroom
        strcpy(House[4].Name, "Bathroom");
        strcpy(House[4].Description1, "You are now in the Bathroom");
        strcpy(House[4].Description2, "A really nice powerful shower is in the corner, with the bath itself directly in front of you");
        House[4].Exit[0] = 8;
        House[4].Exit[1] = 0;
        House[4].Exit[2] = 0;
        House[4].Exit[3] = 0;
        // Setup Room; Small Living Room
        strcpy(House[6].Name, "Small Living room");
        strcpy(House[6].Description1, "Now you're in the original living room");
        strcpy(House[6].Description2, "This was the main living room before we had the extension");
        strcpy(House[6].Description3, "Now the fireplace has been knocked through on both sides, and you can see into the Large living room through the fire =- it's awesome");
        House[6].Exit[0] = 8;
        House[6].Exit[1] = 7;
        House[6].Exit[2] = 0;
        House[6].Exit[3] = 0;
        // Setup Room; Big Living Room 
        strcpy(House[7].Name, "Big Living Room");
        strcpy(House[7].Description1, "You've left the Small Living room, and now you're in the Big Living room");
        strcpy(House[7].Description2, "This is where the TV lives, and a really comfy sofa");
        strcpy(House[7].Description3, "A huge floor-to-ceiling bookcase covers one wall.");
        House[7].Exit[0] = 6;
        House[7].Exit[1] = 0;
        House[7].Exit[2] = 0;
        House[7].Exit[3] = 0;
        // Setup Room; East Hallway
        strcpy(House[8].Name, "East Hallway");
        strcpy(House[8].Description1, "Now you're in the East Hallway");
        strcpy(House[8].Description2, "It forms the second part of an internal ring around the house");
        strcpy(House[8].Description3, "The 8-year old cousins love to do laps of it");
        House[8].Exit[0] = 5;
        House[8].Exit[1] = 6;
        House[8].Exit[2] = 4;
        House[8].Exit[3] = 1;
             // Setup Room; Utility Room
        strcpy(House[9].Name, "Utility Room");
        strcpy(House[9].Description1, "This is an ex-cow shed brick out-building");
        strcpy(House[9].Description2, "It is seperate from the main house, and holds the booze fridge");
        House[9].Exit[0] = 0;
        House[9].Exit[1] = 0;
        House[9].Exit[2] = 0;
        House[9].Exit[3] = 5;
        // Clear Screen
        // Discription of current room
        // Go to a new room
            Location = MoveRoom(Location);
    void main ()
    I think it's all fairly ok, but if there are any glaring problems I'd really appreciate it if anyone could tell me why it's wrong, and help me to fix/understand it.

    many thanks,


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > strcpy(House[9].Name, "Utility Room");
    Like your friend, you have a tendency to overrun your arrays.
    You declares rooms[9], which means you have rooms[0] to rooms[8] to play with.

    > if (House[RoomNumber].Exit[0] != -1)
    Elsewhere in your code, you use 0 for a non-exit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stuck on a game of craps in C
    By Rob31645 in forum C Programming
    Replies: 3
    Last Post: 02-08-2012, 01:28 AM
  2. stuck on card game in C. any suggestions?
    By ihatec++ in forum C Programming
    Replies: 8
    Last Post: 03-19-2010, 11:04 AM
  3. Replies: 3
    Last Post: 09-06-2009, 01:48 PM
  4. Replies: 2
    Last Post: 06-07-2009, 08:49 AM
  5. Can anyone Field a Visual Basic Question?
    By Eber Kain in forum Windows Programming
    Replies: 2
    Last Post: 06-09-2004, 10:50 AM

Tags for this Thread