And & Or working the wrong way around

Monkey Forums/Monkey Programming/And & Or working the wrong way around

monkeyben(Posted 2013) [#1]
I'm trying to chop an image into a 4x4 grid and display all but the top left rectangle, i.e. not display the rectangle if its coordinates are y = 0 *AND* x = 0. I wrote the code below and it works as I want it to if I use code 2 (Or) but if I use code 1 (And) it doesn't display all the squares in the first row and first column. That's the wrong way around!? Sorry if I have a stupid error somewhere, I'm a bit poorly at the moment.

Code 1: -
For Local y:Int = 0 Until 4
	For Local x:Int = 0 Until 4
		If y <> 0 And <> = 0
			DrawImageRect(image, x * 200, y * 200, x * 200, y * 200, 200, 200)
		EndIf
	Next
Next


Code 2: -
For Local y:Int = 0 Until 4
	For Local x:Int = 0 Until 4
		If y <> 0 Or x <> 0
			DrawImageRect(image, x * 200, y * 200, x * 200, y * 200, 200, 200)
		EndIf
	Next
Next



Redbeer(Posted 2013) [#2]
You're missing an x in the first code?
             If y<> 0 And <> = 0


Should be?

           If y<> 0 And x <> 0



monkeyben(Posted 2013) [#3]
Thanks :) but that is a typo in the code in my post sorry :O} the typo wasn't in the actual code: -

Updated Code 1:
For Local y:Int = 0 Until 4
	For Local x:Int = 0 Until 4
		If y <> 0 And x <> 0
			DrawImageRect(image, x * 200, y * 200, x * 200, y * 200, 200, 200)
		EndIf
	Next
Next

I can't see anything wrong with it :-/

BTW, I'm testing HTML5 in Chrome and on my Android phone.

EDIT: I'm using Monkey version 69


rIKmAN(Posted 2013) [#4]
Replace the "Until" with "To"
For Local y:Int = 0 To 4
	For Local x:Int = 0 To 4



monkeyben(Posted 2013) [#5]
Just to clarify the problem, here is the full code: -

Strict

Import mojo
	
Class Game Extends App

	Field image:Image

    Method OnCreate:Int()
		SetUpdateRate 60
		image = LoadImage("testpicture.jpg")
		Return True
    End

	Method OnRender:Int()
		Cls
		For Local y:Int = 0 Until 4
			For Local x:Int = 0 Until 4
				If x <> 0 And y <> 0
					DrawImageRect(image, x * 200, y * 200, x * 200, y * 200, 200, 200)
				EndIf
			Next
		Next
		
'		For Local y:Int = 0 Until 4
'			For Local x:Int = 0 Until 4
'				If x <> 0 Or y <> 0
'					DrawImageRect(image, x * 200, y * 200, x * 200, y * 200, 200, 200)
'				EndIf
'			Next
'		Next
		Return True
	End

End

Function Main:Int()
    New Game()
	Return True
End


This is the output for the AND loop: -


This is the output for the OR loop: -


Totally the wrong way around...


Redbeer(Posted 2013) [#6]
Since you're using <>, not equal too, with the Or, it means that if either x or y is zero, it won't draw. The internal for loop runs all the way through from 0 to 4, with the external loop equal to 0 the whole time. Then it dumps back out to the external for loop and y becomes 1, then loops through, x = 0, 1, 2, 3 again inside the internal loop.
So whenever EITHER value is zero, because you're using Or, it doesn't draw. This happens on the entire first run through the loop, and the first step of each successive run, where the second loop makes a value zero.

I'm not sure that explanation helps, but sometimes it's good to write out the numbers as the loops run. Something like:

First run y = 0
x = 0, then x = 1, then x = 2, then x = 3

Second run y = 1
x = 0, then x = 1, then x = 2, then x = 3

and so on...

Since in every run through, both loops have at least one of the two values as zero, the image won't draw using Or because it only needs one statement to be true, not both.


Xaron(Posted 2013) [#7]
No it's the correct behaviour.

1st case AND: you paint only when x AND y are NOT zero.

Let's go through the loop
x=0,y=0: condition x<>0 (false) AND y<>0 (false) is false -> nothing to paint
x=0,y=1: condition x<>0 (false) AND y<>0 (true) is false -> nothing to paint
...
x=1,y=0: condition x<>0 (true) AND y<>0 (false) is false -> nothing to paint

So you can see, as long as one of both (x,y) is zero you get false and nothing to paint.


monkeyben(Posted 2013) [#8]
Thanks all :) Thanks Xaron, that makes sense now :D


Redbeer(Posted 2013) [#9]
Yeah, mine is backwards now that I look at it again.
think I had the And and Or pictures reversed in my head...and too little time to read it and type an explanation here at work.
Sorry if I misled you for a second.


monkeyben(Posted 2013) [#10]
No worries Redbeer - I made a mistake in the first place! :)