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; Code: char menu(void){ char x; printf("\nyou want to do this y, n:"); scanf("%c",&x); return x; } int main (void) { ...

  1. #46
    Registered User
    Join Date
    Oct 2007
    Posts
    29
    Code:
    char menu(void){
    char x;
    printf("\nyou want to do this y, n:");
    	scanf("%c",&x);
    	return x;
    	}
    
    int main (void)
    {
    char play;
    
    while(1) {
    	play=menu();
    		if(play=='n')
    			break;
    			}
    
    return 0;
    }
    theres no diffence i just added the \n

  2. #47
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't you have at least #include <stdio.h> , etc., at the top of your program?

  3. #48
    Registered User
    Join Date
    Oct 2007
    Posts
    29
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    char menu(void){
    char x;
    printf("\nyou want to do this y, n:");
    	scanf("%c",&x);
    	return x;
    	}
    
    int main (void)
    {
    char play;
    
    while(1) {
    	play=menu();
    		if(play=='n')
    			break;
    			}
    
    return 0;
    }
    ya thats in there just missed coping it.

  4. #49
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is what you want for the menu logic, I believe. Note that it will not play a game until it gets the game playing function call added into the menu function.


    Code:
    #include <stdio.h>
    
    char menu(void) {
       char yesno;
    
       printf("\nyou want to do this? (y/n): ");
       scanf("&#37;c", &yesno);
    
       return yesno;
    }
    
    int main (void) {
       char play;
       printf("\n\n\n\n\n\n");
    
       while(1) {
          play=menu();
          getchar();  /* removes newline from input buffer 
          This is a problem with scanf(), which is why it's not used much.
          Next time you look at this program, ask about fgets() - it is the
          only good way to get input from a user in C, but slightly too
          complicated for right this second. */
    
    	   if(play =='n')
    		   break;
          /* else
             new_game();  game playing function call, goes here */
       }
    
       printf("\n\n  Program Complete - Press Enter ");
       getchar();  /* wait for the key press */
    
       return 0;
    }
    Last edited by Adak; 11-05-2007 at 11:45 PM.

  5. #50
    Registered User
    Join Date
    Oct 2007
    Posts
    29
    nice it worked thanks, now if i put it all together

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    void script (int room_num){
    
    if (room_num==0){
    	printf("\nroom0");
    	}
    if (room_num==1){
    	printf("\nroom1");
    	}
    if (room_num==2){
    	printf("\nroom2");
    	}
    if (room_num==3){
    	printf("\nroom3");
    	}
    if(room_num==-1){
    	printf("\nthe walls are acid");
    	return 0;
    	}
    }
    
    int next_room(unsigned int a, unsigned int b)
    {
        static const int map[4][4] =    {   {1, 2,-1,-1},
                                            {2, 0,-1,-1},
                                            {0, 1,-1, 3},
                                            {-1,-1,2,-1}
                                        };
    				
    			return map[a][b];
    }
    
    int conversion (char letter){
     switch(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 and end up walking into the slabbering fangs of a lurking Grue.\n", letter);
    	return 4;
        }
    }
    
    char menu(void){
    char x;
    printf("\nyou want to do this y, n:");
    	scanf("%c",&x);
    	return x;
    	}
    
    \\\so i would be adding another function here? like..
     int game(int start _room);{
        int start_room=current_room;
    return current_room
    }
    
    
    int main (void)
    {
    char letter;
    char play;
    int current_room;
    
    while(1) {
    	play=menu();
    	getchar();
    		if(play=='n')
    			break;
    			
    	\\\then  i add int game here like so:  the int game function updates the current room from the first loop to the second. i hope i am doign it right.
    int game(current_room);
          \\\or do i add another while loop within the first one?
    		
    printf("direction:");
    	scanf("%c",&letter);
    	
    int letter_num=conversion(letter);
    int room_num=(next_room(current_room,letter_num));
     if(letter_num==4)
    	break;	
    		
    script (room_num);
    }
    return 0;
    }

  6. #51
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Usually, you have an outer "menu & game loop" that you go through once only, for each complete game (or until the player presses the Q to quit).

    Then, inside the functions that the outer loop calls, will be other loops that might address the player's movements from room A to room B, then back to room A, then room C, whatever.

    These secondary loops should be in the next level down from the main "menu & game" loop. You want to distribute the loops so there's no big headache to debug them, later.

  7. #52
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Two considerations:
    1. Using the "number of choices + 1" as indication of failure is not a particularly good plan - especially if it's a "magic number" [just a number, rather that a constant declaration]. Using either an "impossible number" or a "hugely outside the range" number is a much better plan. So don't use 4 as your indication of "you pressed the wrong key", but perhaps either -1 or say 1000. What happens if you feel like implementing further directions, such as up, down, NorthWest, SouthEast, etc, etc.

    2. The getchar() to get rid of the newline should really be part of "menu" rather than stuffed after menu() - this is because it fixes a side-effect of the actual reading of the choice using scanf() - if at some future point, you change it to use for example fgets(), then you don't need the getchar(). It is also bad style to rely on some other part of the code to clean up after a particular function - if it's at all possible, the function should make sure that it exits in a "clean" state.

    3. You probably also want this:
    Code:
    printf("direction:");
    	scanf("&#37;c",&letter);
            getchar();
    as it suffers from the same thing as your "menu()".

    4. This can end up "out of range":
    Code:
    int room_num=(next_room(current_room,letter_num));
     if(letter_num==4)
    	break;
    It letter_num is 4 in the above line, next_room will be called with a 4, which is out of bounds for the array, and there is no checking for that. The red parenthesis are un-necessary. I'm all for extra parenthesis when they clarify the code, but I don't see these doing that.

    5. Declaring local variables "as you need them" [as in #4 above] is a C++ technique and non-standard in "plain" C. It's better style to declare the variables in the beginning of the function.

    Please don't take this criticism as "you are crap", but rather as "this is things you can improve on, and learn from, so that you become a better programmer".

    --
    Mats
    Last edited by matsp; 11-06-2007 at 03:42 AM.
    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.

  8. #53
    Registered User
    Join Date
    Oct 2007
    Posts
    29
    Quote Originally Posted by Adak View Post
    Then, inside the functions that the outer loop calls, will be other loops that might address the player's movements from room A to room B, then back to room A, then room C, whatever.
    i am not sure what this means, dose it mean there should be loops in my functions?

    thanks matsp i think i did most of what you said, i think?

    but this is how i have it now and it comes with an parse error befor ";" in the for loop. i dident know how to update the while loop so i am trying a for loop.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    void script (int room_num){
    
    if (room_num==0){
    	printf("\nroom0\n");
    	}
    if (room_num==1){
    	printf("\nroom1\n");
    	}
    if (room_num==2){
    	printf("\nroom2\n");
    	}
    if (room_num==3){
    	printf("\nroom3\n");
    	}
    if(room_num==-1){
    	printf("\nthe walls are acid\n");
    	return 0;
    	}
    }
    
    int next_room(unsigned int a, unsigned int b)
    {
        static const int map[4][4] =    {   {1, 2,-1,-1},
                                            {2, 0,-1,-1},
                                            {0, 1,-1, 3},
                                            {-1,-1,2,-1}
                                        };
    				
    			return map[a][b];
    }
    
    int conversion (char letter){
     switch(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 &#37;c is not a actual direction and end up walking into the slabbering fangs of a lurking Grue.\n", letter);
    	return -2;
        }
    }
    
    char menu(void){
    char x;
    printf("\nyou want to do this y, n:");
    	scanf("%c",&x);
    	getchar();
    	return x;
    	}
    
    int main (void)
    {
    char letter;
    char play;
    int current_room;
    int room_num;
    int letter_num;
    
    /*while(1) {
    	play=menu();
    	
    		if(play=='n')
    			break;
    		} */   i commented the menu out because i did not whant to deal with it.but it works just fine.
    	
    	while(1){
    	printf("direction:");
    		scanf("%c",&letter);
    		getchar();
    	for( letter_num=conversion(letter), room_num=next_room(current_room,letter_num);1; current_room=room_num;)
    	
    	{
    
    if(letter_num==-2)
    	break;	
    			
    	}	
    script (room_num);
    			}
    
    			
    return 0;
    }
    Last edited by deadherorising; 11-09-2007 at 12:43 AM.

  9. #54
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This code is all wrong:
    Code:
    for( letter_num=conversion(letter), room_num=next_room(current_room,letter_num);1; current_room=room_num;)
    	
    	{
    
    if(letter_num==-2)
    	break;	
    			
    	}
    You don't need a for-loop here - and this particular loop definitely doesn't work - it spins around forever doing very little. The check for "letter_num == -2" is after you call next_room [not to mention that it breaks only the for-loop - which will only happen if an invalid direction was given before the loop was entered].

    I would remove the for-loop, and move the check for -2 to before the next_room call. [Calling next_room with -2 as letter_num will cause bad index in your variable, and you'll get some "random" room number from that].

    The variable room_num can be replaced with current_room - no need for the two variables in this case.

    --
    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.

  10. #55
    Registered User
    Join Date
    Oct 2007
    Posts
    29

    halla luula

    it works i got nothing to say exsept thanks. and are there any problems with it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    void script (int current_room){
    
    if (current_room==0){
    	printf("\nroom 0\n");
    	}
    if (current_room==1){
    	printf("\nroom 1\n");
    	}
    if (current_room==2){
    	printf("\nroom 2\n");
    	}
    if (current_room==3){
    	printf("\nroom 3\n");
    	}
    if(current_room==-1){
    	}
    }
    
    int next_room(unsigned int a, unsigned int b)
    {
        static const int map[4][4] =    {   {1, 2,-1,-1},
                                            {2, 0,-1,-1},
                                            {0, 1,-1, 3},
                                            {-1,-1,2,-1}
                                        };
    				
    			return map[a][b];
    }
    
    int conversion (char letter){
     switch(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 and end up walking into the slabbering fangs of a lurking Grue.\n", letter);
    	return -2;
        }
    }
    
    char menu(void){
    char x;
    printf("\nYou sure you want to do this y, n:");
    	scanf("%c",&x);
    	getchar();
    	return x;
    	}
    
    int main (void)
    {
    char letter;
    char play;
    int current_room;
    int letter_num;
    int last_room;
    
    while(1) {
    	play=menu();
    	
    		if(play=='n') 
    			break;
    		else{
    		printf("to exit press any key exsept n,s,e,w. you start in room 0\n");
    			}
    		 
    	
    	while(1){
    	printf("direction:");
    		scanf("%c",&letter);
    		getchar();
    		
    	 letter_num=conversion(letter);
    		
    		if(letter_num==-2)
    			break;	
    		
    			last_room=current_room;
    	  current_room=next_room(current_room,letter_num);
    		if (current_room==-1){
    			printf("\nthe walls are acid\n");
    			current_room=last_room;
    							}
    							
    			
    	
    script (current_room);
    			}
    		}
    			
    return 0;
    }

  11. #56
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Wandering "into the slabbering fangs of a lurking Grue" and exiting is a little harsh just for pressing the wrong key, don't you think? Why not print an error message for every key except q or something? (Or perhaps, because 'q' is right next to 'w' for west, some other key.)

    Code:
    char menu(void){
    char x;
    printf("\nYou sure you want to do this y, n:");
    	scanf("&#37;c",&x);
    	getchar();
    	return x;
    	}
    If you pass in the string to print, and rename it, that function could be used in at least one other place.

    In script(), it looks like you could use a switch statement. Or a lookup table, implemented as an array of strings for the descriptions.
    Code:
    const char *message[] = {
        "room 1",
        "room 2",
        /* ... */
    };
    
    if(current_room >= 0) puts(message[current_room]);
    Let's see . . . your indentation could also be improved.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Page 4 of 4 FirstFirst 1234
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