Thread: catch the same objet with stand alone and with parameter

  1. #1
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92

    catch the same objet with stand alone and with parameter

    Hi friend ,
    It's just my first thread .
    i write a code for a text adventure game.
    but i have some problem with look command
    i want it use with both way a stand alone look and with object
    let me clear,
    when i give command
    >look
    then it displays the object such as watch,chair
    AND
    when
    >look watch then info abt watch
    just like
    >look
    printf("u currently looking a watch,chair,fan).
    and
    >look watch
    printf("Its 5:45 now")
    its possible to use look with both ways?
    AbHHinaay

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>its possible to use look with both ways?
    Yes, if you code it that way You just need to recognise the extra text the user types and act accordingly.

    If you're talking about game programming, we have a forum specifically for that topic, I suggest you post questions like this in there. This forum is for C questions.

    Welcome to cprog.com
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is easy. You make your parser basicly just rip the first word from the string. You compare that word with the list of commands to see if the command is valid. If it isn't a valid command, then either dispose of the whole string and tell the user that the command was invalid, or make it have some default action like talking or something. (Since this is single player, that wouldn't benifit you, so just go for option one.)

    From there, you pass the rest of the string along to the function/command itself. If the string you pass is empty, then you just use the standard no-argument version. If it contains something, you chop it up into arguments and deal accordingly.
    Code:
    word = parse_input( input )
    if( (command = get_command( word )) != INVALID_COMMAND )
    {
        do_command( command, input+strlen(word) );
    }
    else
    {
        do_invalid( );
    }
    Or you could do it all in a single step:

    if( (command = get_command((word = parse_input( input )))) != INVALID_COMMAND )



    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed