[Tech] Pascal Fun!

Nanor

Well-Known Member
So I have my Pascal exam (why are you learning Pascal?) in a month and I'm trying to get myself fully to grips with all aspects I'm required to know.

The Program

The program is a simple one. It consists of reading, writing, global variables and so forth. Our exam will test us to maintain it or add to it. As you will see it's not terribly well written with a lot that could be changed.

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;
  PhraseHasBeenSet : Boolean;
  PhraseGuessed : Boolean;
  Choice : Integer;
  GuessStatusArray : TGuessStatusArray;
  LettersGuessedArray : TLettersGuessedArray;
  NextGuessedLetter : Char;
  Index : Integer;

Procedure DisplayMenu;
  Begin
    Writeln('__________________________________');
    Writeln;
    Writeln('1. SETTER - Makes new word/phrase');
    Writeln;
    Writeln('2. USER - Next letter guess');
    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);
      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);
  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;

{ 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 = 5) And (PhraseGuessed = False)
      Then
        Begin
          Writeln('You have not completed this word/phrase...Press return to exit');
          Readln;
        End;
  Until Choice = 5;

End.
169 lines of simplicity. Along with the program we were given a text file with a number of different phrases on it. It seems obvious that we're gonna be asked to locate said file and read phrases to guess or write phrases to be guessed at a later date. I'm currently working at that now.

How you can help

Well, should you be so kind to help me I need questions based around these things:
  • Records
  • Filehandling
  • Reading
  • Writing
  • Procedures
  • Functions
For example: "Add a procedure to count the number of attempts the user has made at guessing the letter and if it gets to 8 then wipe their hard drive".

So, hit me!
 
Top