Why doesn't this routine work?

stralloc( 80 );
mov( eax, w );
stdin.flushInput();
stdout.put(">");
stdin.gets(w);
mov(0,s); // s=noun
mov(0,j); // j=verb
stdout.put("Value of s=",s,nl);
if( str.index(w,"west")) then mov(4,s);
else mov(0,s);
endif;
if( str.index(w,"sword")) then mov(7,s);
else mov(0,s);
endif;
stdout.put("Value of s=",s,nl);

As you can see, no matter what I enter the value of s is always 7. If
part of the string contains "west", s should then be 4. If part of the
string contains "sword", s should then be 7. If neither of these are
true, s should still be 0.

Why doesn't this work?

Regards,

Paul Panks
dunric@yahoo.com
Posted on 2003-04-30 08:15:36 by Paul Panks
some strange asm syntax you have here..



if( str.index(w,"west")) then
mov(4,s);
else
mov(0,s);
endif;

if( str.index(w,"sword")) then
mov(7,s);
else
mov(0,s);
endif;



the first "if" is useless since both of them use the same variable, so the value of s depends on the second "if"
or you just did not print s after the first "if"
Posted on 2003-04-30 09:25:50 by Azrim
Azrim,

Basically what I'm attempting to do is the following (ps-code):

if(part.of.string "w")="north" then s=1
if(part.of.string "w")="south" then s=2
if(part.of.string "w")="east" then s=3
if(part.of.string "w")="west" then s=4
if(part.of.string "w")="up" then s=5
if(part.of.string "w")="down" then s=6
if(part.of.string "w")="lantern" then s=7
if(part.of.string "w")="oil" then s=8
if(part.of.string "w")="rope" then s=9
if(part.of.string "w")="key" then s=10
if(part.of.string "w")="bottle" then s=11
if(part.of.string "w")="dagger" then s=12
if(part.of.string "w")="sword" then s=13
...etc

The first thing I do when I learn a new language is write a short text adventure. That way I can learn the basic i/o, text, array, string and variable functions of the language while doing something fun.

Regards,

Paul Panks
dunric@yahoo.com
Posted on 2003-04-30 09:34:57 by Paul Panks
Hi Paul

Consider this version of your program:



program PaulPanksQ;
#include("stdlib.hhf")

static
w : string;
j : int32;
s : int32;


begin PaulPanksQ;

stralloc( 80 );
mov( eax, w );
stdin.flushInput();
stdout.put(">");
stdin.gets(w);
mov(0,s); // s=noun
mov(0,j); // j=verb
stdout.put("Value of s=",s,nl);

str.index(w,"west");

if((type int32 eax) >= 0) then
mov(4,s);
else
mov(0,s);
endif;

stdout.put("str.index return value = ",(type int32 eax),nl);
stdout.put("Value of s=",s,nl);

stdin.flushInput();
stdin.gets(w);
str.index(w,"sword");

if((type int32 eax) >= 0) then
mov(7,s);
else
mov(0,s);
endif;

stdout.put(nl, nl, "str.index return value = ",(type int32 eax),nl);
strfree(w);
stdout.put("Value of s=",s,nl);
stdin.readLn();

end PaulPanksQ;

Posted on 2003-04-30 12:29:04 by Green Joe
Green Joe,

I tried your example but it only found the 4 as true if I entered "go west". If I entered instead "get sword" it returned it as -1 and thus 0. So somewhere the check for sword isn't working.

Paul
Posted on 2003-04-30 13:22:13 by Paul Panks
Sorry , my fault

I already edited the code... try it again
Posted on 2003-04-30 13:24:11 by Green Joe
Green Joe,

Cool it works! Thanks!

Paul
Posted on 2003-04-30 13:33:42 by Paul Panks

Why doesn't this routine work?

stralloc( 80 );
mov( eax, w );
stdin.flushInput();
stdout.put(">");
stdin.gets(w);
mov(0,s); // s=noun
mov(0,j); // j=verb
stdout.put("Value of s=",s,nl);
if( str.index(w,"west")) then mov(4,s);
else mov(0,s);
endif;
if( str.index(w,"sword")) then mov(7,s);
else mov(0,s);
endif;
stdout.put("Value of s=",s,nl);

As you can see, no matter what I enter the value of s is always 7. If
part of the string contains "west", s should then be 4. If part of the
string contains "sword", s should then be 7. If neither of these are
true, s should still be 0.

Why doesn't this work?

Regards,

Paul Panks
dunric@yahoo.com


Others have made comments about what this sequence is doing, so I won't bother.
However, I can recommend a *much* better way to do this kind of logic.
Use the HLA Standard Library pattern matching code. It makes the kind of string
processing you're doing quite trivial. It is a bit of work to learn how to use the pattern
matching code, but for a text-based role playing game like you're developing, it will save you considerable effort. Consider the following HLA code:



program testPats;
#include( "stdlib.hhf" )

static
input: str.strvar(256);

begin testPats;

stdout.put( "Enter line of text: " );
stdin.gets( input );

// Convert string to lower case to make things easy:

str.lower( input );

// Okay, do the pattern matching:

pat.match( input );

pat.matchStr( "go" );
pat.zeroOrMoreWS();
pat.matchStr( "west" );

stdout.put( "You entered 'go west'" nl );

pat.alternate;

pat.matchStr( "go" );
pat.zeroOrMoreWS();
pat.matchStr( "east" );

stdout.put( "You entered 'go east'" nl );

pat.alternate;

pat.matchStr( "go" );
pat.zeroOrMoreWS();
pat.matchStr( "north" );

stdout.put( "You entered 'go north'" nl );

pat.alternate;

pat.matchStr( "go" );
pat.zeroOrMoreWS();
pat.matchStr( "south" );

stdout.put( "You entered 'go south'" nl );

pat.if_failure;

stdout.put( "You entered an illegal command" nl );

pat.endmatch;


end testPats;



Cheers,
Randy Hyde
Posted on 2003-05-02 23:15:44 by rhyde