Forum > Programming

Lua Eight Queens Program

<< < (2/3) > >>

America:

--- Quote from: DarthTyrael on September 01, 2016, 17:11:49 ---
--- Quote from: America on September 01, 2016, 16:44:03 ---
--- Quote from: 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.

--- End quote ---

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?

--- End quote ---

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.



--- Quote from: Manuster on September 01, 2016, 16:46:37 ---sooo? is this a game? whats the aim?

--- End quote ---

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

--- End quote ---

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?

DarthTyrael:
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: showThough 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!

America:

--- Quote from: 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 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: showThough 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.


--- End quote ---

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.

DarthTyrael:
Aye I see. Anycase, updated my previous post again bc I'm a dumbass coder.


--- Quote from: DarthTyrael on September 01, 2016, 17:42:49 ---
----
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.

----


--- End quote ---

That should be the answer to your question.

America:

--- Quote from: DarthTyrael on September 01, 2016, 17:51:41 ---Aye I see. Anycase, updated my previous post again bc I'm a dumbass coder.


--- Quote from: DarthTyrael on September 01, 2016, 17:42:49 ---
----
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.

----


--- End quote ---

That should be the answer to your question.

--- End quote ---

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?

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version