Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Need some help (Easy Problem)

Author
Message
Divided
FPSC Reloaded TGC Backer
12
Years of Service
User Offline
Joined: 31st Oct 2013
Location:
Posted: 13th Nov 2013 14:39
Hello people,

I seem to have a slight issue...
I am creating a text adventure game but there is one bug that is driving me up the wall.
I am no experience programmer so I'm sure there is a simple solution but I can't seem to find it.

Now to the issue at hand...



The issue is I can't seem to find out how to only be able to select 1 of the 3 classes and if something is misspelled an error message appears...

The array I am using for the classes is...



Your assistance is much appreciated.



DividedV -- YouTube Channel
Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 13th Nov 2013 17:45 Edited at: 13th Nov 2013 17:52
A simple solution could be:



Another method:



D.D.
Mobiius
Valued Member
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 13th Nov 2013 19:57 Edited at: 13th Nov 2013 20:01
Don't tell newbies to use goto for the love of god!

Using the code you've provided, I'd do something like this...



But I wouldn't do it like this in any game I create as it's quite a poor way of doing it. I'd use my GUI system to create a proper menuing system, leaving only clickable buttons to choose your class, rather than have your users typing anything.

Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 13th Nov 2013 21:43
Quote: "Don't tell newbies to use goto for the love of god!"



LoL... It's a text game. I doubt the performance will be hindered.


D.D.
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 13th Nov 2013 22:36 Edited at: 13th Nov 2013 22:46
I'd say this is one occasion where GOTO is okay. Maybe there is a better way of writing this, as there usually is with GOTO, but it seems pedantic to avoid using GOTO in the current layout. The repeat-loop here is basically an inverted goto with more code required to work.

All three words here have different initials so we don't need the entire word, I'd just grab the first character of the input string.
Since this is a text adventure, it's especially important for you to think about the least amount of information you require from the user to be able to interpret their actions, for example, if you have two commands "talk to" and "take" then you only need to read the first three letters to distinguish between them.

You could also make a menu system like DD suggested, great for choosing from a list of items, but for the main game I would prefer the input line for that nostalgic feel.

It makes more sense to store the player's chosen class as the number of the class in your array, then you can reference all the attributes of the player's class with the same number, eg Classes(myClass).dexterity.



Formerly OBese87.
Divided
FPSC Reloaded TGC Backer
12
Years of Service
User Offline
Joined: 31st Oct 2013
Location:
Posted: 14th Nov 2013 00:55
Quote: "
chooseClass:
input "Thief, Mage or Captain? -> "; in$
in$= lower$(left$(in$,1))

select in$
case "t" : myClass= 0 : endcase
case "m" : myClass= 1 : endcase
case "c" : myClass= 2 : endcase
case default : goto chooseClass : endcase
endselect

Print "You are now a "; Classes(myClass).character + "!"
"


Your code works perfectly.
The only issue I have now is understanding it completely.
Would you please break down the code so that I could understand it?

DividedV -- YouTube Channel
Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 14th Nov 2013 01:16 Edited at: 16th Nov 2013 01:59
If you're allowing the user input raw text, then some effort has to go into sanitizing what they enter once they've hit return, as it may well be anything.

The very least you can do, is strip the spaces/tabs and force the case.




Another common trick in legacy parsers is they only use the first 2 or 3 letters when matching against user input.

eg,




There's still a problem with the word sanitizing & matching though. Since if the player is anything like me and tends to bash out on the keyboard quickly making lot of mistakes, then they're bound to hit characters outside what the program is expecting. Which can make this kind of entry very tedious for end users.

There's a few ways of attacking such problems, I'd probably go for a solution where the user presses a key ( the program polls inkey$() ) to make a selection, rather than make them manually input the selection, since this by design removes the programmer from most of the sanitizing head aches and is easier for end users also.

But if you have to use input, then an alternative method to find matches could be by instr. By flipping the comparisons around, we can see if the word we want is in the string they provided. Another way would to be to write a function that strips the illegal characters out of the users input. So only alphabet characters remain.

The slightly more complex solution is to write a parser to break the input text down into individuals words and whatever syntax you need to support also.

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 14th Nov 2013 01:21
in$= lower$(left$(in$,1))
This line first takes the leftmost character from in$ left$(in$,1), (I think chr$(in$) will take the first character but I wasn't sure and can't test atm, maybe you can test that for me.) Then the result is converted to lower case.
It works the same as if it were: first$= left$(in$,1) : lower$(first$) but it's less writing and one less variable to worry about.

Then there's the SELECT CASE statement, it should be pretty obvious what this does even if you haven't seen one before. Each CASE is like an "if in$=... then ... endcase", if the value of in$ doesn't match any of the cases then it falls under the default case and for our program that means an invalid input, so we send it back to the chooseClass label to get another input.

The value assigned to myClass corresponds to the field in your array for the chosen class. using variables to reference arrays is the real power of using arrays.


Formerly OBese87.
Divided
FPSC Reloaded TGC Backer
12
Years of Service
User Offline
Joined: 31st Oct 2013
Location:
Posted: 14th Nov 2013 01:49
Quote: "
in$= lower$(left$(in$,1))
This line first takes the leftmost character from in$ left$(in$,1), (I think chr$(in$) will take the first character but I wasn't sure and can't test atm, maybe you can test that for me.) Then the result is converted to lower case.
It works the same as if it were: first$= left$(in$,1) : lower$(first$) but it's less writing and one less variable to worry about.

Then there's the SELECT CASE statement, it should be pretty obvious what this does even if you haven't seen one before. Each CASE is like an "if in$=... then ... endcase", if the value of in$ doesn't match any of the cases then it falls under the default case and for our program that means an invalid input, so we send it back to the chooseClass label to get another input.

The value assigned to myClass corresponds to the field in your array for the chosen class. using variables to reference arrays is the real power of using arrays.
"


Thank you as this helped a lot and I will use this for my final solution.

Quote: "
There's still a problem with the word sanitizing & matching though. Since if the player is anything like me and tends to bash out on the keyboard quickly making lot of mistakes, then they're bound to hit characters outside what the program is expecting. Which can make this kind of entry very tedious for end users.
"


Thank you for that, I really appreciate your assistance even tough I'm not 100% sure I know what you are on about.
you did however make me reconsider how I approach this solution. I totally agree that spaces, tabs and incorrect input should not be a factor.

DividedV -- YouTube Channel

Login to post a reply

Server time is: 2026-07-06 20:15:14
Your offset time is: 2026-07-06 20:15:14