With Lazarus: program Hangman; uses SysUtils; const MaxIncorrectGuesses = 6; function FindWordToGuess: string; var Words: array[1..10] of string; WordIdx: integer; begin Words[1] := 'Gato'; Words[2] := 'Seis'; Words[3] := 'Koala'; Words[4] := 'Sol'; Words[5] := 'Quebra'; Words[6] := 'Forca'; Words[7] := 'Flor'; Words[8] := 'Jogo'; Words[9] := 'Pascal'; Words[10] := 'Habita'; Randomize; WordIdx := Random(6) + 1; Result := UpperCase(Words[WordIdx]); end; procedure DrawHangman (Wrongs: integer); {draw a gallow and an increasing hangman on wrong guesses} var Members: array[1..6] of string; I: integer; begin Members[1] := '| o '; Members[2] := '| /'; Members[3] := '|'; Members[4] := '\'; Members[5] := '| / '; Members[6] := '\'; Writeln('____'); Writeln('| |'); if Wrongs 0 then begin if Wrongs >= 1 then Writeln(Members[1]); if Wrongs >= 2 then Write(Members[2]); if Wrongs >= 3 then Write(Members[3]); if Wrongs >= 4 then Writeln(Members[4]); if Wrongs >= 5 then Write(Members[5]); if Wrongs >= 6 then Writeln(Members[6]); end; Writeln; end; function StringContains(const S: string; C: char): Boolean; {searches string S for character C} var I: integer; begin for I := 1 to Length(S) do if S[I] = C then Exit(True); Result := False; end; procedure PlayGame; var SecretWord: string; {word to guess} YourWord: string; {word based on user guesses} AllLetters: string; {letters guessed by player} Guess: char; {players next guess} GuessesLeft: integer; {number of guesses left} WrongLetters: integer; {number of wrong letters guessed, to build the hangman} I: integer; UpdateCount: integer; {number of letters replaced in the word, based on guess} begin GuessesLeft := MaxIncorrectGuesses; Writeln('Welcome to the Hangman game!'); Writeln('You have ', GuessesLeft, ' tries to guess the word before hanging'); Writeln('Each time you guess incorrectly, you take another step into the gallows.'); DrawHangman(WrongLetters); SecretWord := FindWordToGuess; WrongLetters := 0; for I := 1 to Length(SecretWord) do YourWord := YourWord + '.'; Randomize; {place one hint letter in a random position of the secret word} I := Random(Length(SecretWord)) + 1; YourWord[I] := SecretWord[I]; repeat {display game state information} Writeln('Word: ', YourWord, ' | Guesses left: ', GuessesLeft, ' | Letters used: ', AllLetters); Writeln('Enter a letter:'); ReadLn(Guess); Guess := UpCase(Guess); if not (Guess in ['A'..'Z']) then WriteLn('Invalid input. Please enter a letter (A-Z).') else if StringContains(AllLetters, Guess) then Writeln('Doh! You have already tried that letter.') else begin UpdateCount := 0; AllLetters := AllLetters + Guess; for I := 1 to Length(SecretWord) do if SecretWord[I] = Guess then begin YourWord[I] := Guess; Inc(UpdateCount); end; if UpdateCount = 0 then begin Dec(GuessesLeft); Inc(WrongLetters); Writeln('Sorry! Wrong guess'); DrawHangman(WrongLetters); end end; until (GuessesLeft = 0) or (YourWord = SecretWord); if GuessesLeft > 0 then WriteLn('Congratulations! You guessed the word: ', SecretWord) else begin WriteLn('Game Over! The word was: ', SecretWord); WriteLn('You are now hanging out to dry!'); end; end; begin try PlayGame; ReadLn; { TODO -oUser -cConsole Main : Insert code here secret word := find word to guess show one hint letter at a random position loop: ask the user to guess a letter enter a letter does this letter appear in the word if yes then replace all occurrences else increment a guess counter and build a hanging man in parts until word guessed or number of tries >= max tries } except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.