If statement is wrong!!

this is my code.


  for i := 0 to 15 do
  begin
    for j := 0 to 2 do
    begin
      if ((Computer[i].Rows[j].Pos.x = iOld) and (Computer[i].Rows[j].Pos.y = jOld)) then
      begin
        // main part
      end;
    end;
  end;


For some reason delphi *sometimes* executes the main part even though the if statement is false! I'm using similar code in other places and there it works, what am I doing wrong here??

Any help is really appreciated...


Fran
WizardsHatAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
geobulConnect With a Mentor Commented:
Hi,

Let j = 0
then k = j = 0
Inc(k) : k=1
Dec(k) : k=0
Dec(k) : k=-1
m := Computer[i].Rows[k].ORowField.x;
// error Rows[-1] out of bounds !!!

Regards, Geo
0
 
billiousCommented:
If you'd show us what the code is in //main part, then we just might be able to help you - don't keep it under your hat, regardless of what you're doing in it!

...Bill
0
 
jumail11Commented:
The codes you given us, doesn't seems any logical error. I expect the problem was caused by outside codes. You'd show us the rest of the code.

-jumail-
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
steve_hskCommented:
Hi Fran ...

These guys are right ... need to see more of your code.

The only suggestion I have is to break the statement down, to enable bug tracking, breakpoints, and variable watches to mean more :-

if (Computer[i].Rows[j].Pos.x = iOld) then
begin
     if (Computer[i].Rows[j].Pos.y = jOld) then
     begin
       // main part
     end;
end;

This isn't a permanant solution, will make no difference, but may help in debugging.

Steve
0
 
sfockCommented:
have you changed the if line? probably delphi has not recomplied it if it is not part of the dpr file or par of a package ....

0
 
JamesLondonCommented:
Hi there,

Sometimes strange things can happen if for example your are doing something wrong elsewhere, i.e. outside that code you should check all accessing of objects, i.e. you are not freeing something then accessing it later. In some cases Access Violations can be suppressed thus causing the strange behaviour.

Also check that you are not exceeding the bounds of your arrays by making sure the "Range Checking" option is turned on.

Best Regards,
- James
0
 
JamesLondonCommented:
Hi again,

The reason for my last statement is experience!

- I once had a problem with the MOD operator and I was convinced that it was Delphi's fault though the perculier behaviour was caused by something completely different.

James
0
 
WizardsHatAuthor Commented:
Okay this is the full code...
Its for a game called "Muehle" in German but I don't know the English name. I test if the position I want to place the stone is near the position it currently has, because the game only allows to move the stone to adjacent fields. The rows are all three places long and each place exists in two rows. Well I guess you can't really understand what I'm talking about unless you know the game, but maybe you find some technical error...

Steve I tried this as well, usually the first if is wrong (so it works out like 3 = 5 for example) but delphi seems to think its correct.

James I have RangeChecking enabled, and I'll look now for possible access violations.


function TFoMuehle.IsInVicinity(iOld, jOld, iNew, jNew : integer): boolean;
var i, j, k, m : integer;
begin
  for i := 0 to 15 do
  begin
    for j := 0 to 2 do
    begin
     if ((Computer[i].Rows[j].Pos.x = iOld) and (Computer[i].Rows[j].Pos.y = jOld)) then
      begin
        k := j;
        Inc(k);        // if it is the next in the row
        if k < 3 then
        begin
          if ((Computer[i].Rows[k].Pos.x = iNew) and (Computer[i].Rows[k].Pos.Y = jNew)) then
          begin
            Result := true;
            exit;
          end;
        end;
        Dec(k);       // if it is the previous in the row
        Dec(k);
        if k > -1 then
        begin
          if ((Computer[i].Rows[k].Pos.x = iNew) and (Computer[i].Rows[k].Pos.Y = jNew)) then
          begin
            Result := true;
            exit;
          end;
        end;
       //maybe its in the other row
        m := Computer[i].Rows[k].ORowField.x;
        k := Computer[i].Rows[k].ORowField.y;
        Inc(k);        // if it is the next in the row

        if k < 3 then
        begin
          if ((Computer[m].Rows[k].Pos.x = iNew) and (Computer[m].Rows[k].Pos.Y = jNew)) then
          begin
            Result := true;
            exit;
          end;
        end;
        Dec(k);       // if it is the previous in the row
        Dec(k);
        if k > -1 then
        begin
          if ((Computer[m].Rows[k].Pos.x = iNew) and (Computer[m].Rows[k].Pos.Y = jNew)) then
          begin
            Result := true;
            exit;
          end;
        end;

      end;
    end;
  end;
  Result := false;
end;
0
 
WizardsHatAuthor Commented:
The game is called "Nine men's morris" in English.
0
 
geobulCommented:
Hi,

If your i and j values are OK then the problem is caused by:

1. iOld and/or jOld values:
- They must have correct initial values before the loops.
- Are you changing their values inside the loop? If yes, how?

2. Computer[i].Rows[j].Pos.x and/or Computer[i].Rows[j].Pos.y values:
- check their values inside the loop

Add something like:

ShowMessage('i='+IntToStr(i)+' j='+IntToStr(j) + #13#10 +
'PosX='+IntToStr(Computer[i].Rows[j].Pos.x) + ' iOld='+ IntToStr(iOld) + #13+#10 +
'PosY='+IntToStr(Computer[i].Rows[j].Pos.y) + ' jOld='+ IntToStr(jOld));

inside the second loop and see what' happening.

Regards, Geo
0
 
WizardsHatAuthor Commented:
Okay now I only get into the loop when I should,  but then I get a rangecheck exception here

m := Computer[i].Rows[k].ORowField.x;



because suddenly k is some ridiculously large number, but i can't see why it should be?
0
 
WizardsHatAuthor Commented:
Weeeeeeell... I now removed the part in the if- statement starting from

m := Computer[i].Rows[k].ORowField.x;


to the end of it since I realized that it isn't necessary ( I go through all rows anyway), and now everything seems to work fine.

But I'll still be grateful to anyone who could explain why!!
0
 
CleanupPingCommented:
WizardsHat:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
geobulCommented:
Comment = I do care :-)
0
All Courses

From novice to tech pro — start learning today.