Shinobi Life Online

Art Category => Programming => Topic started by: America on September 01, 2016, 14:55:52

Title: Lua Eight Queens Program
Post by: America on September 01, 2016, 14:55:52
(http://i.imgur.com/LXHvMRL.jpg)

Can anyone help me understand this further? I don't get how the function isplaceok works. I know the arguments come from the addqueen function, but I don't understand how they are computed.
Title: Re: Lua Eight Queens Program
Post by: DarthTyrael on September 01, 2016, 15:25:32
As far as I see it (correct me if I'm wrong):

- isplaceok is the argument for placing the queens, checking if the places don't intervene.
- addqueen adds the queen at the spot to a column, then isplaceok checks if the spot is available, from n to N until N+1 queens have been placed.

Update: Edited 2nd part, sorry for that.
Title: Re: Lua Eight Queens Program
Post by: America on September 01, 2016, 16:44:03
As far as I see it (correct me if I'm wrong):

- isplaceok is the argument for placing the queens, checking if the places don't intervene.
- addqueen adds the queen at the spot to a column, then isplaceok checks if the spot is available, from n to N until N+1 queens have been placed.

Update: Edited 2nd part, sorry for that.

Yeah, I understand that, but how does it compute isplaceok? I fail to understand how it references the index i of table a. When it returns false, does it repeat the function with the next cycle of i? Or does it return false, quit the function and do the next cycle of for c = 1, N?
Title: Re: Lua Eight Queens Program
Post by: Manuster on September 01, 2016, 16:46:37
sooo? is this a game? whats the aim?
Title: Re: Lua Eight Queens Program
Post by: DarthTyrael on September 01, 2016, 17:11:49
As far as I see it (correct me if I'm wrong):

- isplaceok is the argument for placing the queens, checking if the places don't intervene.
- addqueen adds the queen at the spot to a column, then isplaceok checks if the spot is available, from n to N until N+1 queens have been placed.

Update: Edited 2nd part, sorry for that.

Yeah, I understand that, but how does it compute isplaceok? I fail to understand how it references the index i of table a. When it returns false, does it repeat the function with the next cycle of i? Or does it return false, quit the function and do the next cycle of for c = 1, N?

Table a is a table that has a list of colums where in the queens are placed. It then checks for each row whether the position is clear (i, n-1)
That's the step.
If a[ i ] == c1 or diagonal etc., the place can be attacked therefore, no queen can be placed.
It returns false.
No queen placed.

Then moves to placing the queen in a different column (c1 =/= c2)
And a new cycle begins.


sooo? is this a game? whats the aim?

@Manuster A riddle/puzzle with queens upon a chess board.
Title: Re: Lua Eight Queens Program
Post by: America on September 01, 2016, 17:26:14
As far as I see it (correct me if I'm wrong):

- isplaceok is the argument for placing the queens, checking if the places don't intervene.
- addqueen adds the queen at the spot to a column, then isplaceok checks if the spot is available, from n to N until N+1 queens have been placed.

Update: Edited 2nd part, sorry for that.

Yeah, I understand that, but how does it compute isplaceok? I fail to understand how it references the index i of table a. When it returns false, does it repeat the function with the next cycle of i? Or does it return false, quit the function and do the next cycle of for c = 1, N?

Table a is a table that has a list of colums where in the queens are placed. It then checks for each row whether the position is clear (i, n-1)
That's the step.
If a[ i ] == c1 or diagonal etc., the place can be attacked therefore, no queen can be placed.
It returns false.
No queen placed.

Then moves to placing the queen in a different column (c1 =/= c2)
And a new cycle begins.


sooo? is this a game? whats the aim?

@Manuster A riddle/puzzle with queens upon a chess board.

So does it repeat the for i, n - 1 with c as 2 and i as 2 or does it go back to the for c = 1, N and repeat the first loop with c as 2 and i as 1?
Title: Re: Lua Eight Queens Program
Post by: DarthTyrael on September 01, 2016, 17:42:49
Sorry... I misunderstood the code as well... Let me try it again. There are no rows in this code (besides printing the board).


You start the command with addqueen({},1} with a = {} and n = 1. n being the queen number and c being the columns.

It will start with c = 1, then look through at the board for arguments (in isplaceok).

If result = false it will  loop the isplaceok again for a different i.
If result = true it will add the column number (c) to the n-th place in table a. Then it will do c+1 and repeat the loop again.

Spoiler: show
Though the addqueen command has an issue though...
It says:
for c= 1, N do
    if isplaceok(a,n,c) then.
       <rest of code>

It needs to say (iirc)

if isplaceok(a,n,c)==true then
      <rest of code>


Hope this helped.


Update: EDITED ONCE MORE FFS I HAVEN'T CODED IN FOREVER!
Title: Re: Lua Eight Queens Program
Post by: America on September 01, 2016, 17:47:05
Sorry... I misunderstood the code as well... Let me try it again. There are no rows in this code (besides printing the board).


You start the command with addqueen({},1} with a = {} and n = 1. n being the queen number and c being the columns.

It will start with c = 1, then look through at the board for arguments (in isplaceok).

If result = false it will go to c = 2 and loop the isplaceok again for a different i.
If result = true it will add the column number (c) to the n-th place in table a.

Spoiler: show
Though the addqueen command has an issue though...
It says:
for c= 1, N do
    if isplaceok(a,n,c) then.
       <rest of code>

It needs to say (iirc)

if isplaceok(a,n,c)==true then
      <rest of code>


Hope this helped.


From what I understand, the first Queen goes at the top in the first row/column because for = 1 , n - 1 is basically for = 1 , 0, which means that it doesn't even run the code. ( it goes from 1 to 0, which doesn't work.)

I don't think that there is a problem with the code, as the guy who wrote the code also created the language.
Title: Re: Lua Eight Queens Program
Post by: DarthTyrael on September 01, 2016, 17:51:41
Aye I see. Anycase, updated my previous post again bc I'm a dumbass coder.


----
You start the command with addqueen({},1} with a = {} and n = 1. n being the queen number and c being the columns.

It will start with c = 1, then look through at the board for arguments (in isplaceok).

If result = false it will  loop the isplaceok again for a different i.
If result = true it will add the column number (c) to the n-th place in table a. Then it will do c+1 and repeat the loop again.

----


That should be the answer to your question.
Title: Re: Lua Eight Queens Program
Post by: America on September 01, 2016, 17:58:38
Aye I see. Anycase, updated my previous post again bc I'm a dumbass coder.


----
You start the command with addqueen({},1} with a = {} and n = 1. n being the queen number and c being the columns.

It will start with c = 1, then look through at the board for arguments (in isplaceok).

If result = false it will  loop the isplaceok again for a different i.
If result = true it will add the column number (c) to the n-th place in table a. Then it will do c+1 and repeat the loop again.

----


That should be the answer to your question.

Ok, that makes sense for returning false, but returning true is where I run into an issue. When it returns true, it says that it runs the whole addqueen function again with the arguments (a, n+1), which would reset the c?
Title: Re: Lua Eight Queens Program
Post by: DarthTyrael on September 01, 2016, 18:05:47
Yeah exactly... Let me correct myself then.

If result = false it will  loop the isplaceok again for a different i.
If all i's are used, it will move to the next column (c+1)

If result = true it will add the column number (c) to the n-th place in table a. then, it will move to placing the next queen (n+1), resetting the column count.

Reason this makes sense: you cannot place the queen in the same column, or they can attack each other.
Title: Re: Lua Eight Queens Program
Post by: America on September 01, 2016, 18:48:15
Yeah exactly... Let me correct myself then.

If result = false it will  loop the isplaceok again for a different i.
If all i's are used, it will move to the next column (c+1)

If result = true it will add the column number (c) to the n-th place in table a. then, it will move to placing the next queen (n+1), resetting the column count.

Reason this makes sense: you cannot place the queen in the same column, or they can attack each other.


I get it now, thanks for your help