Thread: A begginer writing a text based dungeon game.

  1. #31
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You seem to insist on 'production' rather than design, and you've 'changed your mind' a few times through this post. Adak, matsp and Salem have all suggested doing a design, and a full design at that.

    So problems like this would be worked out in the design phase, not the production phase. The mono-directional SDLC goes a bit like:
    Analysis - You should work out what you need, what you need to do
    Design - Design everything, function names, variable names, IPO charts, anything you can think of
    Development - The coding bit you seem so hell bent on doing
    Testing - Yes you should test your game
    Documentation - Document it (internal documentation - source comments) and user docs etc
    Implementation - How you're releasing and distributing your game, etc
    Evaluation - How your game went, did you get done what you established in the analysis phase?

    So seem to have done step 1 & 2 in your head and moved straight to development, which is getting you in trouble.
    Last edited by zacs7; 11-04-2007 at 06:49 PM.

  2. #32
    Registered User
    Join Date
    Oct 2007
    Posts
    29
    the next_room function is taking in 2 numbers and giving back the one number from the array. sorry Adak but i don't understand. but here i tryed again

    like this?
    Code:
    int next_room(int a, int b){
    int map[a] [b]={
                                       {1, 2,-1,-1},
    					{2, 0,-1,-1},
    					{0, 1,-1, 3},
    					{-1,-1,2,-1}
    };
    return map[a][b];
    }
    
    
    int main (void)
    {
    printf("number of index %i",next_room(0,0)
    
    return 0;
    }
    from this example I want it to print out the number one, but it dosent. can you give me an example of one that works.

    sorry zac7 you posted while i was still typing.
    Last edited by deadherorising; 11-04-2007 at 07:13 PM.

  3. #33
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I don't see how it would print 1 if it wouldn't even compile.

    Code:
    #include <stdio.h>
    
    int nextRoom(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}
                                        };
        /* check a and b are in bounds */
        if( a < (sizeof(map) / sizeof(map[0])) &&
            b < (sizeof(map[0]) / sizeof(map[0][0])))
        {
            return map[a][b];
        }
        
        perror("out of bounds");
        return 0;
    }
    
    int main(void)
    {
        printf("number of index &#37;d\n", nextRoom(0, 0));
        
        return 0;
    }
    Stop ignoring what people are saying

    BTW, you check that a and b are in bounds because otherwise you could try and read past the end of the array possibly causing a segfault or other naughty things.
    Last edited by zacs7; 11-04-2007 at 07:15 PM.

  4. #34
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Zac: I would blame his teacher, except in this case, I believe he's working more or less, on his own. It's hard to learn how the process of programming should work, until you've seen it a few times.

    Hero: Generally, using several small functions is what you should do - but not just to get an array element's value.

    You can delete the whole next room function. All you need is:

    Code:
    int a;
    a = array[row][col];
    Where row and col are positive integers, within the bounds of the array[][]. If you are only getting one number from the user, you may need to change the array from 2D's into a single dimension.

    Note that a single dimension array can still look like a 2D array to the user, if you display it with just a limited number of elements being shown, in each row.

    (Since I don't really know what you're doing here, with these specifics, I'm probably just talking out of my as*. Watch out for "bad breath"!

  5. #35
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    After re-reading your earlier posts, here's my suggestion:

    When the user enters a direction, say 'n' for North (up). Then you'll have a function called "Move", with code something like this:

    Code:
    void Move(int move)  {
    
    /* t before a variable indicates a temporary variable
    
       if  (move == 'n') 
          trow = row - 1;  
       
       if (move == 'e' )
          tcol = col + 1;
    
       if (move == 's')
          trow = row + 1;
    
       if (move == 'w')
          tcol = col - 1;
    
       if (trow >= 0 && trow < maxrow)  {               /* test the row, that it's inside the array */
          if (tcol >= 0 && tcol < maxcol)  {            /* now test the column */
             row = trow;                                      /* If they're good, we make the assignments */
             col = tcol;
             printf("\nYou're at row: %d and col: %d", row, col); /* and give some feedback to user */
          }
       else
          /* row and col variables remain unchanged, since move was bad */  
          printf("\nI can't move you to row: %d and col: %d", row, col);
       }
    }
    The variables of row and col are globals, and together, define the current position (cp) of the player at any time. Cp is also a global variable, and would be a struct containing one row, and one col, but I didn't use that here, because I thought it might be too hard for you, just now.

    Hope that helps.

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

    ug

    i now can make it go from room0 to an adjacent room. i do have a problem with it but its not any error it actually works. now to make it continue and end were i want it. i have tried a while loop that dident work then i tryed a do while loop did the exact same thing, and i have tride them in diffrent ways with the loops but nothing seems to work how i want it. how do i make it continue and end.

    yes i now these are this is what happens when i skip the design step.
    thanks zacs7 that realy helped and its not realy the teachers fault its the system, i have never seen code in my life till this class, the teacher teaches the class like we have been writing code most of our life, which is mostly correct i am the exception.

    and Adak thanks for the example, i kinda understand it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    void script (int room_num){
    
    if (room_num==0){
    	printf("room0\n");
    	}
    if (room_num==1){
    	printf("room1\n");
    	}
    if (room_num==2){
    	printf("room2\n");
    	}
    if (room_num==3){
    	printf("room3\n");
    	}
    if(room_num==-1){
    	printf("the 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 %c is not a actual direction and end up walking into the slabbering fangs of a lurking Grue.\n", letter);
    	return 0;   <---here is my small problem i want the whole program to         completely end here.
        }
    }
    
    int main (void)
    {
    char letter;
    int a;  //current room
    
    printf("direction:");
    	scanf("%c",&letter);
    	
    int y=conversion(letter);  //the number of the direction
    int x=(next_room(a,y)); //the next room number
    
    script (x);
    
    return 0;
    }
    to up date the room number i was thinking x=a, would that work some how?

  7. #37
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, let your conversion() function return a value that is "impossible", e.g. -1, and then check for that in your main - if it's -1, then return. I suggested this previously.

    Of course, exiting the game just because the user pressed the wrong key is a bit harsh, but it's your game.

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

  8. #38
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    char letter;
    int a; //current room

    printf("direction:");
    scanf("&#37;c",&letter);

    int y=conversion(letter); //the number of the direction
    int x=(next_room(a,y)); //the next room number

    script (x);
    Here's your next suggestion:

    Instead of having a variable called "letter", or "y", or "x", make the names of your variables something that instantly tells you something about it, like:

    char move;
    int current_rm;
    int conversion;
    or
    int next_rm;

    As mentioned by others, you want to have your characters hold a "KNIFE", instead of a "2" or other designation which has no meaningful association to us humans.

    **NOW** looking at your code, you begin to understand it much faster, and you will make far fewer mistakes.

    Most games have an outer loop, which basically ensures that they keep looping around until the player wants to end:

    Code:
    while (1)  {
       /* Menu() asks if the player wants to play, and returns that choice to this loop */
       play = Menu(); 
       if (play == 'n' || play == 'N') 
          break;  /* breaks out of this endless loop */
    }
    You don't want variables like x = a. You don't want to see me get ill looking at your code, do you?
    Last edited by Adak; 11-05-2007 at 06:48 AM.

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

    agh!

    what did i do wrong it wont end.

    Code:
    char menu(void){
    char x;
    printf("you want to do this yes, no:");
    	scanf("%c",&x);
    	return x;
    	}
    
    int main (void)
    {
    char play;
    while(0) {
    	play=menu();
    		if(play=='no'){
    			break;
    			}
    		}

  10. #40
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    play=='no'
    compares a single char with a multibyte character constant - it is never going to be equal.

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

  11. #41
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by deadherorising View Post
    what did i do wrong it wont end.

    Code:
    char menu(void) {
      char x;
      printf("you want to do this? (y/n):");
    	scanf("&#37;c",&x);
    	return x;
    }
    
    int main (void)
    {
    char play;
       while(0) {       /* don't you want while(1), instead? */
    	play=menu();
    	if(play=='n')    /* no curled brace is needed here -->{  */
    	   break;
    /* to the compiler, an if statement starts with the "if" and ends with the first semi-colon ";"
    after that. So for one line after the if, no curly brace is needed. For two
    or more lines with semi-colons that you want to have as part of the "if" statement, THEN
    you'll have to have the curly braces, to let the compiler know what you want.  */
    
       }  /* end of multi-line while statement */
    return 0;  /* customary return from a normal termination is zero */
    } /* end of main */
    Looks better, hero!
    Last edited by Adak; 11-05-2007 at 06:37 PM.

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

    death by coding.

    it still acting strange and i cant put the zero in for while it just ends.

    heres what happens
    ------
    [Session started at 2007-11-05 20:02:45 -0500.]
    you want to do this y, n:y
    you want to do this y, n:you want to do this y, n:y
    you want to do this y, n:you want to do this y, n:n

    assinment 4b has exited with status 0.
    ------

    and heres the code
    Code:
    char menu(void){
    char x;
    printf("you want to do this y, n:");
    	scanf("%c",&x);  /// i tryed  using \n but that dident work.
    	return x;
    	}
    
    int main (void)
    {
    char play;
    
    while(1) {    ///i changes it 1 i wouldent work as 0.
    	play=menu();
    		if(play=='n')
    			break;
    			}

  13. #43
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by deadherorising View Post
    it still acting strange and i cant put the zero in for while it just ends.

    heres what happens
    ------
    [Session started at 2007-11-05 20:02:45 -0500.]
    you want to do this y, n:y
    you want to do this y, n:you want to do this y, n:y
    you want to do this y, n:you want to do this y, n:n

    assinment 4b has exited with status 0.
    ------

    and heres the code
    Code:
    char menu(void) {
      char x;
      printf("\nyou want to do this y, n:");  /* this will move the question, one line down */
      scanf("%c",&x);  
      
      return x;
    }
    
    int main (void)
    {
    char play;
    
    while(1) {    ///i changes it 1 i wouldent work as 0.
    	play=menu();
    		if(play=='n')
    			break;
    			}
    The while(1) is correct. while(0) is never going to loop even once.

  14. #44
    Registered User
    Join Date
    Oct 2007
    Posts
    29
    err its still doing the same thing. just with more spaces.

    ------
    [Session started at 2007-11-05 21:23:45 -0500.]

    you want to do this y, n:y

    you want to do this y, n:
    you want to do this y, n:y

    you want to do this y, n:
    you want to do this y, n:n

    assinment 4b has exited with status 0.
    -------

    i added the \n but this is all it did.

  15. #45
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Would you post up your most recent full code, please?

    I'll see what's going on. "Acting strange" is not all the details I need.

    Thanks.

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