Computer Science Canada

Data files

Author:  Sevensins [ Wed Apr 09, 2008 8:51 am ]
Post subject:  Data files

How do I grab a random word from a text file of 30 some words and display it on turing?

Like...I'm trying to make a hangman game and I don't now how to grab a random word from this text file. I can only get the first word.

var word : string
var pos : int
var stream : int
var num : int
open : stream, "hangwords.dat", get
assert stream > 0


get:stream,word
put word

close : stream


some help would be appreciated O.o

Author:  BigBear [ Wed Apr 09, 2008 10:04 am ]
Post subject:  Re: Data files

if you get all the words in the text file then stop randomly at one word.
Turing:
loop
    exit when eof
    get : fn, word
    count += 1
    exit when count = Rand.Int (1, 30)
end loop

Just one way to do it

Author:  Sevensins [ Thu Apr 10, 2008 8:41 am ]
Post subject:  Re: Data files

Thanks.

Author:  Saad [ Thu Apr 10, 2008 2:41 pm ]
Post subject:  Re: Data files

BigBear @ Wed Apr 09, 2008 10:04 am wrote:
if you get all the words in the text file then stop randomly at one word.
Turing:
loop
    exit when eof
    get : fn, word
    count += 1
    exit when count = Rand.Int (1, 30)
end loop

Just one way to do it


That is a poor method, it isn't completely random as count might not be equal to a random number we will be using the last word. What you want to do is load the words into an array and pick a random index from 1 to the number of words

Author:  Nick [ Thu Apr 10, 2008 3:35 pm ]
Post subject:  RE:Data files

for i:1.. Rand.Int(1,30)
get : fn, word
end for

Author:  gitoxa [ Thu Apr 10, 2008 3:36 pm ]
Post subject:  Re: Data files

Saad @ Thu Apr 10, 2008 2:41 pm wrote:
What you want to do is load the words into an array and pick a random index from 1 to the number of words


Why store all the words into memory when they're already stored in the file? You're only going to be using one of the words per game.
In my opinion, BigBear's idea is decent, but his code... I understand what you were trying to do, but sit down and think through what you're program is going to do. Also two exit statements in one loop is poor structure. Spagetti code, huzzah!


Just a litte hint: Assign a random number to a variable outside the loop.

Nick beat me to it >.>


: