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 / Just a little more hangman help?

Author
Message
MacMan
14
Years of Service
User Offline
Joined: 9th Apr 2012
Location:
Posted: 3rd May 2012 07:05
Almost done with my hangman game. Just have a few things left that I can not seem to figure out on my own.

1. No matter what I try I can't get the word that comes before guess in my game to go away without royally screwing everything else up.

2. I can't seem to figure out how to input Lower$ and Upper$ into my code in order for my game to accept upper and lower case letters as guesses.

3. I need to figure out an easy way to make it so when a duplicate letter is guessed it alerts the player and lets them guess again.

4. Lastly, I need my game to be able to access an outside word list in a .txt file. No clue on how to do this.

If you can lend a hand in any of this that would be super. Need to get this game finished up here soon. Thanks!

Kevin Picone
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 3rd May 2012 08:55
Quote: "1. No matter what I try I can't get the word that comes before guess in my game to go away without royally screwing everything else up.
"


Can't test your code here atm.. so Dunno.

Quote: "2. I can't seem to figure out how to input Lower$ and Upper$ into my code in order for my game to accept upper and lower case letters as guesses."


To get rid of case issues, we generally force both sides of string compares to be either all upper or all lower. Then it doesn't what case the text was entered.

eg.



Quote: "
3. I need to figure out an easy way to make it so when a duplicate letter is guessed it alerts the player and lets them guess again.
"


An easy way to trap duplicates is we compare what was the program (or user) selects against what was last selected.

Imagine we're randomly picking values from a range of 0 to 5 say and we need to detect when the a newly picked value is the same as the previous/old value.. There's a bunch of ways of doing it, but just storing the OLD value is about the simplest.



Here it's showing us when a selection was the same as the previous choice. When such a situation occurs, we just pick again.


Quote: "4. Lastly, I need my game to be able to access an outside word list in a .txt file. No clue on how to do this."


Look into sequential file access. You basically just open the file (open to read) and then grab the rows of text from it until at the end of the file, where we close the file again.

MacMan
14
Years of Service
User Offline
Joined: 9th Apr 2012
Location:
Posted: 4th May 2012 05:57
I'm having some issues getting my game to open to my external wordlist properly. What am I doing wrong?

MacMan
14
Years of Service
User Offline
Joined: 9th Apr 2012
Location:
Posted: 4th May 2012 06:00
wordlist is attatched to post.
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 4th May 2012 11:39 Edited at: 4th May 2012 11:40
You definitely have the right idea with functions, but you could be making more use of functions to shorten your code!

One thing that worries me is this:

You could replace all of these functions with a single, shorter function:


Then instead of this code:

you can write this:





Another thing is the lack of formatting in your code. The following is much easier to read:


Another error is that this command:

is the same as "set text size 0". This is because "normal" is a variable that doesn't exist, and when a variable doesn't exist DBPro interprets that value as zero.

This code can be shortened with a loop:

to:


The giant thing of nested if-else statements was unnecessary, and can be replaced with this. I shortened it using the "greater than or equal to" operator.



And... I'm sorry to say... the whole while loop is really unclear.

The first problem is the formatting. It's hard for people who aren't already familiar with this code to read it. The second problem is that it's difficult to visualize the flow of the program. A "while" function is supposed to repeat until the condition isn't met, but you jump FROM the while loop to OUTSIDE the while loop, rendering it useless (as far as I can tell). GOTOs are generally considered bad practice for that exact reason.

To solve it, and to help compartmentalize everything, you can make use of global variables. Global variables can be changed from inside functions, and their values carry over into every other function and loop. All you have to do is add the "global" keyword before your variables:


So now, instead of using goto to restart the program each time, we're just going to have an initialize function.


this function resets all variables


So this is the final, readable code. I removed the variable "letterguess", and the functions handleerror and letterwrong, because they weren't being used.




So... Now on to your actual question! Sorry, it's just that... it's difficult to help people if their code isn't cleared up first, because whenever you run in to an issue (wrong letter shows up, system doesn't respond), you can't be sure where the problem is until you've made sense of messy code.

Your loading code looks perfect to me. One thing you might have messed up with is that wordarray and wordlist are two separate arrays, so only one will be used. The problem might be that you're trying to load from "hangmanfinal/hangman.txt". This means that if your executable was in "C:\hangmanfinal\", the program would look for hangman.txt in the directory "C:\hangmanfinal\hangmanfinal\hangman.txt". Your code works perfectly for me.

Anyways, it turns out that it's possible to handle words of ANY size, with a word list of ANY length! First what we're going to do is initialize the array with 0 elements:


-1 means that they have absolutely NO elements. We can't even say wordlist(0)="HI". Now we're going to create another variable keeping track of the number of elements in "wordlist".


Now we'll want to open word list, read string after string from it, and add each string to the array, increasing its size each time. This code accomplishes that using the "array insert at bottom" command.

Now when we pick a random value, we'll say this instead:

num = rnd(numwords)
gitem = wordlist(num)

To fill up the letter array after we've selected a new word, we can do this:

first the array is emptied by completely redeclaring it. There's also a little quirk here, because now this is programmed so that itemletters starts from the index 0. So we need to subtract one from some parts and add one to other parts. Likewise, the "check for matches" part will have to go from 0 to li-1:


Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 4th May 2012 11:42
Oh right. So here is the outcome: Super awesome hangman deluxe:


And here's a challenging word list I had around:


MacMan
14
Years of Service
User Offline
Joined: 9th Apr 2012
Location:
Posted: 4th May 2012 18:55
Thank you NeuroFuzzy for all the great help! I've got everything fixed but my stupid game still won't open my .txt file. All I'm doing is putting my hangmanfinal.dba and my hangman.txt file in a folder named hangman on my desktop. When I run my program I am still getting Runtime Error 105-File does not exist. I've attatched the wordlist if you think it has to do with that. Thanks again
MacMan
14
Years of Service
User Offline
Joined: 9th Apr 2012
Location:
Posted: 4th May 2012 19:38
Whew finally got it to work. Guess I wasn't saving the .dba files right, but it all works great now. The only thing that I still need is I need the game to accept both upper and lower case letters. Also if it's not to difficult I need to come up with a way for my game to not accept the same letter as a guess more then once. Thanks for all of your help.
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 5th May 2012 03:19
The .exe is created in the same directory as the project file (.dbpro), not the source file.

Also, you might want to keep going back to my previous post. There are a lot of changes you can make that seem trivial, but they really help clear things up.

Anyways, for loading words, you'll want to always store them lower case, so in the loading code, you'll be saying:
loaded_word_array(n)=lower$(loadedstring)

And right after your input command, you can say:
input "Guess a letter or press qq to quit: ", guess
guess=lower$(guess)

That's it!


To not accept letters more than once, what you'll have to do is create an array that stores all the previous guesses. You'll initialize the array with zero elements (dim guessarr(-1) as string) and each time the user guesses, you'll check each element in the array to see if the user has already guessed that element before. If they have, you'll skip the main loop (do this using if statements, not gotos) and prompt them to guess again. If they haven't, you'll add that letter to the guessed array using "array insert at bottom" (note: You can use the "array count(arr())" function instead of storing the array size in a separate variable), and once that element is in the array you run the rest of the code.

jestermon
15
Years of Service
User Offline
Joined: 26th Nov 2010
Location:
Posted: 7th May 2012 14:25
Perhaps my online hangman game may give you a few pointers as to a simple interface for a hangman game.
Of course your game will be different, and have it's own flavor.
Take a peek at how I display all letters pressed, and then prevent duplicate letters from being used again.
The game uses DXStudio web player which you will need to install to play the game. . . the page links to the player download if you don;t have it installed yet.
Hope this helps.
jestermon
15
Years of Service
User Offline
Joined: 26th Nov 2010
Location:
Posted: 7th May 2012 14:27
Oops I forgot the link to my hangman game .. sorry, here it is
http://3dfoundry.net/hangman

Login to post a reply

Server time is: 2026-07-08 05:37:17
Your offset time is: 2026-07-08 05:37:17