For loop if statement errors

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: For loop if statement errors

Post by miko »

LuaWeaver wrote:I am working on Connect 4, and I ran into a strange error. If you change the size of the grid, the size of the pieces change to allow it to still fit snug. I have that down and working. But when I try to position these, I keep running into errors. I came up with a formula to find a scaled size, and now I try to make it so it positions it next to the old one by multiplying by using a for loop value -1. So, it looks like this.

Code: Select all

for i=1, 8 do
for h=1, 8 do
local piece=board[i][h]
if piece~=0 then
love.graphics.drawq(piece, size, 600*(1/selected*2)*(h-1), 600*(1/selected*2)*(i-1), 0, 1/selected*2, i/selected*2, 0, 0)
end
end
end
In theory, this should work. When I tried it, nothing appeared on the screen. This is obviously not a table error, because when I used this code,

Code: Select all

for i=1, 8 do
for h=1, 8 do
local piece=board[i][h]
if piece~=0 then
love.graphics.drawq(piece, size, 0, 0, 0, 1/selected*2, 1/selected*2, 0, 0) --Note to self, add positioning equation
end
end
end
it showed the image. Why won't this work? Oh, and yes, I do comment myself notes to self.
Change "love.graphics.drawq" to "print" and compare the numbers. Is this what you wanted?
I don't know how you want it to look like, but this kind of works:

Code: Select all

local scale=1/selected
love.graphics.drawq(piece, size, scale*300*(h-1), scale*300*(i-1), 0, scale, scale, 0, 0)
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
Wulfie
Prole
Posts: 12
Joined: Mon Apr 11, 2011 6:46 pm

Re: For loop if statement errors

Post by Wulfie »

BlackBulletIV wrote: 1. Parenthesis
2. Exponents
3. Multiplication/division
4. Addition/subtraction

And then you go left to right for everything else.
Same here. Although I did not pass Algebra II :P Geometry and Trig on the other hand I did in my sleep. **shrugs**
"We do not stop playing because we grow old. We grow old because we stop playing." -- Unknown
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: For loop if statement errors

Post by tentus »

BlackBulletIV wrote:
Jasoco wrote:That's why I've made it a habit to put all my calculations in parenthesis ahead of time to avoid any potential misordered calculations.

And yes, I think I just made up a word.

Also, there was just recently a thread at NeoGAF about whether 48 / 2 (9 + 3) equalled 2 or 288. When the civil war was over, 288 won, but it showed how many calculators, apps and people (Spotlight in OS X included) calculated it as 2 by doing the multiplication first (i.e. 2 times 12 into 48 being 24 into 48 being 2 instead of 24 times 12 being 288.). So everyone threw their back packs at the stupid kids.

Image
Ha ha! That's funny. I can't believe how many don't know their order of operations! I was taught this:

1. Parenthesis
2. Exponents
3. Multiplication/division
4. Addition/subtraction

And then you go left to right for everything else.

And, any idea what on earth that kid was doing? Looked like he was smacking his book and half-falling off his chair.
Please Excuse My Dear Aunt Sally, anyone?
Kurosuke needs beta testers
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: For loop if statement errors

Post by Jasoco »

Exactly. That's how I was taught. In that order. P E M D A S.

But, wouldn't that mean 2 is correct? Because it does the Parenthesis first. 9+3=12. Then the multiplication. 2*12=24. Then the division. 48/24=2.

2 is correct if you follow PEMDAS. Isn't it? You only get 288 if you divide 48 by 2 first then multiply that by 12. But 48/2 doesn't have parenthesis, and multiplication comes first. So 2 is correct!

Of course, my school also taught me I before E except after C then stopped there. So I went through life spelling Cashier wrong. Cashier. Cashier. Stupid POS private school.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: For loop if statement errors

Post by Robin »

Jasoco wrote:2 is correct if you follow PEMDAS. Isn't it? You only get 288 if you divide 48 by 2 first then multiply that by 12. But 48/2 doesn't have parenthesis, and multiplication comes first. So 2 is correct!
PEMDAS is still ambiguous.

PEDMSA would be unambiguous.
Help us help you: attach a .love.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: For loop if statement errors

Post by BlackBulletIV »

It was Parachute Expert My Dear Aunt Sally for me. However, it was stated clearly that MD and AS are on the same level. So in English it would sort be like this:

Parachute Expert My-Dear Aunt-Sally
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: For loop if statement errors

Post by vrld »

Jasoco wrote:But, wouldn't that mean 2 is correct? Because it does the Parenthesis first. 9+3=12. Then the multiplication. 2*12=24. Then the division. 48/24=2.
\[ 48 / 2 * (9 + 3) = \frac{48}{2}\cdot (9 + 3) = \frac{48 \cdot (9 + 3)}{2} = 48 \cdot \frac{9 + 3}{2} = 48 \cdot (9 + 3) \cdot \frac{1}{2} \]
It would be 2 if there were paranthesis around the 2 * (9+3).

Think of dividing as multiplying with a fraction: \( 48 \cdot (9 + 3) \cdot \frac{1}{2} = 48 \cdot (9 + 3) \cdot 0.5 \). This is the same as thinking of subtraction as adding a negative number: \( 5 - 3 = -3 + 5 \).
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: For loop if statement errors

Post by ivan »

BlackBulletIV wrote:1. Parenthesis
2. Exponents
3. Multiplication/division
4. Addition/subtraction
There's a few extra operators in Lua:
1. Parenthesis
2. Unary negation (-)
3. Exponents (^)
4. Multiplication/division/modulo (*, /, %)
5. Addition/subtraction (+, -)
6. Comparison (<, <=, >=, >)
7. Equality (==, ~=)
8. Assignment (=)
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: For loop if statement errors

Post by BlackBulletIV »

Indeed there are (and there's actually more), although we're talking about maths in general here, not just Lua.
ivan wrote:8. Assignment (=)
Technically = isn't an operator in Lua, and it's actually not in the table of precedence in Programming in Lua. I found this out when I tried to do multiple assignment:

Code: Select all

a = b = 0
Nevertheless, it does come last.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: For loop if statement errors

Post by ivan »

BlackBulletIV wrote:Technically = isn't an operator in Lua, and it's actually not in the table of precedence in Programming in Lua. I found this out when I tried to do multiple assignment:

Code: Select all

a = b = 0
Nevertheless, it does come last.
Yes, you're right. Assignment in Lua doesn't work like in C/C++ (in a sense that an assignment operation doesn't return a value).
By the way, you can 'sort of' do multiple assignments on the same line using:

Code: Select all

a, b = 0, 0
Also, you can also swap values by:

Code: Select all

a, b = b, a
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 198 guests