Jump to content
Willicious

for i := X to Y inclusive... how?

Recommended Posts

If you want to specify "i" as being any value between 0 - 7, you can use

for i := 0 to 7 do

And, as I understand it, this will run code as long as "i" matches any value between 0 and 7.

 

But, what if you only want to run the code when "i" matches every value between 0 and 7 at the same time?

 

So, for example, rather than typing:

if (X, Y - 0) and (X, Y - 1) and (X, Y - 2) and (X, Y - 3) etc...

 

Is there a way to express it as (X, Y - i) where i can be any value between 0 and 7, but every value must be accounted for simultaneously?

Share this post


Link to post

So, what is (x, y-0) etc. meant to evaluate to? That's not a Boolean expression nor an integer that could be processed with a bit manipulation.

 

Ok, assuming this somehow is a Boolean anyway, that's the code I'd use:

 

Res := True;

For i := 0 to 7 do begin

  If not (x, y-i) then begin

    Res := False;

    Break;

  End;

End;

if Res then

  ....

Share this post


Link to post

It's hard to understand what exactly you want. How about this:

 

if (X in [0..7]) and (Y in [0..7]) then begin

...

end

 

Edited by zed

Share this post


Link to post
5 hours ago, Willicious said:

But, what if you only want to run the code when "i" matches every value between 0 and 7 at the same time?

That makes no sense, a variable can only have one value at a time. Of course it may be a complex "value", like an array or set, but that does not seem to be what you want.

Share this post


Link to post
6 hours ago, zed said:

 


if (X in [0..7]) and (Y in [0..7]) then

 

 

6 hours ago, Attila Kovacs said:

if (i >=0) and (i <=7) then do_something;

 

The above suggestions will run the code if i is equal to any number between 1 and 7. So, it's more like this:

if (i = 1) or (i = 2) or (i = 3) or (i = 4) etc...

 

What I need is for the code to run only when all numbers are satisfied, like this (note "and" instead of "or"😞

if (i - 1) and (i = 2) and (i = 3) and (i = 4) etc...

 

I realise that, of course, it's not possible for i to equal more than one value at once, that's why I need help with this. Essentially, it's a collision detection check in which the character has to turn around if they reach a vertical wall of at least 7 pixels in height, but  - all 7 pixels have to be filled. The following code works, but I want to try and simplify it if possible:

if HasPixelAt(X, Y) and HasPixelAt(X, Y -1)
  and HasPixelAt(X, Y -2) and HasPixelAt(X, Y -3)
  and HasPixelAt(X, Y -4) and HasPixelAt(X, Y -5)
  and HasPixelAt(X, Y -6) and HasPixelAt(X, Y -7) then
begin
...
end;

 

Thanks in advance, I hope this explains the question a little better.

Share this post


Link to post
4 minutes ago, Willicious said:

 

 

The above suggestions will run the code if i is equal to any number between 1 and 7. So, it's more like this:


if (i = 1) or (i = 2) or (i = 3) or (i = 4) etc...

 

What I need is for the code to run only when all numbers are satisfied, like this (note "and" instead of "or"😞


if (i - 1) and (i = 2) and (i = 3) and (i = 4) etc...

 

I realise that, of course, it's not possible for i to equal more than one value at once, that's why I need help with this. Essentially, it's a collision detection check in which the character has to turn around if they reach a vertical wall of at least 7 pixels in height, but  - all 7 pixels have to be filled. The following code works, but I want to try and simplify it if possible:


if HasPixelAt(X, Y) and HasPixelAt(X, Y -1)
  and HasPixelAt(X, Y -2) and HasPixelAt(X, Y -3)
  and HasPixelAt(X, Y -4) and HasPixelAt(X, Y -5)
  and HasPixelAt(X, Y -6) and HasPixelAt(X, Y -7) then
begin
...
end;

 

Thanks in advance, I hope this explains the question a little better.

I see no other way to do this for vertical walls, since the memory locations of the pixels in question are not contiguous if the storage used has the typical bitmap layout.

 

But you may be approaching the whole problem the wrong way. Instead of working directly with the bitmap image of the rendered scene you should build the scene as a list of objects, each of which represents an element of the scene. In this case the vertical wall would be one such object, storing its location and dimensions internally. You then walk over the list of objects and test whether the point (X,Y) falls inside such an object. This search can be sped up considerably by storing the objects in a tree or list sorted on coordinates. I have never worked in this area myself, but it is a common problem in game design, there should be plenty of literature around on the subject.

  • Like 1

Share this post


Link to post

maybe when you run your code in "quantICUM" cpu you can have it!  8 states in 1... for now, just 2 in 1

Edited by programmerdelphi2k

Share this post


Link to post

@Willicious

using your analogy of one set within another, and not the whole set, seems to me similar to what Delphi uses when adding or removing "an item" in an options property, i.e.:
AOption := AOption + [ItemX] or AOption := AOption - [ItemX]  (or using multiplication, etc...)
However, a "Set" is restricted to 256 elements, and, to circumvent this limit, you would have to use a List or Array to store the values, and later compare whether or not they are contained in the container!

 

other hazards could occur in an unattended task...

implementation

{$R *.dfm}

type
  TMyEnums     = (xx1, xx2, xx3, xx4, xx5, xx6, xx7, xx8, xx9, xx10);
  TMyEnumsSet = set of TMyEnums;

procedure TForm1.Button1Click(Sender: TObject);
var
  a: TMyEnumsSet;
  b: TMyEnumsSet;
  c: TMyEnumsSet;
  d: TMyEnums;
  e: TMyEnums;
begin
{$R-}
  // Intersection of Sets
  d := TMyEnums(11); // out of range, then... {$R-}...{$R+}
  e := TMyEnums(0);
  //
  a := [xx1, xx2, xx3, xx4, xx5, xx6, xx7, xx8, xx9, xx10];
  b := [xx2, xx3, d, xx1, xx8, xx2, e];
  c := a * b; // = xx1, xx2, xx3, xx8
{$R+}
end;

end.

image.thumb.png.c2c531622643af382ee019d26a687725.png

 

maybe this post can help to add /remove new elements in Set

https://stackoverflow.com/questions/33173534/how-do-i-check-or-change-which-set-elements-are-present-using-rtti

Edited by programmerdelphi2k

Share this post


Link to post
1 hour ago, Willicious said:

Hmm, I can't get this to work. What is "Res :=" in this example?

Res is a Boolean variable.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×