Posted: Mon Jun 06, 2005 4:56 pm Post subject: Hangman Help!!!
if i was doing a hangman program where the computer choses the phrase by random from a datafile,how would i do that and how do i put it in?plz and thanku=)
Sponsor Sponsor
StarGateSG-1
Posted: Mon Jun 06, 2005 5:01 pm Post subject: (No subject)
The simple way, which I think is better for you would be to do a random variable and then have each outcome load a differnet text, look into
code:
put :
get :
MysticVegeta
Posted: Mon Jun 06, 2005 7:03 pm Post subject: (No subject)
I am just typing the code without running so there might be some typos.
code:
var words : array 1..totalnumofwords of string
var randword : string
var streamno : int
open : streamno, "filename.txt", get
for s : 1..totalnumofwords
get : streamno, words(s)
end for
randword := words(Rand.Int(1, upper(words)))
azn_babie07
Posted: Tue Jun 07, 2005 11:40 am Post subject: (No subject)
MysticVegeta wrote:
I am just typing the code without running so there might be some typos.
code:
var words : array 1..totalnumofwords of string
var randword : string
var streamno : int
open : streamno, "filename.txt", get
for s : 1..totalnumofwords
get : streamno, words(s)
end for
randword := words(Rand.Int(1, upper(words)))
wow!thnx so much!lolz,well..there are 2 errors on 'total num of words'...
MysticVegeta
Posted: Tue Jun 07, 2005 3:31 pm Post subject: (No subject)
that is the amound of words you have in the textfile.
azn_babie07
Posted: Wed Jun 08, 2005 11:37 am Post subject: (No subject)
oo!!!wow,i could learn a lot from u,lolz.i noe u have 2 declare it put how would i put it in turing?
MysticVegeta
Posted: Wed Jun 08, 2005 3:10 pm Post subject: (No subject)
Posted: Wed Jun 08, 2005 3:22 pm Post subject: (No subject)
MysticVegeta wrote:
ok, i have made a sample program for you ->
i dont see it... sry=p
Sponsor Sponsor
Cervantes
Posted: Wed Jun 08, 2005 5:35 pm Post subject: (No subject)
Mystic: I think you knew this was coming.
You don't want to have to count how many words are in your file, then code your program appropriately. Instead, we let the program do it all for us.
Time to unleash the powers of the flexible array!
Turing:
var streamno :int%Stream number to import file in var words :flexiblearray1.. 0ofstring%Stores the words in the file var randomword :string%The random word that will be selected
open: streamno, "words.txt",get%Opening "words.txt" should be in the same folder
loop exitwheneof(streamno) new words, upper(words) + 1%incriment the upper bounds of the array get: streamno, words (upper(words))%Each word is stored in the array endloop
randomword := words (Rand.Int (1, upper(words)))%Random word is selected from the words in the array.
put randomword %Displaying random word (duh!)
I'm sure you were just trying to keep it simple, Mystic, and with regard to that, I quite agree. But I think the correct version should also be posted, for anyone else who may be interested, as well as to give azn_babie07 a little spur to learn flexible arrays (assuming he doesn't know them, that is).
Cheers!
MysticVegeta
Posted: Wed Jun 08, 2005 5:39 pm Post subject: (No subject)
ah flexible array to the rescue. Nice. I thought that she knew how many words there will be in the file lol.
Anyways, flexible arrays, super method to do this because in the end if you think you have to add or decrease some words from the text file, you wont have to change the arrays! Just the words in the file.
azn_babie07
Posted: Thu Jun 09, 2005 12:13 pm Post subject: (No subject)
thnx so much!now it works,lolz.but how do i get rid of the menu in the beginning that asks u to make a phrase?and wen i actually play...it still doesznt pic by random..i got rid of the loop but wen i run it,it was blank...
azn_babie07
Posted: Thu Jun 09, 2005 12:14 pm Post subject: (No subject)
if itll help...heres the program..
%make datafile***%
var input : string (1)
var puzzle : string := ""
var unString, unDisplay : string := ""
var bodyCount : int := 0
var lettersUsed : string := ""
var choiceDisplay : string := ""
var playAgain : string (1)
var winID : int := Window.Open ("position:center;middle,graphics:785;500")
var streamno : int %Stream number to import file in
var words : flexible array 1 .. 0 of string %Stores the words in the file
var randomword : string %The random word that will be selected
open : streamno, "data1.txt", get %Opening "words.txt" should be in the same folder
loop
exit when eof (streamno)
new words, upper (words) + 1 %incriment the upper bounds of the array
get : streamno, words (upper (words)) %Each word is stored in the array
end loop
randomword := words (Rand.Int (1, upper (words))) %Random word is selected from the words in the array.
put randomword %Displaying random word
proc PutCenter (inputString : string)
Text.Locate (Text.WhatRow, (maxcol div 2) - (length (inputString) div
2))
put (inputString)
end PutCenter
% Displays a blinking message
proc BlinkMessage (row : int, blinkTimes : int, message : string,
blinkColour : int)
colour (blinkColour)
for i : 1 .. blinkTimes * 2
if i mod 2 = 1 then
Text.Locate (row, 1)
put repeat (" ", maxcol)
Text.Locate (row, 1)
PutCenter (message)
else
Text.Locate (row, 1)
put repeat (" ", maxcol)
Text.Locate (row, 1)
PutCenter (repeat (" ", length (message)))
end if
View.Update
delay (300)
end for
colour (13)
end BlinkMessage
% state - a value from 0-6. 0 = no hangman, 1 = head, 2 = torso, 3 = r arm, 4 = l arm, 5 = r leg, 6 = l leg
proc DrawHangman (xLeftFoot : int, yLeftFoot : int, headRad : int, legHeight
: int, torsoHeight : int, footToTorso : int, armsOut : int, armsVertOffset :
int, state : int, colorOf : int)
% Left Leg (6)
if state > 5 then
Draw.Line (xLeftFoot, yLeftFoot, xLeftFoot + footToTorso, yLeftFoot +
legHeight, colorOf)
end if
% Right Leg (5)
if state > 4 then
Draw.Line (xLeftFoot + footToTorso * 2, yLeftFoot, xLeftFoot + footToTorso,
yLeftFoot + legHeight, colorOf)
end if
% Left Arm (4)
if state > 3 then
Draw.Line (xLeftFoot + footToTorso, yLeftFoot + legHeight + (torsoHeight -
(torsoHeight div 6)), (xLeftFoot + footToTorso) - armsOut, (yLeftFoot +
legHeight + (torsoHeight - (torsoHeight
div 6))) + armsVertOffset, colorOf)
end if
% Right Arm (3)
if state > 2 then
Draw.Line (xLeftFoot + footToTorso, yLeftFoot + legHeight + (torsoHeight -
(torsoHeight div 6)), (xLeftFoot + footToTorso) + armsOut, (yLeftFoot +
legHeight + (torsoHeight - (torsoHeight
div 6))) + armsVertOffset, colorOf)
end if
% Torso (2)
if state > 1 then
Draw.Line (xLeftFoot + footToTorso, yLeftFoot + legHeight, xLeftFoot +
footToTorso, (yLeftFoot + legHeight) + torsoHeight, colorOf)
end if
% Head (1)
if state >= 1 then
Draw.Oval (xLeftFoot + footToTorso, yLeftFoot + legHeight + torsoHeight +
headRad, headRad, headRad, colorOf)
end if
end DrawHangman
% Set colors and other graphics
View.Set ("nobuttonbar")
View.Set ("Title:Hangman")
RGB.SetColor (1, 0.11328125, 0.15234375, 0.1953125)
RGB.SetColor (2, 0.90234375, 0.99609375, 0.234375)
colourback (black)
colour (white)
cls
loop
Text.Locate (2, 3)
put "Please enter your phrase here and save Hilary Duff from the kidnappers! But only ", maxcol div
2, " characters=)"
Text.Locate (3, 3)
View.Set ("cursor,echo")
get puzzle : *
exit when length (puzzle) <= maxcol div 2 and puzzle not= ""
if puzzle = "" then
BlinkMessage (5, 3, "You must enter a phrase!", red)
else
BlinkMessage (5, 3, "Your phrase is too long!", red)
end if
Text.Locate (3, 3)
put repeat (" ", maxcol - Text.WhatCol)
end loop
for i : 1 .. length (puzzle)
if ord (puzzle (i)) > 96 and ord (puzzle (i)) < 123 then
puzzle := puzzle + chr (ord (puzzle (i)) - 32)
else
puzzle := puzzle + puzzle (i)
end if
end for
puzzle := puzzle ((length (puzzle) div 2) + 1 .. *)
unString := ""
for i : 1 .. length (puzzle)
if ord (puzzle (i)) > 64 and ord (puzzle (i)) < 91 then
unString := unString + "_"
else
unString := unString + puzzle (i)
end if
end for
View.Set ("offscreenonly")
loop
cls
unDisplay := ""
for i : 1 .. length (unString)
unDisplay := unDisplay + unString (i) + " "
end for
unDisplay := unDisplay (1 .. * -1)
Text.Locate (3, 1)
PutCenter (unDisplay)
Draw.Box (View.maxx div 2 - ((View.maxx div 3) div 2), 130, View.maxx div 2
+ ((View.maxx div 3) div 2), 134, 2)
Draw.Box (View.maxx div 2 - ((View.maxx div 3) div 2) + 10, 134, View.maxx
div 2 - ((View.maxx div 3) div 2) + 14, 364, 2)
Draw.Box (View.maxx div 2 - ((View.maxx div 3) div 2) + 14, 360, View.maxx
div 2 - ((View.maxx div 3) div 2) + 140, 364, 2)
Draw.Line (View.maxx div 2 - ((View.maxx div 3) div 2) + 14, 324, View.maxx
div 2 - ((View.maxx div 3) div 2) + 50, 360, 2)
Draw.Line (View.maxx div 2 - ((View.maxx div 3) div 2) + 138, 360,
View.maxx div 2 - ((View.maxx div 3) div 2) + 138, 310, 2)
exit when bodyCount = 6
exit when unString = puzzle
choiceDisplay := ""
for i : ord ("A") .. ord ("Z")
if index (lettersUsed, chr (i)) > 0 then
choiceDisplay := choiceDisplay + " "
else
choiceDisplay := choiceDisplay + chr (i)
end if
choiceDisplay := choiceDisplay + " "
end for
locate (maxrow - 3, 1)
PutCenter (choiceDisplay)
loop
Text.Locate (5, 1)
PutCenter ("Enter your guess but be careful, you only have " + intstr (6
- bodyCount) + " chances before they hang her.")
View.Set ("nocursor,noecho")
View.Update
getch (input)
exit when ord (input) > 64 and ord (input) < 91
% Converts input to caps (if lower case)
if ord (input) > 96 and ord (input) < 123 then
input := chr (ord (input) - 32)
% This sees if letter has been used
if index (lettersUsed, input) > 0 then
BlinkMessage (5, 3, "You've already used that letter!", yellow)
end if
% If letter is not in lettersUsed, add it
if index (lettersUsed, input) = 0 then
lettersUsed := lettersUsed + input
exit
end if
else
BlinkMessage (5, 3, "Must be a letter!", yellow)
end if
end loop
% If letter is there, go through and change unString
if index (puzzle, input) > 0 then
for i : 1 .. length (puzzle)
if puzzle (i) = input then
unString := unString (1 .. i - 1) + input + unString (i + 1 .. *)
end if
end for
elsif index (puzzle, input) = 0 then
bodyCount += 1
end if
View.Update
end loop
% Show any letters not discovered
locate (3, maxcol div 2 - (length (unDisplay) div 2))
for i : 1 .. length (unString)
if unString (i) = "_" then
colour (white)
put puzzle (i) ..
colour (2)
else
put unString (i) ..
end if
put " " ..
end for
% Victory/Loss Screen
colour (brightmagenta)
locate (5, 1)
case bodyCount of
label 0 :
PutCenter ("Good job, the police came before they could hurt her!")
label 1 :
PutCenter ("Hilary shakes her head and thanks you.")
label 2 :
PutCenter ("Hiliary looks a bit shaken but she'll be fine.")
label 3 :
PutCenter ("Hilary doesn't look too pleased but thanks you none the less...")
label 4 :
PutCenter ("Hilary doesn't even give you a oassing glance...")
label 5 :
PutCenter ("Hilary was rushed to the hospital but recovered.")
label 6 :
PutCenter ("The kidnappers laugh at what's left of Hilary.")
end case
colour (13)
locate (maxrow - 3, 1)
PutCenter ("Care to Play Again? (Y/N)")
View.Update
loop
getch (playAgain)
exit when (playAgain = "Y" or playAgain = "y") or (playAgain = "N" or
playAgain = "n")
BlinkMessage (maxrow - 2, 2, "Must be 'Y' or 'N'", 13)
end loop
View.Update
exit when playAgain = "n" or playAgain = "N"
end loop
cls
locate (4, 1)
PutCenter ("Thanks for playing! Please wait while we end the game...")
DrawHangman (335, 190, 15, 40, 50, 15, 10, -20, 6, red)
View.Update
delay (1000)
Window.Close (winID)
MysticVegeta
Posted: Thu Jun 09, 2005 4:11 pm Post subject: (No subject)
mind attaching the file? "datafile"
azn_babie07
Posted: Thu Jun 09, 2005 9:17 pm Post subject: (No subject)
well..i made the datafile using notepad,because thats what my teacher says..it has 8 phrases and i named it 'data1'. in the datafile...i only seperateted them with each column..for ex:
dghdhdf
dgdg
fhf dxhdxh fh
fhf fhfh fhfhgf gfh
...
MysticVegeta
Posted: Sat Jun 11, 2005 10:07 am Post subject: (No subject)
You see in the data file if you separate the words wih a space ->
asd dfg
It wil take one of the word in one element of the array
then,
words(1) = asd
words(2) = dfg
but
if you want them taken as one word then
code:
get : streamno, words(1):*
the ":*" function only works when you have both the words in one line in the text file
so words(1) = asd dfg
but if they are separated in by a line not by a space then