- Program Hangman; { Daniel Neville }
- {
- 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;}
- wincrt,
- strings; {changed to work on our pascal compiler CT}
- Type
- TGuessStatusArray = Array[1..20] Of Char;
- TLettersGuessedArray = Array[1..26] Of Char;
- Var
- NewPhrase : String;
- PhraseHasBeenSet : Boolean;
- PhraseGuessed : Boolean;
- Choice : Integer;
- GuessStatusArray : TGuessStatusArray;
- LettersGuessedArray : TLettersGuessedArray;
- NextGuessedLetter : Char;
- Index : Integer;
- CompleteGuess : String;
- DataFile : Text;
- RandomPhrases : Array[1..100] of String;
- NumberOfPhrases : Integer;
- Guesses : Integer;
- Procedure DisplayMenu;
- Begin
- Writeln('__________________________________');
- Writeln;
- Writeln('1. SETTER - Makes new word/phrase');
- Writeln;
- Writeln('2. USER - Next letter guess');
- Writeln;
- Writeln('3. GUESSES - Make a full word guess');
- Writeln;
- Writeln('4. RANDOM PHRASE - Choose a random phrase from the text file.');
- Writeln;
- Writeln('5. End');
- Writeln;
- End;
- Function GetNewPhrase : String;
- Var
- PhraseOK : Boolean;
- ThisNewPhrase : String;
- Begin
- Repeat
- Write('Key in new phrase ...(letters and any Spaces) ');
- Readln(ThisNewPhrase);
- For Index := 1 to Length(ThisNewPhrase) do
- ThisNewPhrase[Index] := UpCase(ThisNewPhrase[Index]);
- 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 SetUpGuessedLettersArray;
- Var
- Position : Integer;
- Begin
- For Position := 1 to 26 do
- LettersGuessedArray[Position] := '0';
- End;
- Procedure SetUpGuessStatusArray(NewPhrase : String;
- Var GuessStatusArray : TGuessStatusArray);
- 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 := UpCase(GuessedLetter);
- LettersGuessedArray[Ord(UpCase(GuessedLetter))-64] := '1';
- 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 SetUpTextFile(FileLocation : String; Var DataFile : Text);
- Var
- S : String;
- I : Integer;
- Begin
- Assign(DataFile, 'N:\phrases.txt');
- Reset(DataFile);
- I := 0;
- While not EoF(DataFile) do begin
- Inc(I);
- ReadLn(DataFile, S);
- RandomPhrases[I] := S;
- NumberOfPhrases := I;
- End;
- Close(DataFile);
- End;
- Procedure ShowGuessedLetters;
- Var
- I : Integer;
- Begin
- For I := 1 to 26 do
- If(LettersGuessedArray[I] = '1') then
- Write(Chr(64 + I) , ' ');
- WriteLn;
- End;
- { Main program block starts here }
- Begin
- Randomize;
- PhraseHasBeenSet := False;
- SetUpTextFile('N:\phrases.txt', DataFile);
- Repeat
- DisplayMenu;
- Write('Choice? ');
- Readln(Choice);
- If Choice = 1
- Then
- Begin
- NewPhrase := GetNewPhrase;
- SetUpGuessStatusArray(NewPhrase, GuessStatusArray);
- SetUpGuessedLettersArray;
- Guesses := 0;
- PhraseHasBeenSet := True;
- End;
- If Choice = 2
- Then
- Begin
- If PhraseGuessed = True then
- Write('Phrase has already been guessed.')
- Else
- If PhraseHasBeenSet = True
- Then
- Begin
- DisplayCurrentStatus(Length(NewPhrase), GuessStatusArray);
- Write('Guesses so far: ', Guesses, ' --- Guessed letters: ');
- ShowGuessedLetters;
- NextGuessedLetter := UpCase(GetNextLetterGuess);
- For Index := 1 To Length(NewPhrase)
- Do
- If NextGuessedLetter = NewPhrase[Index]
- Then GuessStatusArray[Index] := NextGuessedLetter;
- Inc(Guesses);
- DisplayCurrentStatus(Length(NewPhrase), GuessStatusArray);
- PhraseGuessed := AllLettersGuessedCorrectly(GuessStatusArray,NewPhrase);
- If PhraseGuessed = True
- Then Writeln('You have guessed correctly. Guesses used: ', Guesses);
- End
- Else Writeln('The setter has not specified the word/phrase ..');
- End;
- If Choice = 3
- Then
- Begin
- If PhraseGuessed = True then
- Write('Phrase has already been guessed.')
- Else
- If PhraseHasBeenSet = True
- Then
- Begin
- Write('Enter your complete guess: ');
- ReadLn(CompleteGuess);
- For Index := 1 to Length(CompleteGuess) do
- CompleteGuess[Index] := UpCase(CompleteGuess[Index]);
- If(CompleteGuess = NewPhrase)
- Then Begin
- For Index := 1 to Length(NewPhrase) Do
- GuessStatusArray[Index] := NewPhrase[Index];
- PhraseGuessed := True;
- WriteLn('You have guessed correctly. Guesses used: ', Guesses);
- WriteLn;
- End
- else
- WriteLn('Incorrect guess. Returning to menu...');
- WriteLn;
- End
- Else Writeln('The setter has not specified the word/phrase ..');
- End;
- If Choice = 4 Then
- Begin
- NewPhrase := RandomPhrases[Random(NumberOfPhrases - 1) + 1];
- SetUpGuessStatusArray(NewPhrase, GuessStatusArray);
- SetUpGuessedLettersArray;
- Guesses := 0;
- PhraseHasBeenSet := True;
- End;
- 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;
- DoneWinCRT;
- End.
Tuesday, 1 February 2011
2009 material
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment