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 Discussion / programming help!

Author
Message
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 7th Nov 2007 00:09
I need help in my program I already made me text based game and now i'm trying to make it so if you press a key that isn't listed there it will say "error not a command"
heres my code


does any one know what to type? thanks in advance.
demons breath
21
Years of Service
User Offline
Joined: 4th Oct 2003
Location: Surrey, UK
Posted: 7th Nov 2007 09:10


That way if they don't type 1 or 2 into your input then it should come up with that message

Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 7th Nov 2007 12:37 Edited at: 7th Nov 2007 12:41
@Demon
I think this would be tidier.


@Ed
but the "proper" way to do this would be

You use a SELECT statement to "examine" a variable. Each CASE is a possible value of that variable and what code should be executed if the variable is equal to that value. CASE DEFAULT defines what code should be executed if none of your cases match the value of the variable; because DB must check all the cases first, CASE DEFAULT must always be the final case, but it is not required.

"You must be someone's friend to make comments about them." - MySpace lied.
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 7th Nov 2007 13:08 Edited at: 7th Nov 2007 14:02
You didn't include the music so I chose some DB music that suited the story (check that the paths are the same on your computers before running). I loaded all the music at the beginning, that is the best way to do it.
I liked the story, although the code wasn't the tidiest I enjoyed playing your game, I got to the end too!



Since you use Yes/No options, I'll show you how to check for "Y" and "N" instead of numbers.
First you need the INPUT like you had before, but this time we want the user to input a string.

A problem that most noobs run into here is that "Y" or "N" could be typed as a lower case "y" or "n", so you would think you'd have to check for all four. The solution to this problem is to convert the string to upper case letters, then all we need do is check for the upper case versions of "Y" and "N".

Now we have our string in upper case, we can check to see what the user has typed.
There is a nifty trick you can use for options like Yes/No: some users may type "Y" or "N" while others might type "Yes" or "No", in both cases the first character is always different. We can exploit that difference to make our lives easier; instead of checking the entire string we will only check the first character. Let's grab the first character now before we head into checking it.

Great, now we can select the string and check for "Y" or "N". We will also give an alternative to "Y" or "N" in case the user messes up.

Awesome! We made that look easy
That CASE DEFAULT statement has got me thinking though; what happens if the user does mess up? What are we going to do to make sure the game can continue?
Let's think what we want the computer to do:

As I hinted, we could use a GOTO to go back before the string was entered and make the user type it again, but GOTOs are dangerous, they are like wandering off in the snow: you'll leave no tracks and wont be able to find your way home. So instead we should make a mini-loop that only continues the program when a valid choice has been made.
What loop do we choose? Well, we want the user to input a variable and then we want to check it, so we want a loop that checks at the end: REPEAT UNTIL is what we want.

This is a working example that includes everything we've done, it only changes the value of the msg$ so it wouldn't be much use in a game, but meh, it's an example.


So there it is, hope I taught you something new

"You must be someone's friend to make comments about them." - MySpace lied.
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 8th Nov 2007 00:06 Edited at: 20th Nov 2007 02:00
Thanks guys for the heads up I might use the Y and N code but the only problem is the whole game is not only y and n it's there are other answers too using numbers would be much faster than having to type in stuff e.g.
if i made it so they would hame to enter an answer like this
what do you like?
toys
cars
chairs
and all the good stuff and they type in
Chair it would say error or skip that part because theres no s in the end for numbers anyone can do it cuz theres only one number e.g.
what do you like to eat?
cheese 1
pizza 2
(the user puts in 2)2
you like to eat pizza
------------------------------------------------------
P.S. you should go to my web site the codes here are kind of outdated also I'll upload the source file so you can do what ever you want from it e.g. add more/better music,etc
my website is on my profile
if not
http://edbertv1.googlepages.com

Attachments

Login to view attachments
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 8th Nov 2007 00:14
Oh one more thing this works only for 2 numbers is there any way to make it work for more?
like 3 numbers?
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 8th Nov 2007 00:26
Nerver mind i figered it out and thanks for helping me
for two numbers @OBese87 way would be better for more than two @demons breath way would be better
Ed222
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: Calgary
Posted: 20th Nov 2007 01:18
@demons breath
just so you know what i've done this is how i modified your code

also you could do > since all my numbers are in order
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 20th Nov 2007 03:07
Quote: "for two numbers @OBese87 way would be better for more than two @demons breath way would be better"

Not really, CASE statements are great if you have lots of possible outcomes.


"You must be someone's friend to make comments about them." - MySpace lied.
demons breath
21
Years of Service
User Offline
Joined: 4th Oct 2003
Location: Surrey, UK
Posted: 24th Nov 2007 11:18
yeah Ed my way was rubbish completely inefficient.

The select case method would probably be best, although I've never used it.

Failing that, as OBese said:


Login to post a reply

Server time is: 2025-06-05 17:37:39
Your offset time is: 2025-06-05 17:37:39