A begginer writing a text based dungeon game.

This is a discussion on A begginer writing a text based dungeon game. within the C Programming forums, part of the General Programming Boards category; ok heres what i got you have 4 directions for the index if you try to type in something higher ...

  1. #16
    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;
    }

  2. #17
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #18
    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.

  4. #19
    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;
    }

  5. #20
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #21
    Registered User
    Join Date
    Oct 2007
    Posts
    29

    i hate you, i hate all of you.

    this is as close to "not code" that i can get i think, i dont know, your program dosent even look like it works. i was show to make a program that works then add on from there with more programs that work. so i chucked everything i learned out the window and made a program that dose not work.

    there are 4 rooms you are in room 0 you have to make the decision to go to rooms 1 or 2 and thats it, i dont know how to make any thing continue and\or asking again.

    i dont know what i am doing any more?so confused?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    # define map_max 4
    
    
    int main(void)
    {
    
    
    int map[3][map_max]={
    				{1, 2,-1,-1},
    				{2, 0,-1,-1},
    				{0, 1,-1, 3},
    				{-1,-1,2,-1}
    				};
    printf("you are in room &#37;i",0)
    
    	directions.c   /// this is the program i have been working on all through out 
                                    this post, i did some small changes like implementing the  
                                   matrix and changed  the words, but it still dose the same                             
                                      thing, so its still have the same problems.
    	
    	if(i==-1){
    	printf("up yours")
    		else{
    		printf("you are in room %i",i)
    				
    	return 0;
    }
    Last edited by deadherorising; 10-28-2007 at 01:13 PM.

  7. #22
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > your program dosent even look like it works
    So did you actually try it, or are you sticking to your approach of code everything.

    I guess if the best you can do is insult the people who try to help you then we're better off not caring whether you pass or fail.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #23
    Registered User
    Join Date
    Oct 2007
    Posts
    29
    I talked to some people and i understand the code now, but i am not quite at that sort of lvl, and i prefer to work with numbers, i tend to get easily confused with words.
    Last edited by deadherorising; 10-29-2007 at 11:08 AM.

  9. #24
    Registered User
    Join Date
    Oct 2007
    Posts
    29

    gakkitwias

    ok i have started completely over now i am using functions, i know its code just bear with my incompetence and help me.

    here is my room description code, i cant seem to get it to work keeps on giving me errorarse error befor 'return' i dont get it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    void script (int room_num){
    
    
    
    if (room_num=0){
    printf("room0");
    	}
    if (room_num=1){
    printf("room1");
    	}
    if (room_num=2){
    printf("room2");
    	}
    if (room_num=3){
    printf("room3");
    	}
    if(room_num=-1){
    printf("ass");
    	}
    }
    
    
    
    int main (void)
    
    {
    script(0);
    
    
    return 0;
    }

  10. #25
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by deadherorising View Post
    ok i have started completely over now i am using functions, i know its code just bear with my incompetence and help me.

    here is my room description code, i cant seem to get it to work keeps on giving me errorarse error befor 'return' i dont get it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    void script (int room_num){
    
    
    
    if (room_num=0){
    printf("room0");
    	}
    if (room_num=1){
    printf("room1");
    	}
    if (room_num=2){
    printf("room2");
    	}
    if (room_num=3){
    printf("room3");
    	}
    if(room_num=-1){  
    /* remember, one = means ASSIGNMENT, you need two == for an equality test.  All the above should have two =='s, OK? */
    printf("ass");
    	}
    }
    
    
    
    int main (void)
    
    {
    script(0);
    
    
    return 0;
    }
    What you're trying to do is quite hard, actually. Let me try and express my thought, in an analogy:

    You have an elephant who has become too mean/sick/whatever to keep. You know that the best thing is to take the elephant to the slaughter-house, before he kills somebody.

    What you're doing now, is butchering the elephant in the back yard, bringing in every piece of meat and bone to your kitchen sink and cleaning it all up and wrapping it, and then putting it on your back and carrying it down to the slaughterhouse.

    Back and forth, maybe 50 or 100 trips, trudging the meat to the slaughterhouse.

    What you want to do with your program, is simply have the elephant walk on his own power, to the slaughter-house.

    Lousy analogy, but the thing is, it's all about doing this the ez way. You're doing it a VERY hard way. Practically guaranteed to rattle your nerves, and make your hair turn grey!

    I know it just SEEMS right, to start with that knife in your hand, killing the elephant, and cutting up the meat - but that is SO wrong.

    That's what Salem was trying to tell you. There's a MUCH easier way to do this program, and it doesn't SEEM easier to you, because you're there with your knife, ready to cut away. But listen, and work outside your comfort zone, and be patient - you won't have this done in a day, no matter how much you get worked up.

    You can't become a good programmer, in a week.

    If you make a map that you want for your RPG, then it becomes much easier to work toward that goal:

    Write a little pseudo code for what you think the program will need.

    Now divide your functions up, like: display main menu, initialize new game board, show a room, etc.

    Now the pseudo code can be "added to and filled in", to get your game code off to a good start. Only write one function at a time while testing, so you're never left with a zillion lines of code, and zillions of errors, from the compiler.

    Meter by meter, code is sweeter - trust me.

    Your game will come along just fine, but more importantly, you'll learn a lot that you can use on EVERY program you code up.

    A better analogy: when you want to lead a horse to water, you grab the horse's bridle and lead it, you don't pull it mightily backwards by the tail. (unless you want to get kicked).

    And put OFF all the details, while you're doing this. You want to work from the top DOWN - and later, put in the details from the bottom up to fill in the "chinks". Right now, "Put off the details". You don't give a hoot about 'em, right now. The design should go from your idea's, DOWN to your map, DOWN to your pseudo code, and then DOWN to your individual functions.

    ONLY then do you work with any actual code (unless you already know the code you need by heart).

    Taking your idea's and trying to put them directly into code, is very hard for most people, and almost impossible for beginners. It's also just the WRONG way to approach a new program.
    Last edited by Adak; 10-30-2007 at 02:28 AM.

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    deadherorising,

    Which of these two is more clear what the code does:

    Code:
    if (direction == 1) ... 
    
    if (weapon == 3) ... 
    // or
    if (direction == NORTH) ... 
    
    if (weapon == KNIFE) ...
    If you use symbolic names, it makes it much easier to understand what the code does.

    It's easy to hold a simple set of numbers in your head when they are just one or two pages up on the screen, but I can assure you that your code will become large enough soon enough that you can't remember whether 1 meant north, south, east or west. And of course, in another place, 3 might mean "the knife", which will be utterly confusing after a while.

    I know - and it's because I've done it myself.

    Use symbolic names, rather than numbers, and you'll definitely help yourself get the code to work better.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    Registered User
    Join Date
    Oct 2007
    Posts
    29

    dont think me as rude i am not ignoring you advise i just incapable to use it.

    heres what i got, i am having a rough time trying to figure out how to make a multidimensional array function. so could you tell me how i am screwing up or give me an example of a correct one. the finction next_room is what it is called and what i want iit to do is spit out the matrix number.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    # define max_room 4
    # define max_go 4
    
    
    
    void script (int room_num){
    
    if (room_num==0){
    	printf("room0");
    	}
    if (room_num==1){
    	printf("room1");
    	}
    if (room_num==2){
    	printf("room2");
    	}
    if (room_num==3){
    	printf("room3");
    	}
    if(room_num==-1){
    	printf("ass");
    	}
    }
    
    
    
    
    int next_room (int room, int direction,int map[][]){
    	int map[room][direction]={
    					{1, 2,-1,-1},
    					{2, 0,-1,-1},
    					{0, 1,-1, 3},
    					{-1,-1,2,-1}
    				};
    				return map[room][direction];
    				}
    				
    				
    				
    int conversion (char letter){
     
    
    		if(letter=='n')
    		{
    		letter=0;
    		}
    		
    	else if(letter=='s')
    		{
    		letter=1;
    		}
    		
    	else if(letter=='e')
    		{
    		letter=2;
    		}
    	
    	else if(letter=='w')
    		{
    		letter=3;
    		}
    		else
    			{
    			printf("No!, After wandering for hours you figure out %c is not a actual direction. You've walked in to the slabbering fangs of a lurking Grue\n", letter);
    			return 0;
    			}
    
    	return letter;
    }
    
    int main (void)
    {
    char letter;
    printf("direction:");
    	scanf("%c",&letter);
    	
    
    printf("%i",);
    
    script (nextroom(room,conversion(letter)));
    
    
    return 0;
    }

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    surely the map doesn't change:
    Code:
    int next_room (int room, int direction,int map[][]){
    	int map[room][direction]={
    					{1, 2,-1,-1},
    					{2, 0,-1,-1},
    					{0, 1,-1, 3},
    					{-1,-1,2,-1}
    				};
    so could be made "const static" so that the compiler doesn't have to generate code to initialize a local variable with the same value each time. Or make it a "const" global variable. [If your rooms occassional "fall apart" or "get blocked", you obviously can't make it const!]
    The array itself looks fine to me.

    Also, you are "hiding" map with a local variable. You are using "room" and "direction" as sizes for your local map are variables - that is not valid in standard C, and will most likely not work right for the .

    Code:
    script (nextroom(room,conversion(letter)));
    What's nextroom() - the function above is called next_room - and it takes three parameters [although I don't see why it needs to].

    Code:
    printf("%i",);
    I doubt this will compile at all - it's missing a parameter after the comma.


    Code:
    int conversion (char letter){
     switch(tolower(letter)) {
        case 'n':
           return 0;
        case 's':
           return 1;
        case 'e':
           return 2;
        case 'w':
           return 3;
        default:
    	printf("No!, After wandering for hours you figure out %c is not a actual direction. You've walked in to the slabbering fangs of a lurking Grue\n", letter);
    	return 0;
        }
    }
    Isn't that a bit easier to read? [Not sure if you really want to move north after that - perhaps you should return -1, separate out the conversion, and only move if the number is greater or equal to zero [and less than 4]?


    In summary, your code doesn't work, but I honestly don't see much wrong with your array itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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

    confusing is my middle name

    I cant figure this out, i am not quit sure i understand what your saying about my next_room function, maby an example would help, i am a very visual learner, oh and yes the map is unchanging.

    i tryed some diffrent ways but none seem to work i must going at this in the wrong way.

    Code:
    int next_room (int room, int direction){
    	int map[room][direction]={
    					{1, 2,-1,-1},
    					{2, 0,-1,-1},
    					{0, 1,-1, 3},
    					{-1,-1,2,-1}
    				};
    				return map[room][direction];
    				}
    or

    Code:
    int next_room (int map[][]){
    	int map[room][direction]={
    					{1, 2,-1,-1},
    					{2, 0,-1,-1},
    					{0, 1,-1, 3},
    					{-1,-1,2,-1}
    				};
    				return map[room][direction];
    				}
    or

    Code:
    # define max_room 4
    # define max_die 4
    .
    .
    .
    int next_room (map[room][directions]){
    	int map[max_room][max_die]={
    					{1, 2,-1,-1},
    					{2, 0,-1,-1},
    					{0, 1,-1, 3},
    					{-1,-1,2,-1}
    				};
    				return map[room][direction];
    				}
    edit:
    or ///this one has one error, parse error "=".
    Code:
    # define max_room 4
    # define max_go 4
    
    
    int next_room (int a, int b, int map[max_room][max_go]={
    					{1, 2,-1,-1},
    					{2, 0,-1,-1},
    					{0, 1,-1, 3},
    					{-1,-1,2,-1}
    				}){
    	
    				
    				}
    or have i forgotten my brains today.
    Last edited by deadherorising; 11-04-2007 at 09:33 AM.

  15. #30
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't understand the purpose of the next room function.

    If you want to change the original array, just use the name of the array as it's address, and Bob's your Uncle. If you want to change the whole array, you can't re-initialize it with values - that only works the first time you create the array. You might want to use a local array or read the new values into the array, from a file.

    I've never actually SEEN anybody create a new array and initialize it as part of a function parameter. Seems dodgy to me.
    Last edited by Adak; 11-04-2007 at 04:35 PM.

Page 2 of 4 FirstFirst 1234 LastLast
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, 12:24 PM
  3. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-21-2006, 11:23 PM
  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, 06:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21