Displaying Guesses

[code]Urgent Plz..
I know the exam is only 3 days ahead.. But I really dont know what to do..
1) How do u display all the guessed Letters?
2) How to limit the number of incorrect guesses?!
Plz help me.. mates.. Thankyou!


Program Hangman; {this is a hangman program}

Uses
Wincrt, Strings; {clears the screen and displays the program for the user to work in }

Type {defines the type for the specified word}
TGuessStatusArray = Array[1..20] Of Char; {TGuessStatusArray and
TLettersGuessedArray are defined as the array of characters}
TLettersGuessedArray = Array[1..26] Of Char;

Var {the global variables are declared here- these variables can be used throughout the program}
NewPhrase : String; {newphrase is assigned as string}
PhraseHasBeenSet : Boolean; {PhraseHasBeenSet and PhraseGuessed are defined as boolean}
PhraseGuessed : Boolean;
Choice : Integer; {choice is defined as the integer}
GuessStatusArray : TGuessStatusArray; {GuessStatusArray is assigned to be the type of TGuessStatusArray }
LettersGuessedArray : TLettersGuessedArray; {LettersGuessedArray is assigned to be the type of TLettersGuessedArray }
NextGuessedLetter : Char; {xtGuessedLetter is assigned as char}
Index : Integer; {dex is assigned as integer}

Procedure DisplayMenu;{this is a procedure for displaying the menu}
Begin {the procedure begins here}
Writeln('__________________________________');
Writeln;
Writeln('1. SETTER - Makes new word/phrase');
Writeln; {displays the menu for the user}
Writeln('2. USER - Next letter guess');
Writeln;
Writeln('5. End');
Writeln;
End; {the procedure ends here}




Function GetNewPhrase : String; {This is a function of string}
Var {the local variables are declared here- it could be used only in this function}
PhraseOK : Boolean; {PhraseOK is assigned as a boolean}
ThisNewPhrase : String; {ThisNewPhrase is assigned as string}

Begin { the function begins here}
Repeat {the following steps will be repeated until PhraseOK is true}
Write('Key in new phrase ...(letters and any Spaces) '); {asks the user to enter any letter including spaces
by displaying this message}
Readln(ThisNewPhrase); {enables the user to enter the data}
If Length(ThisNewPhrase) < 10 {if the user entered less number fo letter
i.e. below 10, the following process will take place}
Then
Begin {the process begins here}
PhraseOK := False; {phrase becomes false because of the insufficient number of letters}
Writeln('Not enough letters ... '); {it also displays the message for the user }
{ possible further validation check(s) }
End {the process ends here}
Else {if the user entered the sufficient number of letters i.e. more than 10 characters, then
the following process takes place}
Begin {the process begins here}
PhraseOK := True; {the PhraseOK will be true}
GetNewPhrase := ThisNewPhrase; {GetNewPhrase is set to be ThisNewPhrase}
End; {the process ends here}
Until PhraseOK = True; {the whole repetition process will be repeating untill PhraseOK is true}
End; {the function ends here}


Procedure SetUpGuessStatusArray(NewPhrase : String; {this is a procedure to display the asterisk}
Var GuessStatusArray : TGuessStatusArray);
Var {this is a local variable which could be used only in this procedure}
Position : Integer; {position is assigned as integer}
Begin {the procedure begins here}
For Position := 1 To Length(NewPhrase) {Identifies the number of characters entered by the user in the begining }
Do
Begin
If NewPhrase[Position] = ' ' {if in the word, a space occured then it will be stored as a space}
Then GuessStatusArray[Position] := ' '
Else GuessStatusArray[Position] := '*'; {if its only character and not space,
then it will be stored as an asterisk}
End;
End; {the procedure ends here}

Procedure DisplayCurrentStatus(PhraseLength : Byte; {this is a procedure for displaying the
current status of the hangman, it shows the previously guessed alphabets.}
GuessStatusArray : TGuessStatusArray);
Var {this is a local variable which could be used only in this procedure}
Position : Integer; {Position is assigned as integer}
Begin {the procedure begins here}
For Position := 1 To PhraseLength {identifies the number of characters entered by the user}
Do Write(GuessStatusArray[Position]); {displays the correctly guessed characters}
Writeln; {a line space}
End; {the procedure ends here}

Function GetNextLetterGuess : Char; {this is a function of char}
Var {local variables are declared here}
Position : Integer; {position is assigned as integer}
GuessedLetter : Char; {guessed letter is assigned as char}

Begin {the function begins here}
Writeln; { a line space}
Write('Next guess ? ');{questions the user for the next guess}
Readln(GuessedLetter); {enables the user to enter the guess}
GetNextLetterGuess := GuessedLetter; {GetNextLetterGuess is assigned to GuessedLetter}
End; {the function ends here}


Function AllLettersGuessedCorrectly(GuessStatusArray: TGuessStatusArray; {this is a function of Boolean}
NewPhrase : String) : Boolean;
Var {thr local variables are declared here}
Position : Integer; {position is assigned as integer}
MissingLetter : Boolean; {missing letter is assigned as boolean}

Begin {the function begins here}
MissingLetter := False; {at the beginning, the missing letter is set to be false}
Position := 1; {position is set to be 1}
Repeat {the following process is set to repeat}
If GuessStatusArray[Position] <> NewPhrase[Position] {if GuessStatusArray[Position] is not equal to NewPhrase[Position]}
Then MissingLetter := True {then the missing letter becomes true}
Else Position := Position+1; {if the 2 variables are not equal then the
Position value will change by adding 1 to itself}
Until (MissingLetter = True) or (Position = Length(NewPhrase)+1);
{the process loops around until the missing letter is true or Position is Length(NewPhrase)+1}

If MissingLetter = False {if the missing letter is false}
Then AllLettersGuessedCorrectly := True {then the all the letters that the user types becomes true which means right}
Else AllLettersGuessedCorrectly := False; {if not the guesses made by the user becomes false
which means they are incorrect}
End; {the function ends here}

{ Main program block starts here }
Begin
PhraseHasBeenSet := False; {Phrasehasbeenset is set to be false}
Repeat {the following process repeats}
DisplayMenu; {compiles the codes from this procedure and displays them here}
Write('Choice? '); {displays the choice}
Readln(Choice); {enables the user to enter the choice}

If Choice = 1 {of the user entered choice as 1, then the following process takes place}
Then
Begin {the process begins here}
NewPhrase := GetNewPhrase; {Newpharse is set to be GetNewPhrase}
SetUpGuessStatusArray(NewPhrase, GuessStatusArray); {compiles the
codes written in this procedure and displays them here}
PhraseHasBeenSet := True; { the booloean variable PhraseHasBeenSet becomes true at this stage}
End; {the process for choice 1 ends here}

If Choice = 2 {of the user entered choice as 2, then the following process takes place}
Then
Begin {the process begins here}
If PhraseHasBeenSet = True {the phrase the user set becomes active}
Then
Begin {then the following process begins here}
DisplayCurrentStatus(Length(NewPhrase), GuessStatusArray); {compiles the codes written in this procedure}
NextGuessedLetter := GetNextLetterGuess;{nextGuessedLetter is saved as GetNextLetterGuess}
For Index := 1 To Length(NewPhrase) {for the variable index all the characters that the user has entered
from 1 performes the following process}
Do
If NextGuessedLetter = NewPhrase[Index] {if the next letter the user guesses
is the same as the Newphrase[index]}
Then GuessStatusArray[Index] := NextGuessedLetter; {if the previous line was the case then,
GuessStatusArray[index] is the same as
NextGuessedLetter is the next letter guessed}
DisplayCurrentStatus(Length(NewPhrase), GuessStatusArray); {compiles the codes written in the procedure
and displays them here}
PhraseGuessed := AllLettersGuessedCorrectly(GuessStatusArray,NewPhrase); {compiles the codes written
in the procedure and displays them here}
If PhraseGuessed = True {if the user has guessed all the characters correctly then the
following process takes place}
Then Writeln('You have guessed correctly'); {displays the message that the user has entered all
the characters successfully }
End
Else Writeln('The setter has not specified the word/phrase ..'); {if not the program displays a message saying that
the
user has entered a different word than the
word set by the setter}
End; {the loop ends here}

If (Choice = 5) And (PhraseGuessed = False) {if the user entered choice 5 or if the phraseguessed is not true then the
following
process takes place}
Then
Begin {the process begins here}
Writeln('You have not completed this word/phrase...Press return to exit'); {the program displays a message saying
that the
user has not completed the word and asks
the
user to exit}
Readln; {takes the user to the next line of codes in the program}
End; {the process ends here}
Until Choice = 5; {the whole process repeats until the user has entered their choice as 5}

End. {the program ends here}[/code]

Comments

  • Thanks for using CODE tags, but you need to format your code. Without formatting it makes it very unreadable. I have posted help on this same topic in the Hangman Game thread. Look there for your answers.
  • Thanks mate.. I figured out the rest.. I only want to know how to limit the number of guesses.. Eg: The user is allowed only 10 INCORRECT letters. Please could u help me.. Time is washing away like anything.. It only 3 days left for the exam.. Please HELP ME!!!
  • This post has been deleted.
  • Create a variable of type integer. In your phrase set procedure, set it to 10. Everytime they guess a letter that is not in the puzzle, subtract 1. When it hits 0, display your message and unset the puzzle (and display the correct answer if desired)
Sign In or Register to comment.

Howdy, Stranger!

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

Categories