Hangman - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Hangman

oashaikhoashaikh Posts: 3Member
Hi all

The following code was given with the AQA computing exam last year, my teacher told me to make it functional

I've done this so far but i've got a problem

i made to functions, one to open a text file a count how many words there are and one to randomly select a line number,

My task is to make a function that will select the Random number and display the corresponding word, this also has to be done with the following function
[b]
[u]function SelectPhraseFromFile[/u][/b]
[u]
(The attachments has the text file included)[/u]

This is the code so far:

[code]Program Hangman;

{ Skeleton Program code for the AQA COMP1 Summer 2009 examination
this code should be used in conjunction with the Preliminary materials
written by the AQA COMP1 Programmer Team
developed in the Delphi 7 (Console Mode) programming environment (PASCAL)
the DisplayMenu procedure has deliberately omitted a menu choice 3 and 4 }


{$APPTYPE CONSOLE}

Uses
SysUtils,
StrUtils;

Type
TGuessStatusArray = Array[1..20] Of Char;
TLettersGuessedArray = Array[1..26] Of Char;
Var
NewPhrase : String; //Asks the User to insert a new phrase
PhraseHasBeenSet : Boolean; //Check to see whether the users input is valid
PhraseGuessed : Boolean; //Checks to see whether the prase has been guessed
Choice : Integer; //Waits for a choice from the user in the procedure 'DisplayMenu'
GuessStatusArray : TGuessStatusArray;
LettersGuessedArray : TLettersGuessedArray;
NextGuessedLetter : Char;
Index : Integer;
RandomPhrase:integer;
PhrasesInFile:integer;
SelectRandomPhrase:string;
count:integer;

Procedure DisplayMenu;
{Displays the menu when the program starts,
giving the user a choice of 1 to 5}
Begin
Writeln('__________________________________');
Writeln;
Writeln('1. SETTER - Makes new word/phrase');
Writeln;
Writeln('2. USER - Next letter guess');
writeln;
Writeln('3. USER - Make a complete word/phrase guess');
writeln;
writeln('4. Run Question 8 code');
writeln;
Writeln('5. End');
Writeln;
End;

{------------------------------------------------------------------------------}

Function GetNewPhrase : String;
{Asks the user to enter a phrase that is greater than 10}
Var
PhraseOK : Boolean;
ThisNewPhrase : String;

Begin
Repeat
Write('Key in new phrase ...(letters and any Spaces) ');
Readln(ThisNewPhrase);
If Length(ThisNewPhrase) < 10
Then
Begin
PhraseOK := False;
Writeln('Not enough letters ... ');
{ possible further validation check(s) }
End
Else
Begin
PhraseOK := True;
GetNewPhrase := ThisNewPhrase;
End;
Until PhraseOK = True;
End;

{------------------------------------------------------------------------------}

Procedure SetUpGuessStatusArray(NewPhrase : String;
Var GuessStatusArray : TGuessStatusArray);
{If the user enters a then a blank space is left,
otherwise an <*> is replaced by every letter}

Var
Position : Integer;
Begin
For Position := 1 To Length(NewPhrase)
Do
Begin
If NewPhrase[Position] = ' '
Then GuessStatusArray[Position] := ' '
Else GuessStatusArray[Position] := '*';
End;
End;

{------------------------------------------------------------------------------}

Procedure DisplayCurrentStatus(PhraseLength : Byte;
GuessStatusArray : TGuessStatusArray);
{}

Var
Position : Integer;
Begin
For Position := 1 To PhraseLength
Do Write(GuessStatusArray[Position]);
Writeln;
End;

{------------------------------------------------------------------------------}

Function GetNextLetterGuess : Char;
Var
Position : Integer;
GuessedLetter : Char;

Begin
Writeln;
Write('Next guess ? ');
Readln(GuessedLetter);
GetNextLetterGuess := GuessedLetter;
End;
{------------------------------------------------------------------------------}

Function AllLettersGuessedCorrectly(GuessStatusArray: TGuessStatusArray;
NewPhrase : String) : Boolean;
Var
Position : Integer;
MissingLetter : Boolean;

Begin
MissingLetter := False;
Position := 1;
Repeat
If GuessStatusArray[Position] <> NewPhrase[Position]
Then MissingLetter := True
Else Position := Position+1;
Until (MissingLetter = True) or (Position = Length(NewPhrase)+1);

If MissingLetter = False
Then AllLettersGuessedCorrectly := True
Else AllLettersGuessedCorrectly := False;
End;
{------------------------------------------------------------------------------}
procedure InputUsersCompletePhraseGuess;
begin
writeln;
writeln('Procedure InputUsersCompletePhraseGuess has been called');
writeln;
end;


{------------------------------------------------------------------------------}

[b]function CountPhrasesFromFile:integer;
var
myfile : textfile;
myfilename:string;
count:integer;
begin
myfilename:='MyPhrases.txt';
assignfile(myfile, myfilename);
reset(myfile);
count:=0;
while not eof(myfile) do
begin
readln(myfile,myfilename);
count:=count+1;
end;
closefile(myfile);
writeln;
writeln('There are ', count , ' words in MyPhrases.txt');
end;

{------------------------------------------------------------------------------}

function GenerateRandomPhraseNumber:integer;
var
myfile : textfile;
myfilename:string;
count:integer;
begin
myfilename:='MyPhrases.txt';
assignfile(myfile, myfilename);
reset(myfile);
count:=0;
while not eof(myfile) do
begin
readln(myfile,myfilename);
count:=count+1;
randomize;
end;

writeln;
writeln('Random word selected is on line: ' , random(count));
end;
{------------------------------------------------------------------------------}

function SelectPhraseFromFile;
begin
end;[/b][color=Blue][/color]




{------------------------------------------------------------------------------}
{ Main program block starts here }
Begin
PhraseHasBeenSet := False;
Repeat
DisplayMenu;
Write('Choice? ');
Readln(Choice);

If Choice = 1
Then
Begin
NewPhrase := GetNewPhrase;
SetUpGuessStatusArray(NewPhrase, GuessStatusArray);
PhraseHasBeenSet := True;
End;

If Choice = 2
Then
Begin
If PhraseHasBeenSet = True
Then
Begin
DisplayCurrentStatus(Length(NewPhrase), GuessStatusArray);
NextGuessedLetter := GetNextLetterGuess;
For Index := 1 To Length(NewPhrase)
Do
If NextGuessedLetter = NewPhrase[Index]
Then GuessStatusArray[Index] := NextGuessedLetter;
DisplayCurrentStatus(Length(NewPhrase), GuessStatusArray);
PhraseGuessed := AllLettersGuessedCorrectly(GuessStatusArray,NewPhrase);
If PhraseGuessed = True
Then Writeln('You have guessed correctly');
End
Else Writeln('The setter has not specified the word/phrase ..');
End;

If choice = 3
Then
Begin
InputUsersCompletePhraseGuess;
End;

I[b]f choice = 4
Then
Begin
PhrasesInFile:=CountPhrasesFromFile;
RandomPhrase:=GenerateRandomPhraseNumber;
SelectRandomPhrase:=SelectPhraseFromFile;
end;[/b]

If (Choice = 5) And (PhraseGuessed = False)
Then
Begin
Writeln('You have not completed this word/phrase...Press return to exit');
Readln;
End;
Until Choice = 5;

End.


[/code]

Comments

  • _Atex__Atex_ Posts: 163Member
    Here's a simple program which opens up a text file, then displays a random line. Just turn it into a function... BTW your subject was discussed here in detail about a year ago ;-)[code][color=Blue]var f:text;
    s:string;
    i,j,t:word;


    begin
    randomize;
    t:=0;
    assign(f,'< your filename here >');reset(f);
    while not eof(f) do begin
    readln(f);
    inc(t);
    end;
    i:=random(t+1);
    reset(f);
    for j:=1 to i-1 do
    readln(f);
    readln(f,s);
    writeln('Total lines: ',t,' Selected line: ',i,' ',s);
    readln;
    close(f);
    end.[/color][/code]
  • AlPinkAlPink Posts: 1Member
    Osama

    Have you done this yet?

    Mr A
Sign In or Register to comment.