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.
LuaWeaver
Party member
Posts: 183
Joined: Wed Mar 02, 2011 11:15 pm
Location: Ohio, USA

For loop if statement errors

Post by LuaWeaver »

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.

Here's a .love to play with.
Attachments
Connect 4.love
Go knock yourself out. :3
(145.26 KiB) Downloaded 113 times
"your actions cause me to infer your ego is the size of three houses" -finley
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 »

Not sure, but in the last place you use i/selected*2 instead of 1/selected*2. Better replace it by 2/selected, that's clearer.
Help us help you: attach a .love.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: For loop if statement errors

Post by bartbes »

Isn't multiplication done before division?
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: For loop if statement errors

Post by ivan »

bartbes wrote:Isn't multiplication done before division?
Multiplication and division have the same precedence.
Therefore 1 / 2 * 3 will be parsed as ( 1 / 2 ) * 3
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 »

Yeah.

Code: Select all

$ lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print(1/2*3)
1.5
Help us help you: attach a .love.
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 »

ivan wrote:
bartbes wrote:Isn't multiplication done before division?
Multiplication and division have the same precedence.
Therefore 1 / 2 * 3 will be parsed as ( 1 / 2 ) * 3
Aaaaah.
Robin wrote:Yeah.

Code: Select all

$ lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> print(1/2*3)
1.5
AAAAAAaaaaaahhhh!!!

The more I think about this the more it bothers me.
Kurosuke needs beta testers
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: For loop if statement errors

Post by Jasoco »

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.
BCQ4v.gif
BCQ4v.gif (1.99 MiB) Viewed 366 times
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: For loop if statement errors

Post by bartbes »

I'm pretty sure the math rules I was taught told me multiplication happens before division though... Then again, I may have done 288 without thinking about it.
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 »

Interestingly, it gets much simpler if you simply calculate / first:

Code: Select all

2 * 3 / 2 = 3
2 * (3 / 2) = 3
2 / 2 * 4 = 4
(2 / 2) * 4 = 4
That is, if you make the rule "/ before *" and "- before +", you get the same results as "/ and * at the same time, from left to right" and "- and + at the same time, from left to right".

I might be horribly mistaken.
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 »

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.
Post Reply

Who is online

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