I recently coded a game of hangman and wanted to know,
A: Are there any places it could be more efficient?
B: Are there any things that are distinctly wrong with it, since I know people seem to say to avoid using GOTO and SLEEP?
C: Is there a way to have it generate words randomly or a database the code could access?
If you don't get any of the variables please let me know, although I have made an effort to match the name to the purpose. (Also if anyone has a better way to print the man, that would be appreciated too).
Since this still hasn't been answered i'm going to try direct responses to only shortening the Gameplay. Many thanks!
Code:
program Hangman;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Classes,
Math;
var
WordPlaceHolder: array [1 .. 10] of string;
BoardStore, BoardPieces: array [1 .. 6] of string;
Wordlist: TStringList;
Check, Lives, Points, Counter, RandomNum: integer;
GuessedWord, SelectedWord: string;
procedure PrintGameBoard;
begin
Writeln(' |//======|');
Writeln(' |/ \| ');
Writeln(' | ', BoardPieces[1]);
Writeln(' | ', BoardPieces[2], BoardPieces[3], BoardPieces[4]);
Writeln(' | ', BoardPieces[5], ' ', BoardPieces[6]);
Writeln(' |');
Writeln(' /|\');
Writeln('/|||\');
for Counter := 1 to length(SelectedWord) do
write(WordPlaceHolder[Counter]);
Writeln('');
end;
Procedure GamePlay;
Begin
Writeln('Please guess a letter');
GuessedWord := 'a';
Writeln(SelectedWord);
repeat
readln(GuessedWord);
GuessedWord := lowercase(GuessedWord);
until ((GuessedWord[1] in ['a' .. 'z']) and (length(GuessedWord) = 1)) or
(lowercase(GuessedWord) = SelectedWord);
if GuessedWord = SelectedWord then
begin
Points := 100;
exit;
end;
for Counter := 1 to length(SelectedWord) do
begin
if SelectedWord[Counter] = GuessedWord[1] then
begin
WordPlaceHolder[Counter] := ' ' + GuessedWord[1];
Points := 1;
Check := Check + 1;
end;
end;
if Points = 0 then
begin
BoardPieces[Lives] := BoardStore[Lives];
Lives := Lives + 1;
end;
Points := 0;
if Check = length(SelectedWord) then
Points := 100;
if Lives = 7 then
Points := 100;
PrintGameBoard;
End;
begin
RANDOMIZE;
Wordlist := TStringList.Create;
Wordlist.CommaText :=
'amazing,supreme,ironic,whatever,guesses,obscene,quintuple,eliminate,random,headway,existing,interest,leaves,seasons,swell,hyper';
Writeln('Hangman');
Writeln('Gameboard');
BoardStore[1] := 'O';
BoardStore[2] := '/';
BoardStore[3] := '( )';
BoardStore[4] := '\';
BoardStore[5] := '/';
BoardStore[6] := '\';
Lives := 1;
PrintGameBoard;
RandomNum := randomrange(0, 16);
SelectedWord := Wordlist[RandomNum];
Writeln('');
for Counter := 1 to 10 do
WordPlaceHolder[Counter] := ' _ ';
for Counter := 1 to length(SelectedWord) do
write(WordPlaceHolder[Counter]);
Writeln('');
repeat
GamePlay;
until Points = 100;
if Lives = 7 then
begin
Writeln('');
Writeln('The word was: ', SelectedWord);
Writeln('You lose, R.I.P.');
end;
if (Points = 100) and (Lives <> 7) then
begin
Writeln('');
Writeln('Well done you win!');
end;
readln;
end.
TArray<string>instead ofarray [1 .. 10] of string;, that will make a lot of things easier and more flexible for changing laterz. Besides that we won't give you answers to add code you didn't implement yet, this is off-topic here. \$\endgroup\$