Hi I'm making a program for a game of Hangman. I have to put a limit to the number of guesses. I have an array to show the current status of the word. So in this array (GuessStatusArray) each cell is either '*', ' ' or a letter guessed correctly eg 'A'.

If someone made a guess of 'O' for the word 'FOOTBALL' then this is how the array would look like. '*OO*****'

The user must have 6 guesses. If they make a correct guess it must not count as one of the six. Therefore after the user guessed 'O' in 'FOOTBALL' they must still have 6 guesses.

Therefore to work out the number of incorrect guesses the user has made I have done IncorrectGuesses := Guesses - CorrectGuesses.
I already have a count for the Guesses else where in the program however I am having trouble working out the correct guesses.

In the code I have done, it goes through each character in the array checking if it is a letter. If it is a letter then the number of Correct Guesses is incremented.

In the example of 'FOOTBALL' above, the code I have done will work the CorrectGuesses to be 2 as the letter O has appeared twice. However the number of correct guesses is actually 1. What do I do to work out the number of correct guesses?

Any help will be appreciated.

(This is only part of the program code that works out the number of incorrect guesses)

Procedure WrongGuesses;
var
  CorrectGuesses : Integer;
  Index : Integer;
  IncorrectGuesses : Integer;


begin
CorrectGuesses := 0;
Index := 1;
repeat
	begin
	If (GuessStatusArray[Index] >= 'A') and (GuessStatusArray[Index] <= 'Z')
		then
			Inc(CorrectGuesses);
	end;
	Inc(Index);
until Index > 20; {The array is from 1 to 20}

IncorrectGuesses := Guesses - CorrectGuesses;


end;

I have an idea :D

Program Solution01;

var mainS:String[20];
        u:Char;
        w:Array[1..20]of Char;
        p:Array[1..20]of Char;
        i:Integer;
      max:Integer;
     corr:Integer;
     inco:Integer;
        h:Set Of Char;


Procedure SorN(s:String);
Begin
   For i:=1 To Length(s) Do Begin
      If (s[i]= ' ') Then p[i]:=' ' else p[i]:='*';
   End;
   For i:=1 To Length(s) Do Begin
      Write(p[i]);
   End;
   WriteLn;
End;



Begin {main}
   mainS:='flaming claw';
   SorN(mainS);
   max:=6;
   corr:=0;
   inco:=0;
   h:=[];

  Repeat
   Write('Try to guess: ');
   ReadLn(u);
   For i:=1 To Length(mainS) Do Begin
      If (u = mainS[i]) Then Begin
        p[i]:= u;
        Write(p[i]);
      End
      Else Begin
         If (mainS[i]= ' ') Then p[i]:=' ' else p[i]:='*';
         Write(p[i]);
      End;
   End;
   For i:=1 To Length(mainS)Do Begin
      h:=h+[mainS[i]];
   End;
   If (u in h) then Inc(corr) {if good then increment the correct answers}
   Else Begin
      Dec(max);{else decrement  the remaining}
      Inc(inco);{and increment the incorrect answers }
   End;

   WriteLn;
   WriteLn('Incorrect: ',inco);
   WriteLn('Correct: ',corr);
   WriteLn('Reamining: ',max);
   Until max<=0;

   ReadLn;
End.{end of main}
{
when the user

if succes:
Try to guess: 
press a key as 'a'
and then enter
Incorrect:0
Correct:1
Remaining:6
**a**** **a*
If is not:
Incorrect:1
Correct:0
Remaining:5
******* ****
Try to guess:
---------
next round
---------
if succes:
Try to guess: 
press a key as 'm'
and then enter
Incorrect:0
Correct:2
Remaining:6
***m*** ****
If is not:
Incorrect:2
Correct:0
Remaining:4
******* ****
Try to guess:
etc....


-=Created by FlamingClaw 2009.05.05=-

}

Or how do you think?

commented: great answer +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.