Thread: A begginer writing a text based dungeon game.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    29

    A begginer writing a text based dungeon game.

    This will be the first of many questions to come
    This is an assinment so dont give me code i won't understand becouse i wont use it, even if you give me code i understand i still won't use it but i will learn from it.

    in this code i am trying to get a specific number to print out when the user types n, e, s, or w.
    for example: n would be 1, and e would be -1.
    all it dose is give me some strange error called signal 10 (sigbus) then it pulls up a debugger window sorry if this sounds to common it's never happend to me before.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
    int room0[4]={1,2,-1,-1};
    char i;
    
    char n=0;
    char s=1;
    char e=2;
    char w=4;
    
    
    
    printf("number...err letter:");
          scanf("%c\n",i);
    
    printf("print the god dam numb. %i",room0[i]);
    
    
    	
    
    	return 0;
    }


    please before reading whats below, can you answer the above question first even if it will not help me with the game, it's how i learn things,i am very new at this got no experience at all.

    as for the text base game this is how its planned, so far i am planning to have 4 rooms and am writing the code in 4 parts, 1 for each room it sounds tedius but i just want to get from 1 room to the other for right now. the rooms have numbers 0-3, you get from each room by pressing n,s,e,w. if you try to go the wrong way its a -1. i am thinking the use of arrays, if else statements and goto statment. if there is more things i should or not use don't be shy speek up.

    and now for my stupid questions

    1)can you get an array of letters to equal an array of numbers.
    i guess that will be it for now i will post more questions as soon as they become more prevalent to the main question.

    lastly you are all probably wondering what text i am using, well i am using Programming in C third edition Stephen G. Kochan Developer's Library

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    scanf("%c\n",&i);

    so how are you going to change that code to your code?
    Quote Originally Posted by deadherorising View Post
    1)can you get an array of letters to equal an array of numbers.
    what do you mean?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    29
    Quote Originally Posted by robwhit View Post
    scanf("%c\n",&i);

    so how are you going to change that code to your code?

    what do you mean?
    thanks, first question: i am not sure what your talking about. i hope i did it.
    but i changed the code a little. now for some resign every thing equals 1
    and i changed every thing to int values.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {
    int room0[4]={1,2,-1,-1};
    int i;
    int g=room0[i];
    int n=0;
    int s=1;
    int e=2;
    int w=4;
    
    printf("number...err whatever:");
    scanf("%i\n",&i);
    
    printf("print the god dam numb. %i",g);
    
    
    	
    
    	return 0;
    }


    and for my stupid question,
    i mean somthing like this

    int room[4]={1,2,3,4}
    int die[4]={'n','s','e','w'}

    int die[]=room[]

    and now for my second stupid question
    2) can i make an array of letters equal to letsay 0-3 using the for loop.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    int room0[4]={1,2,-1,-1};
    int i;
    int g=room0[i]; //i is not initialized.
    > int die[]=room[]
    what would this do?
    > 2) can i make an array of letters equal to letsay 0-3 using the for loop.
    you mean an array of chars?

    char arr[4] = {0,1,2,3};
    Last edited by robwhit; 10-26-2007 at 10:08 PM.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    29
    Quote Originally Posted by robwhit View Post
    Code:
    int room0[4]={1,2,-1,-1};
    int i;
    int g=room0[i]; //i is not initialized.
    > int die[]=room[]
    what would this do?
    > 2) can i make an array of letters equal to letsay 0-3 using the for loop.
    you mean an array of chars?

    char arr[4] = {0,1,2,3};
    what do you mean i is not initialized i is initilized right above it with int i;

    1) int die[]=room[], i was hoping they would equal each other i guess not, dam.

    2) ya an array of chars!
    "char arr[4]={0,1,2,3}"
    i thought that would just ment that there are 4 places 0=0 1=1 2=2 3=3.
    i want in this case n=0 s=1 e=2 w=3.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > what do you mean i is not initialized i is initilized right above it with int i;
    that's declared. int i=5; is initialized.
    >1) int die[]=room[], i was hoping they would equal each other i guess not, dam.
    nope, have to do a loop to do that.
    > i want in this case n=0 s=1 e=2 w=3.
    sounds like you want a map.

    like this?

    arr[n] == 0
    but not
    arr[0] == n

    there's not an easy way to do maps in C.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    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.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    29
    Quote Originally Posted by Salem View Post
    > 1000 x 1000 x 30
    30Mbytes of storage (at least).

    But that's just the tip of the iceberg, you've got to initialise all those locations with meaningful data, unless you have a lot of

    N
    There is ocean here
    N
    There is ocean here
    N
    There is ocean here
    N
    There is ocean here
    N
    There is ocean here

    Not only that, no user will ever visit every location in your RPG, it would simply take too long (1 room per second for almost a year, non-stop)

    Your RPG will have at most a few hundred locations (that are interesting in some way). I suggest you store the connectivity of the locations within the data, then you only need to store the locations which interest you

    Perhaps something like this
    Code:
    enum { NONE, KITCHEN, HALL, STAIRS };
    enum { NORTH, SOUTH, WEST, EAST };
    
    struct room {
        char    *name;
        int      next[4];   // N,S,W,E from this location
    } rooms[] = {
        { "None" },
        { "Kitchen",    { NONE,    HALL,   NONE, NONE } },
        { "Hall",       { KITCHEN, STAIRS, NONE, NONE } },
        { "Stairs",     { HALL,    NONE,   NONE, NONE } },
    };
    If you have
    int room_num = KITCHEN;

    Moving south is just
    room_num = rooms[room_num].next[SOUTH];
    humm interesting i see some things but i deont see others like
    were dose the north south east west become initilized, is he even using them, or is he just using the hall ,kitchen and stairs as the directions.

    still this dose not answer my question, how do i make a variable equal to the index.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Separate your data from your code.

    Each room contains up to 4 directions which lead to other rooms. You can have as many rooms as you like.

    If you want, you can add "up" and "down" to the possible directions, so that stairs for example do the right thing.

    > or is he just using the hall ,kitchen and stairs as the directions.
    No, they're places at the destination of moving in a direction.

    Design your map, design your data structures, read from a file.
    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.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    29
    so i should get all the numbers to work first and add all the data first than worry about adding the north south east weast, and other descriptions later. ok thanks

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    29

    ok ok ready for another big step.

    ok heres what i got you have 4 directions for the index if you try to type in something higher than whats inside the array my message will pop up and the program will end,

    Q:what do i type to get the program to start over again from the top ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    # define direct_max 3
    int main(void)
    {
    int room0[direct_max]={1,2,-1,-1};
    int i;
    
    printf("number please, later it will be direction:");
    scanf("%i",&i);
    
    if (i>direct_max)
    	{
    	printf("you have to type n,s,e,w for you directions nothing else will work.");
    	return 1;          /// i have it end here if you dont type 0,1,2,3. 
    	}
    		
    	printf("print the god dam numb. %i",room0[i]);  
                ///it prints the number corresponding the array.
     
    
    	return 0;
    }

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    I would suggest you create a fairly simple map, and a fairly simple design and have a go at implementing it.

    This will prepare you for your "big idea" later on.

    Being able to design and implement large programs is a whole new level above simply being able to get the curly braces in the right place.
    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.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    29
    okay this is my very simple map it consist of one room and 4 directions you can use but you cant go any were. right now the directions are in number form. so how do i make it so when you type the wrong number it asks for a number again.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    29

    Kick Ass

    ok heres what i got :

    right now the user types a letter and it prints out the index number then it prints out one of the numbers in the array. but i am getting more than what i wanted. this is what it actually dose.

    direction n,s,e,w: n <---user prints letter in this case i printed an "n"
    no type n,s,e,w
    no type n,s,e,w
    no type n,s,e,w
    index number please:0
    print the god dam room numb. 1
    assinment 4 has exited with status 0.

    it prints the right number but it keeps on going with the else statments.
    first question

    1) how do i make it continue to ask me for the direction when i imput a letter that is not n,s,e or w.

    2)how do i stop the repeating "no type n,s,e,w" from the else statment.

    3) and if i wanted to end the program when the user typed end, using an if statment what do i put in the statment.




    Code:
    #include <stdio.h>
    #include <stdlib.h>
    # define direct_max 4
    int main(void)
    {
    int room0[direct_max]={1,2,-1,-1};
    int i;/*number*/
    char a;/*letter*/ 
    
    
    printf("direction n,s,e,w:");
    scanf("%c",&a);
    
    	if(a=='n')
    		{
    		i=0;
    		}
    		else
    			{
    			printf("no type n,s,e,w\n");
    			}
    	if(a=='s')
    		{
    		i=1;
    		}
    		else
    			{
    			printf("no type n,s,e,w\n");
    			}
    	if(a=='e')
    		{
    		i=2;
    		}
    		else
    			{
    			printf("no type n,s,e,w\n");
    			}
    	if(a=='w')
    		{
    		i=3;
    		}
    		else
    			{
    			printf("no type n,s,e,w\n");
    			}
    			
    			
    printf("index number please:%i\n",i);
    
    
    
    		
    	printf("print the god dam room numb. %i",room0[i]);
    
    
    	return 0;
    }

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    You're still writing game data into the code.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    enum { NONE, KITCHEN, HALL, STAIRS };
    enum { NORTH, SOUTH, WEST, EAST };
    
    struct room {
        char    *name;
        int      next[4];   // N,S,W,E from this location
    } 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
    $
    Everything about the room,
    - the description
    - the ways out
    - any secrets
    - any objects
    - any creatures
    need to be encoded into the data in some way, and then acted upon by the code.

    For my code, adding another room is dead easy, but if all the directions are hard-wired into the code (as per your example), then you're looking at rewriting the code every time you make a small change to the map.
    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. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  2. OpenGL - 2d text in 3d game
    By mikeb1986 in forum C++ Programming
    Replies: 1
    Last Post: 03-22-2006, 01:24 PM
  3. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM