Recent

Author Topic: Find repeated elements in an array.  (Read 3459 times)

DarkDiosito

  • Newbie
  • Posts: 3
Find repeated elements in an array.
« on: July 20, 2018, 02:08:17 am »
Hello!

I'm making a hangman game in Pascal for a study project and I'm having trouble with this part.

Let's say the word is "mom", if the player wants to guess the letter "m" the program should return that the letter "m" is in the 1st and 3rd position.

The code used to find elements in the array is the following:

Code: Pascal  [Select][+][-]
  1.                 repeat
  2.                     c := c+1
  3.                 until (word[c] = letter) or (c >= wordlength);
  4.  
  5.                 position := c;
  6.  
  7.                 if (word[c] = letter)
  8.                         then                                            
  9.                             begin                                        
  10.                                  repeat                                  
  11.                                        z:=z+1
  12.                                  until (correct[z] = letter) or (z >= wordlength);
  13.  
  14.                                  if correct[z] = letter
  15.                                     then
  16.                                         begin                                            
  17.                                              writeln('This letter has already been guessed correctly')
  18.                                         end
  19.                                     else
  20.                                         begin
  21.                                              correctAmout := correctAmount+1;
  22.                                              correct[position] := letter;
  23.                                              writeln('CORRECT!');
  24.                                              WriteLn;
  25.                                              for y := 1 to wordlenght do
  26.                                                  Write(correct[y]);
  27.                                         end;
  28.                             end
  29.                          ...
  30.                      

Being "word" the array that contains the original word and "correct" the array that contains the already correctly guessed letters.

Obviously this code is wrong because it jumps out of the searching process as soon as it founds a match, which is not good.

I tried a few things and couldn't get it working and have absolutely no idea how to make this right.

If anyone has an idea, I would be very welcome!

EDIT: If you need I can upload the source code file, but it's in spanish. It shouldn't be much problem since it's just the variables that have names in other language, but hey, it might be a little bit harder to understand than one in English
« Last Edit: July 20, 2018, 04:12:19 am by DarkDiosito »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Find repeated elements in an array.
« Reply #1 on: July 20, 2018, 04:25:39 am »
I am not sure what problem you have:
Are you asking how to loop through the letters of a word like 'mom'?
Are you in doubt how to compare two letters?
Are you having problem finding a place to save the locations where a letter like 'm' is located in a word like 'mom'?

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Find repeated elements in an array.
« Reply #2 on: July 20, 2018, 11:12:48 am »
So you want to know if a given character is in the string to be guessed, and if so in which positions?
Can the "characters" also be diacritics (like ä, ë €)?
If so, then you need to know wether your string is encoded as single byte (old Windows/DOS codepages) or possibly as UTF8.

Suppose it is single byte encoded for now.
Untested code!!

Code: Pascal  [Select][+][-]
  1. function Contains(Ch: Char; const S: String; out Arr: TDynIntArray): Boolean;
  2. var
  3.   i: Integer;
  4. begin
  5.   Arr := nil;
  6.   Result := False;
  7.   for i := 1 to length(S) do
  8.   begin
  9.     if Ch = S[i] then
  10.     begin
  11.       Result := True;
  12.       SetLength(Arr, Length(Arr)+1);
  13.       Arr[High(Arr)] := i;
  14.     end;
  15.   end;
  16. end;

The function retruns True if Ch can be found in S.
If the result = True then Arr contaisn the positions at which Ch can be found in S.

@Everybody else: this can of course be optimized for speed, but since user interaction will be orders of magnitude slower then this code, I did not bother.

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: Find repeated elements in an array.
« Reply #3 on: July 20, 2018, 03:14:59 pm »
If it is Ansi, this also works:
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  2. uses
  3.   sysutils;  
  4. var
  5.   s:string ='this string contains mom several moments and maybe more moms are to be expected, mom';
  6.   c:char = 'm';
  7.   i:integer = -1;
  8. begin  
  9.   repeat
  10.     inc(i);
  11.     i:=s.IndexOf(c,i);
  12.     writeln(i);
  13.   until i = -1;      
  14. end.
Specialize a type, not a var.

JimD

  • Jr. Member
  • **
  • Posts: 62
Re: Find repeated elements in an array.
« Reply #4 on: July 20, 2018, 03:57:31 pm »
Well, this was fun.
Attached is a rather trivial and incomplete example to get you started.
Hope this helps.

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: Find repeated elements in an array.
« Reply #5 on: July 20, 2018, 05:00:54 pm »
another Example attached.
800+ words and ascii graphics.
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

 

TinyPortal © 2005-2018