

We assume that the world is set up so that the bottom left patch is (0,0). Subsequent button-presses iterate through the solution list.

For 8 queens, generating all the solutions takes about 0.1 seconds, and this is done only on the first button-press. The Queens button executes the following code. We also need a global variable to store a list of solutions: to-report all-queen-solutions If (safe ? i) Īt the starting level, we pass in the same number (usually 8) in both arguments.

[ let lst (queen-solutions (n - 1) num-rows ) All solutions that pass the safety check are accumulated in res, which is the value returned: to-report queen-solutions Otherwise, we recursively place n - 1 queens (giving lst), and for each solution in that list try all possible rows in the next column (using a while loop). If n = 0, there is one way of placing zero queens (the sole solution is represented by the empty list). It returns a list of all possible solutions: a list of lists. The main reporter takes two arguments: the number of queens to place ( n) and the number of rows (usually 8). The combination is safe if we find no threats: to-report safe Simultaneously, we use i to iterate through the column positions in the existing solution. The command block inside this loop refers to the current list element as ?. We then use a foreach loop to iterate through the existing solution. We can test a new row position n for compatibility with an existing partial solution lst by calculating the column the new queen will be in: ((length lst) + 1). Report (j = n) or (i + j = m + n) or (i - j = m - n)

To test the (column,row) pairs ( i, j) and ( m, n) for mutual threats, we check for queens in the same row or diagonal (our placement strategy will guarantee exactly one queen per column): to-report add-queen We can add a new queen to such a list with the lput operator ( fput and lput are equally efficient in NetLogo 5.0, so there is no longer any need to prefer fput). The picture above shows the solution, for example. Following a relatively standard approach, we will place exactly one queen in each column, representing a solution as a list of row positions, indexed from 1. so that none are in the same row, column, or diagonal. The Eight Queens puzzle requires placing eight queens on a chessboard so that none threaten each other – i.e. NetLogo also includes a reduce operator, which allows non-parallel versions of the MapReduce style of programming. In this tutorial, I will highlight the power of the list operators foreach, map, and filter, through two classic list-processing case studies: the Eight Queens puzzle (illustrated above) and the Sieve of Eratosthenes. In fact, NetLogo incorporates much of the power of Lisp. Nothing could be further from the truth – such comments are like suggesting that The Lord of the Rings is unsuitable for adults simply because children enjoy it.
#Netlogo lists how to
However, this ease of use has attracted scorn from some quarters, with one blogger even suggesting that “ using NetLogo signals that you don’t know how to do real programming.” A colleague of mine once expressed a similar sentiment. It is so easy to use that children can program in it, and very sophisticated functionality can be delivered with surprisingly little code. NetLogo is one of my favourite simulation tools, as I have said in a previous tutorial.
