Bryan
Bryan

Reputation: 61

Delphi Timer Issue

procedure TForm1.Timer2Timer(Sender: TObject);
var  ff : integer;
begin

  for ff := 0 to 32 do
    If CardArray[NewValue] + CardArray[NewValue3] = PlayArray[ff] then
    begin
       sndPlaySound('cashregister.wav', snd_Async or snd_NoDefault);
    end
    else
    begin
       SendClick(451,541);
    end;

end;

Can anyone spot a reason to why this would be working incorrectly? Its triggering SendClick() when it shouldn't be... is there a reason why the if and else is not working ?

 for i := 0 to 32 do
   If CardArray[NewValue] + CardArray[NewValue3] = PlayArray[i] then
     sndPlaySound('cashregister.wav', snd_Async or snd_NoDefault);
   end;

Is in a completely different timer... and plays the sound when it should... so the condition is returning valid when it should be..I'm almost positive it has to be something with the ELSE because i can do this....

  if f1 = 0 then
    SendClick(451,541);
  for ff := 0 to 32 do
    If CardArray[NewValue] + CardArray[NewValue3] = PlayArray[ff] then 
    begin
      sndPlaySound('cashregister.wav', snd_Async or snd_NoDefault);
      f1 := 1;
    end;

and it will stop clicking after the condiition has returned true one time but wont start back up again...

so i know CardArray[NewValue] + CardArray[NewValue3] = PlayArray[ff] is working perfectly fine ...I have not had sleep and I'm sorry if this is confusing to you guys but I don't know why you would need any further code then the first snippet ive posted to tell me whats wrong...

Now if you guys would like the ARRAYS here...

  CardArray[2] := '2';
  CardArray[3] := '3';
  CardArray[4] := '4';
  CardArray[5] := '5';
  CardArray[6] := '6';
  CardArray[7] := '7';
  CardArray[8] := '8';
  CardArray[9] := '9';
  CardArray[10] := '10';
  CardArray[11] := 'J';
  CardArray[12] := 'Q';
  CardArray[13] := 'K';
  CardArray[14] := 'A';
  SuiteArray[99] := 'c';
  SuiteArray[100] := 'd';
  SuiteArray[104] := 'h';
  SuiteArray[115] := 's';
  PlayArray[0] := '22';
  PlayArray[1] := '33';
  PlayArray[2] := '44';
  PlayArray[3] := '55';
  PlayArray[4] := '66';
  PlayArray[5] := '77';
  PlayArray[6] := '88';
  PlayArray[7] := '99';
  PlayArray[8] := '1010';
  PlayArray[9] := 'JJ';
  PlayArray[10] := 'QQ';
  PlayArray[11] := 'KK';
  PlayArray[12] := 'AA';
  PlayArray[13] := 'AK';
  PlayArray[14] := 'AQ';
  PlayArray[15] := 'AJ';
  PlayArray[16] := 'A10';
  PlayArray[17] := 'KA';
  PlayArray[18] := 'QA';
  PlayArray[19] := 'JA';
  PlayArray[20] := '10A';
  PlayArray[21] := 'KQ';
  PlayArray[22] := 'KJ';
  PlayArray[23] := 'K10';
  PlayArray[24] := 'QK';
  PlayArray[25] := 'QJ';
  PlayArray[26] := 'Q10';
  PlayArray[27] := 'JK';
  PlayArray[28] := 'JQ';
  PlayArray[29] := 'J10';
  PlayArray[30] := '10K';
  PlayArray[31] := '10Q';
  PlayArray[32] := '10J';

so for example lets say do this...

NewValue := 2;
NewValue3 := 2;

CardArray[NewValue] + CardArray[NewValue3] would = 22

now lets check the play array for 22

with a for loop

for ff := 0 to 32 do
 if PlayArray[ff] = CardArray[NewValue] +  CardArray[NewValue3] then
begin
dowhatever...
end;

.... is this a valid explanation of whats going on now do you guys understand what the for loop is doing?

Upvotes: 0

Views: 885

Answers (5)

Gregor Brandt
Gregor Brandt

Reputation: 7799

Simply put, the line:

If CardArray[NewValue] + CardArray[NewValue3] = PlayArray[ff] then

Is not evaluating to true when you expect it to. It is evaluating to false, so the sendclick is executed.

Set a breakpoint on the 'If' in the debugger, look at the values you are adding, are they what you expect?

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613491

I think you want to do something like this:

Play := False;
Cards := CardArray[NewValue] + CardArray[NewValue3];
for ff := 0 to 32 do begin
  if Cards=PlayArray[ff] then begin
    Play := True;
    break;
  end;
end;
if Play then begin
  ContinuePlaying;//I've got great cards
end else begin
  Fold;//I've got a hand full of bus tickets and have to fold
end;

You want to play your cash register sound if any single one of the 33 options matches, otherwise you click the fold button.

I've moved the CardArray[NewValue] + CardArray[NewValue3] code out of the loop because it evaluates the same every time round and that was just confusing the heck out of us all!

Does this to it?

Upvotes: 2

Arnaud Bouchez
Arnaud Bouchez

Reputation: 43053

The sndPlaySound is taking some time to return.

So you should disable the timer during the loop:

procedure TForm1.Timer2Timer(Sender: TObject);
var
ff : integer;
 begin
   Timer2.Enabled := false;
for ff := 0 to 32 do
If CardArray[NewValue] + CardArray[NewValue3] = PlayArray[ff] then
   begin
   sndPlaySound('cashregister.wav', snd_Async or snd_NoDefault);
   end
else
   begin
   SendClick(451,541);
   end;
   Timer2.Enabled := true;
end;

And... what is strange with your code that you're looping into 33 values, with SendClick or sndPlaySound for each value... I guess your code will never work as such...

Edit: here is a modified version - perhaps it's what you want to have:

procedure TForm1.Timer2Timer(Sender: TObject);
var
ff : integer;
 begin
   Timer2.Enabled := false;
   try
     for ff := 0 to 32 do
     If CardArray[NewValue] + CardArray[NewValue3] = PlayArray[ff] then
     begin
       sndPlaySound('cashregister.wav', snd_Async or snd_NoDefault);
       exit; // value found
     end;
     // not found
     SendClick(451,541);
   finally  
     Timer2.Enabled := true;
   end;
  end;

Upvotes: 2

Uwe Raabe
Uwe Raabe

Reputation: 47819

You are probably not aware of the fact that the if clause is executed for all values ff from 0 to 32. So you have 33 checks and 33 times either the sound or the click.

My psychic power tells me that you are looking for the sum of the both CardArray values being equal to one value inside PlayArray and then playing a sound or making a click when no match is found. But that is not what you programmed.

Upvotes: 1

Larry Lustig
Larry Lustig

Reputation: 50998

NewValue or NewValue3 does not contain what you think it contains (and no one can guess what that is because there's no indication of where the values come from).

Upvotes: 1

Related Questions