Creating Mice and Stairs in Pascal | A Friendlier Hangman Game

  Рет қаралды 269

The Silver Pascal Coder

The Silver Pascal Coder

Күн бұрын

Пікірлер: 2
@pmralbuquerque
@pmralbuquerque 21 күн бұрын
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.
@silvercoder70
@silvercoder70 21 күн бұрын
love it!
Pointers in Pascal: The Basics Explained (Part 1)
6:40
The Silver Pascal Coder
Рет қаралды 148
Why do developers hate Rust?
8:20
Let's Get Rusty
Рет қаралды 153 М.
Ozoda - Alamlar (Official Video 2023)
6:22
Ozoda Official
Рет қаралды 10 МЛН
SLIDE #shortssprintbrasil
0:31
Natan por Aí
Рет қаралды 49 МЛН
She wanted to set me up #shorts by Tsuriki Show
0:56
Tsuriki Show
Рет қаралды 8 МЛН
Conditions and Loops in Modern Pascal - Simplified!
20:09
The Silver Pascal Coder
Рет қаралды 315
why rust libraries may never exist.
7:26
Low Level
Рет қаралды 286 М.
Simple Types in Pascal: Booleans, Integers, Floats, and Chars Explained
17:36
The Silver Pascal Coder
Рет қаралды 235
Using Advanced Records in Modern Pascal (Delphi)
15:05
The Silver Pascal Coder
Рет қаралды 453
Explaining Pointers Until I Go Insane
6:42
Mults
Рет қаралды 347 М.
A Day in the Life of a Japanese Hikikomori (Shut In)
14:32
Sean and Oreo
Рет қаралды 14 МЛН
Solving one of the logic puzzles of all time!
20:34
Sheafification of G
Рет қаралды 29 М.
Object-Oriented Programming in Pascal
13:54
The Silver Pascal Coder
Рет қаралды 265
Don’t ask AI to write your code
7:24
Sajid
Рет қаралды 191 М.
Sections of a Modern Pascal Program
11:05
The Silver Pascal Coder
Рет қаралды 316