LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-10-2005, 11:54 AM   #1
MylesCLin
Member
 
Registered: Sep 2004
Location: Texas, USA
Distribution: Slack 9.1 with slackware-current packages...
Posts: 164

Rep: Reputation: 30
Text adventure command problem


Me again.
Anyway, from my last few posts in this forum, you can tell that I'm writing some kind of txt edventure (Note the e, that's the name of my 'engine' )
So, I've implemented basic commands, north, south, look, quit, etc.
But! I have not gotten 'get' down.
I have a basic object structure, shown here;
Code:
typedef struct item{ //item structure
int location,pickedup;
char name[20],desc[20];
void (*callback)();
} item;
and a *basic* way to 'pick up' one of those.
Code:
	if(strspn(commands,"get") == 3){
		char exbuf[10];
		int i,gotobj=0;
		printf("What item would you like to get?");
		scanf("%s",exbuf);
		printf("%s\n\n",exbuf);
		for(i = 0; i < MAXITEMS; i++){
			if(items[i].name == exbuf && items[i].location == curoom){
		    curitemnum++;
			inv[curitemnum] = i;
			printf("%s\n\n",items[i].name);
		    items[i].callback();
			gotobj = 1;
			}
		}
		if(gotobj != 1){
		printf("I cannot get that!");
		}
	}
Though when I type 'get' then 'gun' or 'Gun' it says 'I cannot get that!' as I have coded it.

Can someone help me?

Also, does anyone know how to make it accept 'get <objectname>'?


Thanks in advance.
 
Old 02-10-2005, 11:58 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Code:
if(items[i].name == exbuf...
That will never be true. Try using strcmp() instead. Using == tests for pointer equality instead of content equality.
 
Old 02-10-2005, 03:23 PM   #3
MylesCLin
Member
 
Registered: Sep 2004
Location: Texas, USA
Distribution: Slack 9.1 with slackware-current packages...
Posts: 164

Original Poster
Rep: Reputation: 30
Thanks, that fixed it.
Now I have another problem.
I have it loop through the array of items and see if they are in the room, but when I have an item named gun2, it 'get's that too.
Any ideas?
 
Old 02-10-2005, 06:31 PM   #4
MylesCLin
Member
 
Registered: Sep 2004
Location: Texas, USA
Distribution: Slack 9.1 with slackware-current packages...
Posts: 164

Original Poster
Rep: Reputation: 30
Anyone?
 
Old 02-10-2005, 07:40 PM   #5
webtoe
Member
 
Registered: Apr 2001
Location: Cambridge, England
Distribution: Slackware 10, Fedora Core 3, Mac OS X
Posts: 617

Rep: Reputation: 30
Erm, what the code look like now? is it like this:

Code:
if(strcmp(items[i].name, exbuf) && strcmp(items[i].location, curoom)){
I'm not much of a C programmer (well, not much of a programmer at all) but would like to have a swing at it. Could it have something to do with the fact that strcmp() returns -1, 0 or 1? Depending on the comparison.

Alex

Last edited by webtoe; 02-10-2005 at 07:42 PM.
 
Old 02-11-2005, 12:05 AM   #6
MylesCLin
Member
 
Registered: Sep 2004
Location: Texas, USA
Distribution: Slack 9.1 with slackware-current packages...
Posts: 164

Original Poster
Rep: Reputation: 30
That's almost what I have it do now.
No need to strcmp the location, as it is an integer, and I've removed exbuf all together.
Basically, you can type in 'get <object>' but if any other object's name starts with, for instance, 'gun', it'll pick it up too.

EDIT: Also, if strcmp returns 0, it means they match.

Last edited by MylesCLin; 02-11-2005 at 12:24 AM.
 
Old 02-11-2005, 04:22 AM   #7
webtoe
Member
 
Registered: Apr 2001
Location: Cambridge, England
Distribution: Slackware 10, Fedora Core 3, Mac OS X
Posts: 617

Rep: Reputation: 30
Hmm, thanks for teaching me a bit more about C!

Could it be something to do with the fact that strcmp() is returning -1? this would be false, so the if statement would be false ( false && true )

Just chucking that in there to see if it is the case. As far as strcmp() is concerned, gun and gun2 should return a minus one i think. It will pad the first gun to make it length 4, but the special padding at 4 will come before 2 in the alphabet causing a return of -1. I think this is how it works but then it may not and I accept I could be going down the wrong track here.

HTH

Alex
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
My stupid code adventure exvor Programming 9 01-18-2005 12:26 PM
Looking for a new adventure corbintechboy *BSD 12 10-24-2004 05:19 AM
Adventure Games on Linux? henrikanttonen Linux - Games 2 08-27-2004 08:18 PM
Command line Text Terri Linux - General 8 04-05-2002 03:10 PM
want to experience the adventure.Help me cybercop12us Linux - Networking 1 10-23-2001 09:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration