Author Topic: input random word from txt-file?  (Read 2650 times)

0 Members and 1 Guest are viewing this topic.

Offline rotebr0

  • Newbie
  • Posts: 1
    • View Profile
input random word from txt-file?
« on: January 31, 2019, 10:21:56 am »
hope someone can help me out.
i try to create a hangman-game in qb64.
i want the program to randomly pick a word from a word list and use it as a string$
i have a txt-file that contains 1000 words (one word per line).
ive used ”open for input” for highscore tables and such, but i really have no idea where to begin with this...
any ideas?

Rotebr0

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: input random word from txt-file?
« Reply #1 on: January 31, 2019, 10:56:50 am »
This kinda sounds a bit like someone’s homework, so forgive me if I don’t just toss out a solution.  Our policy has always been to help people with homework so they learn; not just do it for them...

You just need to break the problem down into easy steps:

DIM an array to hold the data.   
OPEN the file for INPUT.
Read it into your array. (LINE INPUT #)
CLOSE the file. 
Get a random number from 1 to 1000. (RND)
Your desired string is array(number)...

Give it a go, share your work, and we’ll help you sort out each step along the way where you might have difficulty.



https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: input random word from txt-file?
« Reply #2 on: January 31, 2019, 11:08:38 am »
You don't need an array.  You know the list of words is 1000. Generate a
random number between 1-1000 and read the file up to that number.  Display
the word.  Then it gets a little tough - a loop to get keyboard input,
see if the letter is in the word, blank that letter if it is, and display
the modified word.  Start all over again when a word is solved. 

You're using an insanely powerful language.  Try to avoid bugs that start
an atomic war. 

It works better if you plug it in.

Offline codeguy

  • Forum Regular
  • Posts: 174
    • View Profile
Re: input random word from txt-file?
« Reply #3 on: January 31, 2019, 02:47:33 pm »
Declare an array of string large enough to hold your 1000 words.
Open the text file for INPUT
Initialize a counter to lower bound of array
While not EOF or  counter > upper bound of string array {
Read a line of text and store it in the array
Increment counter
}
If you're doing this multiple times, it's better not to needlessly make your input device do extra work.
Calculate random index into string array
R=lower bound + int(rnd*(upper bound - lower bound))
« Last Edit: January 31, 2019, 02:52:54 pm by codeguy »

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
Re: input random word from txt-file?
« Reply #4 on: February 02, 2019, 06:36:58 am »
hope someone can help me out.
i try to create a hangman-game in qb64.
i want the program to randomly pick a word from a word list and use it as a string$
i have a txt-file that contains 1000 words (one word per line).
ive used ”open for input” for highscore tables and such, but i really have no idea where to begin with this...
any ideas?

Rotebr0
I'm surprised no one put a code. So, here's an example code:
Code: QB64: [Select]
  1. RANDOMIZE TIMER 'This statement is required to make a random numbered variable
  2. a$ = "myfile.txt" 'Change this with your file's name
  3. num = INT(RND * 1000) + 1 'Make an integer number from 1 to 1000.
  4. x = x + 1
  5. INPUT #1, w$
  6. IF x = num THEN
  7.  
Hope you understood xD!
« Last Edit: February 07, 2019, 05:33:44 am by Prithak »
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: input random word from txt-file?
« Reply #5 on: February 02, 2019, 09:22:31 am »

I'm surprised no one put a code. So, here's an example code:
Code: QB64: [Select]
  1. RANDOMIZE TIMER 'This statement is required to make a random numbered variable
  2. a$ = "myfile.txt" 'Change this with your file's name
  3. num = INT(RND * 1000) + 1 'Make an integer number from 1 to 1000.
  4. x = x + 1
  5. INPUT #1, w$
  6. IF x = num THEN
  7.  
Hope you understood xD!

Don't be surprised no one posted code, because it does look like a homework assignment and rotebrO hasn't denied it (yet).

I don't think your code example will work for him, did it work for you? Looks like you are missing a DO to increase x and to EXIT when x hits the random number.

Also I think Hangman would want to be played more than once, so best to load the words into an array, you could even check off words you've done already so not replayed.

« Last Edit: February 02, 2019, 09:30:05 am by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: input random word from txt-file?
« Reply #6 on: February 02, 2019, 10:51:55 am »
Interesting age we live in. We now have machines to access information with just a little more time than if we knew (learned) how to do something ourselves. It makes me wonder how often students use these device resources. I hope not to the point where, without a machine backing them up, they'd be nearly uneducated. I mean if a pilot had to land an airliner in the Hudson, I'd rather he know his job and fly the plane in than be Googling how to crash land it while it's going down. Sure, there's always been ways to game the system, frat notes, old tests, etc., but never as much as today. It's just a shame that making our devices smarter makes us go in the opposite direction. The proof of that is a person today is considered an idiot if they don't know how to "Google" it. You'd think the pride would be in not having to "Google" it, but no.

If this person doesn't return it's a pretty good bet he spent most of what could have been educational time on signing up for as many BASIC programming site as he could find, in hopes someone would post a working code example. Honestly, since coding isn't a core course, I wonder why anyone who would want to learn coding would try so hard not to learn how to code. In a weird way, that's similar to the pilot analogy, except there is a whole different set of consequences when it comes to the crash part.

OK, now that I Murphy lawed it, the OP can come back, post some code, get some help, become the next Steve on the forum, and tell me to go shove my CPU where the Sun Microsystems doesn't shine.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Qwerkey

  • Forum Resident
  • Posts: 755
    • View Profile
Re: input random word from txt-file?
« Reply #7 on: February 02, 2019, 12:18:48 pm »
Prithak, what Steve, bplus and even Pete in his own way are trying to say is that as a community we love to help (and do so) wherever possible.  But just writing code for someone else would just be doing that someone's job.  If rotebr0 had submitted some code and said that it wouldn't work correctly, we could advise how to proceed.  So whilst you wanted to help, perhaps just don't submit a complete block of work for someone else.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: input random word from txt-file?
« Reply #8 on: February 02, 2019, 01:17:53 pm »
In Biblical terms: Give a person a program and he uses it for a day. Teach a person to program, and he works on it for a lifetime. :D

Anyway, this is a fairly young board, but back in the day... when the dinosaurs ROM the motherboard... homework help was a regular daily occurrence. The QBasic Forum started a Homework Policy back in the 1990's. That tradition was carried over to QB64, because it just makes sense. I do recall I had to deal with one pinhead at QBF who stated he would post the code to the homework because he wanted to show off. I told him if someone asks for hangman and you want to show off... post Monopoly!

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: input random word from txt-file?
« Reply #9 on: February 02, 2019, 03:37:02 pm »
OK RotebrO for starters you can check if you can access the 1000 word file with this code:
Code: QB64: [Select]
  1. _TITLE "File Access Check" 'B+ 2019-02-02
  2.  
  3. myFile$ = "File Access Check.bas" '<<<<<<<<<<<<<<<<< use whatever text file you want to check
  4. OPEN myFile$ FOR INPUT AS #1 'open text file for sequential reading
  5. WHILE NOT EOF(1) 'setup loop to read the file line by line
  6.     LINE INPUT #1, myFileLine$ '<<< read the file line to this variable$
  7.  
  8.     'do stuff with the line, or not
  9.  
  10.     'use the following to check you have access to your file
  11.     PRINT myFileLine$
  12.     INPUT "press enter to read next line... "; wate$
  13.     PRINT
  14. WEND 'close loop
  15. PRINT "That's all folks!"
  16.  
  17.  

Save this code as "File Access Check.bas" and you can check if you can read this code, then change the file name to your word file and let us know if you read the file all in one line or in about a 1000 lines (after you put the 1000 word file in same directory as this code and compile the exe to the same directory as source code).

« Last Edit: February 02, 2019, 03:46:10 pm by bplus »

Offline Prithak

  • Newbie
  • Posts: 56
  • Life itself is a Programming Language!
    • View Profile
    • My Programming Language
Re: input random word from txt-file?
« Reply #10 on: February 07, 2019, 05:34:34 am »

I'm surprised no one put a code. So, here's an example code:
Code: QB64: [Select]
  1. RANDOMIZE TIMER 'This statement is required to make a random numbered variable
  2. a$ = "myfile.txt" 'Change this with your file's name
  3. num = INT(RND * 1000) + 1 'Make an integer number from 1 to 1000.
  4. x = x + 1
  5. INPUT #1, w$
  6. IF x = num THEN
  7.  
Hope you understood xD!

Don't be surprised no one posted code, because it does look like a homework assignment and rotebrO hasn't denied it (yet).

I don't think your code example will work for him, did it work for you? Looks like you are missing a DO to increase x and to EXIT when x hits the random number.

Also I think Hangman would want to be played more than once, so best to load the words into an array, you could even check off words you've done already so not replayed.
My bad xD, I forgot to write DO WHILE NOT EOF(1) xD. I didn't check it as I was using iPad at that moment lol.

-Prithak
CLS
IF computer$ = "ON" THEN
me$ = "Happy!"
ELSE
me$ = "Time To Draw!"
END IF
END

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: input random word from txt-file?
« Reply #11 on: February 07, 2019, 11:21:20 am »
Prithak,

It is good you wanted to help.

I have actually created my own version of Hangman on QB64 since last I posted on this thread and am waiting for progress report from rotebrO before any more discussion or posts, but it's hard for a posting freak to be quiet and wait for the guy to figure things out on his own.

roebrO, I am wondering if you got stuck and gave up or found your own way through. Inquiring minds need to know! ;-))

I have successfully tried an entirely different way to build a word list. I extracted the words from Pete's speech made in reply #6 of this thread. I recorded into one very long string called PetesSpeech$ and put it at start of program. With rule of 6 letters or more, non repeating words the code extracted 55 words as I recall. One word is suspect PARTOK? ;-)) They actually make for a quick fun game. I skipped the graphics and just spell out the word Hanged! in a "gallows report" line.

I was going to do a random selection from a dictionary but thought "Nope, Hangman needs themes." So now I am thinking of Wheel of Fortune hybrid of Hangman, maybe using what we have learned of linked lists to store phrases or quotes... hmm? I will probably use my Split routine that takes a string and a delimiter and produces an array of the pieces cut out by the delimiter.

« Last Edit: February 07, 2019, 11:25:43 am by bplus »