Hangman game

2

Comments

  • Use code tags when you insert code ......
    This is a Pascal forum not visual basic.

  • SetUpUsedLetterArray2(NextGuessedLetter, LettersGuessedArray2)
    Console.WriteLine("You have used these letters")
    DisplayUsedLetterStatus2(LettersGuessedArray2)
    Console.WriteLine()


    'sets up the SECOND method used letter array and inputs each guessed letter

    Sub SetUpUsedLetterArray2(ByVal NextGuessedLetter As String, ByVal LettersGuessedArray2() As Char)
    Static position As Integer

    LettersGuessedArray2(position) = NextGuessedLetter
    position = position + 1

    End Sub

    'shows the letters used from previous array (SECOND method)

    Sub DisplayUsedLetterStatus2(ByVal LettersGuessedArray2() As Char)
    Dim position As Integer = 1
    Dim UsedLetter As String

    For position = 1 To 26
    UsedLetter = LettersGuessedArray2(position)
    Console.Write(UsedLetter)
    Next
    End Sub











    sorry am new to programming forum, wt do u mean by adding code tags?
  • Firstly, visual basic forum is [link=http://www.programmersheaven.com/mb/VBasic/Board.aspx?S=B20000]here[/link] but i dont think that they have got a topic for this exam.
    Secondly, Code tags are on the tool bar above the text box when you are posting a new message/post, its next to colour.

  • Oh ok sorry about that....
  • hey Thanks for the code mate!!
  • Hi mate .. sorry to disturb you. but the code u gave me didn't work.. it only displays z... when i didn't even enter a z.. and it only shows that single alphabet.. But I want to display every guess the user has entered! CAn anyone help?! I need the code for pascal.. and does anyone know how to limit the number of incorrect guesses?!
  • Can Anyone tell me if this is OK? This is the complete version i added everything i could think of:P. The only thing that annoys me is the fact that it doesnt update wrong guesses at first but id have to make a new procedure for it.[code]Program hangman7;

    { 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;

    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;
    Wholephrase : string;
    Guesses : Integer;
    Badguesses : Integer;
    Letters : String;
    b_guess : Boolean;

    procedure DisplayMenu;
    Begin
    Writeln('__________________________________');
    Writeln;
    Writeln('1. SETTER - Makes new word/phrase');
    Writeln;
    Writeln('2. USER - Next letter guess');
    Writeln;
    Writeln('3. USER - Whole phrase guess');
    Writeln;
    Writeln('4. USER - Set a new random phrase');
    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
    if length(thisnewphrase) > 20 then
    begin
    PhraseOK := False;
    Writeln('Too many letters ... ');
    end else

    begin
    PhraseOK := True;
    GetNewPhrase := uppercase(ThisNewPhrase);
    Letters := 'abcdefghijklmnopqrstuvwxyz';
    Guesses := 0;
    Badguesses := 0;
    end;
    until PhraseOK = True;
    end;

    Function getrandomphrase : string;
    Const
    myphrases = 'myphrases.txt';
    var
    PhraseOK : Boolean;
    ThisNewPhrase : string;
    a_phrases : array[0..99] of string;
    i_phrases : Integer;
    phrases : Textfile;

    begin
    Assign(phrases, myphrases);
    Reset(phrases);
    i_phrases := 0;
    While not EOF(phrases) do
    begin
    Readln(phrases, a_phrases[i_phrases]);
    inc(i_phrases);
    end;
    Closefile(phrases);

    repeat
    Randomize();
    Thisnewphrase := a_phrases[Random(i_phrases)];

    If Length(ThisNewPhrase) < 10 Then
    begin
    PhraseOK := False;
    Writeln('Not enough letters ... ');
    { possible further validation check(s) }
    end Else
    if length(thisnewphrase) > 20 then
    begin
    PhraseOK := False;
    Writeln('Too many letters ... ');
    end else

    begin
    PhraseOK := True;
    getrandomphrase := Uppercase(ThisNewPhrase);
    Letters := 'abcdefghijklmnopqrstuvwxyz';
    Guesses := 0;
    Badguesses := 0;
    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;
    For position := 1 to length(letters) do
    begin
    Lettersguessedarray[position] := '*';
    end;
    end;

    procedure DisplayCurrentStatus(PhraseLength : byte; GuessStatusArray : TGuessStatusArray);
    var
    Position : integer;
    begin
    for Position := 1 to PhraseLength do
    Write(GuessStatusArray[Position]);

    Writeln;
    Writeln;
    Writeln('Letters used so far');
    For position := 1 to length(letters) do
    begin
    Write(Lettersguessedarray[Position]);
    end;
    Writeln;
    Writeln('Number of guesses so far :- ',guesses, ' Number of bad guesses left :- ', badguesses);
    Writeln('Bad guesses remaining :- ', 10-Badguesses)
    end;

    function GetNextLetterGuess : char;
    var
    Position : integer;
    GuessedLetter : char;
    letterok : Boolean;
    begin
    Repeat
    Writeln;
    Write('Next guess ? ');
    Letterok := True;
    Readln(GuessedLetter);
    For Position := 0 to length(letters) do
    begin
    if Guessedletter = LettersguessedArray[Position] then
    begin
    Writeln('You have tried this letter already');
    letterok := False;
    end;

    end;

    GetNextLetterGuess := GuessedLetter;
    inc(Guesses);

    Until letterok = True;
    end;


    function GetWholephrase : string;
    var
    Guessedphrase : string;
    begin
    Writeln;
    Write('Next guess ? ');
    Readln(Guessedphrase);
    Getwholephrase := Uppercase(Guessedphrase);
    inc(guesses);
    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;
    b_guess := False;
    for Index := 1 to Length(NewPhrase) do
    if Uppercase(NextGuessedLetter) = NewPhrase[Index] then
    begin
    GuessStatusArray[Index] := NextGuessedLetter;
    b_guess := True;
    end;
    For Index := 1 to Length(letters) do
    if Nextguessedletter = letters[Index] then
    begin
    Lettersguessedarray[Index] := NextguessedLetter;
    end;

    DisplayCurrentStatus(Length(NewPhrase), GuessStatusArray);
    PhraseGuessed := AllLettersGuessedCorrectly(GuessStatusArray,NewPhrase);
    if b_guess = False then
    begin
    inc(badguesses);
    if badguesses > 10 then
    begin
    writeln('You have ran out of guesses');
    Phrasehasbeenset:= False;
    end;
    end;

    if PhraseGuessed = True then
    Writeln('You have guessed correctly');
    end else
    Writeln('The setter has not specified the word/phrase ..');
    end;

    if Choice = 3 then
    Begin
    if PhraseHasBeenSet = True then
    begin
    DisplayCurrentStatus(Length(NewPhrase), GuessStatusArray);
    Wholephrase := Getwholephrase;

    if Wholephrase = NewPhrase then
    begin
    For Index := 0 to length(Newphrase) do
    begin
    GuessStatusArray[Index] := Newphrase[Index];
    end;

    DisplayCurrentStatus(Length(NewPhrase), GuessStatusArray);
    PhraseGuessed := True;
    Writeln('You have guessed correctly');
    Phrasehasbeenset := False;
    end else
    begin
    inc(badguesses);
    if badguesses > 10 then
    writeln('You have ran out of guesses');
    Phrasehasbeenset:= False;

    end;

    end else
    Writeln('The setter has not specified the word/phrase ..');
    end;

    if Choice = 4 then
    begin
    NewPhrase := Getrandomphrase;
    SetUpGuessStatusArray(NewPhrase, GuessStatusArray);
    PhraseHasBeenSet := True;
    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.[/code]
  • Hi can someone plzz helpme..................I know this is a pascal forum but.....

    Im using VB6 and just wondering how to GUESS the whole word, also how do i limit the number of Guesses that i have ..
  • A few things.

    First off, you missed a section. The game is supposed to allow ONLY letters. Currently I can enter digits, apostrophes, etc.

    When all characters have been guessed, the user is not told. You must enter the phrase in for it to say correct guess! After entering the last letter, it should say complete and no longer let you guess more letters.

    If you accidentally type in a letter at the menu, it creates an error.
    Don't set Choice as an INTEGER, set it as a CHAR and then use CHOICE:=READKEY();
    This way you will not have to press enter after and you won't crash your program.

    If you try to guess the phrase and fail, it un-sets the phrase. You need a BEGIN..END statement:
    [code]
    end else
    begin
    inc(badguesses);
    if badguesses > 10 then
    [b]begin[/b]
    lineDisplay := 'You have ran out of guesses';
    Phrasehasbeenset:= False;
    [b]end;[/b]
    end;
    [/code]

    ***** BELOW IS NOT ABOUT ERRORS, BUT HOW TO MAKE YOUR UI BETTER *****

    Also, the layout is confusing. I just did some research and found Delphi does not support CLRSCR() or GOTOXY(), but use this code at the top of your program:
    [code]
    //-----------------------------------------
    // Position cursor to X, Y
    //-----------------------------------------
    procedure GotoXY(X, Y : Word);
    begin
    Coord.X := X; Coord.Y := Y;
    SetConsoleCursorPosition(ConHandle, Coord);
    end;
    //-----------------------------------------
    // Clear Screen - fill it with spaces
    //-----------------------------------------
    procedure ClrScr;
    begin
    Coord.X := 0; Coord.Y := 0;
    FillConsoleOutputCharacter(ConHandle, ' ', MaxX*MaxY, Coord, NOAW);
    GotoXY(0, 0);
    end;
    [/code]

    and then you can do a little more visual formatting.

    Here's an example that doesn't require many changes and makes the program alot smoother to use.
    First, in your main procedure at the start of your loop, change the first few lines to :
    [code]
    { Main program block starts here }
    begin
    PhraseHasBeenSet := False;

    Repeat
    clrscr;
    DisplayCurrentStatus(Length(NewPhrase), GuessStatusArray);
    DisplayMenu;
    GotoXY(1,23); WriteLn([b]lineDisplay[/b]);
    GotoXY(1,22); Write('Choice? ');
    Readln(Choice);
    lineDisplay := '';
    [/code]
    Add the lineDisplay variable to your global variables as a STRING. Since we clear the screen everytime, we need a way to write the error information to the screen.
    So change the following lines from WriteLn() to:
    [code]
    lineDisplay := 'You have ran out of guesses';
    lineDisplay := 'You have guessed correctly';
    lineDisplay := 'The setter has not specified the word/phrase ..';
    [/code]

    Your display menu needs a start point:
    [code]
    procedure DisplayMenu;
    Begin
    [b] GotoXY(1,10);[/b]
    Writeln('__________________________________');
    Writeln;
    Writeln('1. SETTER - Makes new word/phrase');[/code]

    and so does your DisplayCurrentStatus:
    [code]
    procedure DisplayCurrentStatus(PhraseLength : byte; GuessStatusArray : TGuessStatusArray);
    var
    Position : integer;
    begin
    GotoXY(1,1); WriteLn('Phrase:');
    for Position := 1 to PhraseLength do
    Write(GuessStatusArray[Position]);

    Writeln; Writeln;
    Writeln('Letters used so far:');
    For position := 1 to length(letters) do
    begin
    Write(Lettersguessedarray[Position]);
    end;
    Writeln;WriteLn;
    Writeln('Number of guesses so far :- ',guesses, ' Number of bad guesses :- ', badguesses);
    Writeln('Bad guesses remaining :- ', 10-Badguesses)
    end;
    [/code]

    This will always display your coded phrase at the top of the screen as well as the letters guessed, right & wrong guesses and the number of guesses left. You can now remove all other calls to DisplayCurrentStatus() in the rest of your code.

    The last suggestion I would make is to change the letters already guessed from '*' to something that makes the letters easier to see, such as '.'
    In your SetUpGuessStatusArray() procedure:
    [code]
    For position := 1 to length(letters) do
    begin
    Lettersguessedarray[position] := '.';
    end;
    [/code]


    Well, my wife is calling me to bed now, so I hope this helps you out a bit more ;)
  • Its OK i like constructive criticism :D.
    However you should know that i will have just 55 minutes to write this and that i have only been using pascal for 8 months so its a challenge they probably wont ask me to do half of the that i have just done.
    To fix the menu i did [code] Try
    begin
    Write('Choice? ');
    Readln(Choice);
    end;
    Except
    On EinOutError do writeln('you can only enter numbers from 1 to 5');[/code]
    For accepting only letters ill use a while loop checking if character is a space or ascii character in between 65 and 90. I added Uppercase to [code]repeat
    if Uppercase(GuessStatusArray[Position]) <> NewPhrase[Position] then
    MissingLetter := True
    else[/code]
    So now it tells the user.
  • Hey all , below is my aqa hangman 2009 code in pascal, the program works however I would like it do do 1 more thing: Display letters which have been guessed.
    PLEASE NOTE: If attempting to help me, please do not reply with part codes but with my code with your code included (copy, paste) as I often make stupid mistakes when copying.

    Maybe someone has a better code or can help me. If so please reply and first good responce will get awarded.

    The code is:

    program project1;

    { 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;

    Const
    myphrases = 'MyPhrases.txt';

    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;
    Phraseguesse : string;
    phrases : Textfile;
    a_phrases : array[0..99] of string;
    Letters : String;
    GuessesLeft : Integer;


    procedure DisplayMenu;
    begin
    Writeln('__________________________________');
    Writeln;
    Writeln('1. SETTER - Makes new word/phrase');
    Writeln;
    Writeln('2. USER - Next letter guess');
    Writeln;
    Writeln('3. User - Whole phrase guess');
    Writeln;
    Writeln('4. User - Set a random phras');
    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
    if length(thisnewphrase) > 20 then
    begin
    PhraseOK := False;
    Writeln('Too many letters ... ');
    end else

    begin
    PhraseOK := True;
    GetNewPhrase := Uppercase(ThisNewPhrase);
    Letters := 'abcdefghijklmnopqrstuvwxyz';
    Guessesleft := 10;
    end;
    until PhraseOK = True;
    end;

    function GetRandomPhrase : string;
    var
    PhraseOK : Boolean;
    ThisNewPhrase : string;
    i_phrases : integer;
    begin
    AssignFile(phrases, myphrases);
    Reset(phrases);


    i_phrases := 0;
    While not EOF(phrases)do
    begin
    Readln(phrases, a_phrases[i_phrases]);
    inc(i_phrases);
    end;

    Closefile(phrases);
    Repeat
    Randomize();

    Thisnewphrase := a_phrases[Random(i_phrases-1)];
    if Length(ThisNewPhrase) < 10 then
    begin
    PhraseOK := False;
    Writeln('Random phrase has not enough letters try again ... ');
    { possible further validation check(s) }
    end else
    if length(thisnewphrase) > 20 then
    begin
    PhraseOK := False;
    Writeln('Random phrase has got too many letters ... ');
    end else
    begin
    PhraseOK := True;
    GetrandomPhrase := Uppercase(ThisNewPhrase);
    Letters := 'abcdefghijklmnopqrstuvwxyz';
    Guessesleft := 10;
    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;
    writeln;
    Writeln('Guesses left ', Guessesleft);
    end;

    function GetNextLetterGuess : char;
    var
    Position : integer;
    GuessedLetter : char;
    b_letter : Boolean;

    begin

    Repeat
    Begin
    B_letter := True;
    Writeln;
    Write('Next guess ? ');
    Readln(GuessedLetter);
    For Position := 0 to Length(Letters) do
    if GuessedLetter = Lettersguessedarray[Position] then
    begin
    Writeln('You have already used this letter ');
    Writeln;
    b_letter := False;
    end;
    end;
    Until B_letter = True ;
    GetNextLetterGuess := GuessedLetter;
    GuessesLeft := Guessesleft-1;
    end;

    function GetWholephrase : string;
    var
    Guessedphrase : string;
    begin
    Writeln;
    Write('Next guess ? ');
    Readln(guessedphrase);
    Getwholephrase := guessedphrase;
    Guessesleft := guessesleft-1;
    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 Uppercase(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 = 3 then
    Begin
    if PhraseHasBeenSet = True then
    begin
    DisplayCurrentStatus(Length(NewPhrase), GuessStatusArray);
    Phraseguesse := Getwholephrase;

    if Uppercase(Phraseguesse) = Uppercase(NewPhrase) then
    for index := 1 to Length(newphrase) do

    GuessStatusArray[Index] := Newphrase[Index];

    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 = 4 then
    begin
    NewPhrase := GetRandomphrase;
    SetUpGuessStatusArray(NewPhrase, GuessStatusArray);
    PhraseHasBeenSet := True;
    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.
  • well.. thats of no use mate.. you can try the other posts.. they've got your answer..
    search for the post written by mobix.. he has given the whole code somewhere.. may be in this same page..
  • does anyone know how to write code for the the following: a procedure which goes through the Myphrases.txt file and counts how many phrases there are. e.g. if Myphrases.txt has 25 words/phrases in it the procedure with return the value of 25
  • you are so lazy and so annoying...
  • blimbly heck man! i just ask one question and i am suddenly called lazy?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories