1. Diese Seite verwendet Cookies. Wenn du dich weiterhin auf dieser Seite aufhältst, akzeptierst du unseren Einsatz von Cookies. Weitere Informationen

Hangman Programm

Dieses Thema im Forum "Pascal" wurde erstellt von LinaNeu, 5. Februar 2017.

  1. LinaNeu

    LinaNeu New Member

    hey kann mir jemand helfen ich muss für die schule hangman mit pascal programmieren und ich habe keine ahnung wie ich das machen muss wir haben erst mit dem programmieren angefangen und ich könnte echt hilfe gebrauchen
    schon mal danke im vorraus für eure hilfe
    LG Lina
  2. Markus.java

    Markus.java New Member

    hab das im Internet gefunden hab bei google nach dem Pascal quellcode gesucht. Hoffe ich konnte helfen habs aber nicht ausprobiert obs geht

    Code:
    Program Hangman;
    
    {$APPTYPE CONSOLE}
    
    uses
    SysUtils,
    StrUtils;
    
    Type
    TGuessStatusArray = Array[1..20] Of Char; //declares a new type of variable that counts letter from 1 to 20 of word
    TLettersGuessedArray = Array[1..26] Of Char; //declares a new type of variabel that counts letter 1 to 26 of word
    Var
    NewPhrase : String; //new variable string
    PhraseHasBeenSet : Boolean; //new variable boolean
    PhraseGuessed : Boolean; //new variable boolean
    Choice : Integer; //new variable integer
    GuessStatusArray : TGuessStatusArray; //new variable new variable type 1
    LettersGuessedArray : TLettersGuessedArray; // new variable
    NextGuessedLetter : Char; //new variable char
    Index : Integer; //new variable number
    
    Procedure DisplayMenu; // new procedure that will display the menu
    Begin
    Writeln('__________________________________'); //WTF looks pretty
    Writeln;
    Writeln('1. SETTER - Makes new word/phrase'); // tells the option
    Writeln;
    Writeln('2. USER - Next letter guess'); //tells the options
    Writeln;
    Writeln('5. End'); //tells the options
    Writeln;
    End;
    
    
    
    
    Function GetNewPhrase : String; //function for getting new phrase
    Var //local variables
    PhraseOK : Boolean; // creates a new boolean to check if the phrase is ok
    ThisNewPhrase : String; // Variable that stores new phrase
    
    Begin
    Repeat //repeat loop
    Write('Key in new phrase ...(letters and any Spaces) '); //asks the user to write new phrase
    Readln(ThisNewPhrase); //reads the line and puts it into the new phrase vaariable
    If Length(ThisNewPhrase) < 10 //If length of the variable is less than 10 then it
    Then
    Begin
    PhraseOK := False; // sets the boolean to false so it asks for another one
    Writeln('Not enough letters ... '); // Writes that there is not enough letters
    
    { possible further validation check(s) }
    End
    Else // if phrase is long enough
    Begin
    PhraseOK := True; //changes boolean to true
    GetNewPhrase := ThisNewPhrase; //assign the new phrase to get newphrase variable
    End;
    Until PhraseOK = True; //repeats until the phrase is long enough
    End;
    
    
    Procedure SetUpGuessStatusArray(NewPhrase : String; //Sets up quess phrase.
    Var GuessStatusArray : TGuessStatusArray); //display the variable
    var
    Position : Integer; //new variable that will show position of a letter
    Begin
    // FOR LOOP
    For Position := 1 To Length(NewPhrase) // from first letter to the last letter of the phrase
    Do
    Begin
    If NewPhrase[Position] = ' ' //if the new phrase position is a space
    Then GuessStatusArray[Position] := ' ' // then make it a space
    Else GuessStatusArray[Position] := '*'; // if it isnt space make it a star
    End;
    End;
    
    Procedure DisplayCurrentStatus(PhraseLength : Byte;
    GuessStatusArray : TGuessStatusArray);
    Var
    Position : Integer; // variable that will show position of a letter
    Begin
    // FOR LOOP
    For Position := 1 To PhraseLength // from first letter to length of phrase
    Do Write(GuessStatusArray[Position]); //writes quessstatus array
    Writeln; //leave a blank line
    End;
    
    Function GetNextLetterGuess : Char; // gets next letter quess
    Var
    Position : Integer;
    GuessedLetter : Char; //stores quessed letter as a character
    
    Begin
    Writeln; // leaves a blank line
    Write('Next guess ? '); // asks for a next letter
    Readln(GuessedLetter); //reades the letter
    GetNextLetterGuess := GuessedLetter; // makes getnextletterquess the letter inputted
    End;
    
    
    Function AllLettersGuessedCorrectly(GuessStatusArray: TGuessStatusArray;
    NewPhrase : String) : Boolean;
    Var
    Position : Integer; // counts position of a letter
    MissingLetter : Boolean; // looks if its a missing letter
    
    Begin
    MissingLetter := False; // sets the missing letter to false
    Position := 1; //starts from 1
    Repeat
    If GuessStatusArray[Position] <> NewPhrase[Position] //if the letter in quesstatus array equal to new phrase same position
    Then MissingLetter := True // It changes boolean to true
    Else Position := Position+1; //else it increments position
    Until (MissingLetter = True) or (Position = Length(NewPhrase)+1); // Does the loop until it reaches the end of phrase or it found letter
    
    If MissingLetter = False // if missing letter is still false
    Then AllLettersGuessedCorrectly := True //then all letters quessed correctly
    Else AllLettersGuessedCorrectly := False; //if it isnt false then all letters werent quessed
    End;
    
    { Main program block starts here }
    Begin
    PhraseHasBeenSet := False; //starts by indicating that there is no phrase
    Repeat
    DisplayMenu; //shows menu
    Write('Choice? '); //writes choice
    Readln(Choice); //reads into variable choice
    
    If Choice = 1 // if choice is 1 then it starts the get new phrase
    Then
    Begin
    NewPhrase := GetNewPhrase; // new phrase will be the outcome of function get newphrase
    SetUpGuessStatusArray(NewPhrase, GuessStatusArray); // start procedure
    PhraseHasBeenSet := True; //the phrase has been set
    End;
    
    If Choice = 2 // begins quess code
    Then
    Begin
    If PhraseHasBeenSet = True // if there is a new phrase then it continues to work
    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 ..'); // if there is no phrase it asks for one
    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.
    LinaNeu gefällt das.