Mister_Steed (1) [Avatar] Offline

First I have to say that I really like Groovy and the great book "Groovy in Action".
Second, the following problem isn't a certain one from the book. But I hope you can give me some advice anyway.

I tried to write a Groovy-script that solves sudokus. (I won't post the whole code in order to avoid flooding this thread.)
In the context of this work I wanted to store some lists.
In the subsequent lines of the script I manipulated these lists. And the saved copies of the lists got manipulated too!

Here some lines of my script:

class TemporaryClass {
private temporaryfreeFields = [:]
private temporarySudoku = [:]

def addFieldsList(list, j) {
temporaryfreeFields[j] = list

def addSudokuList(list, j) {
temporarySudoku[j] = list

def temporaryObjects = new TemporaryClass()

def h = 0

def suche(temporaryObjects, freieFields, sudoku, h) {
// Aktuelle Liste der unbesetzten Felder speichern
temporaryObjects.addFieldsList(freieFields, h)

// Umkopieren, sonst wirken Veränderungen zurück auf die Speicherung in der Klasse.
def newFreeFields = []

// Feld aus der Liste der unbesetzten Felder holen
def feld = newFreeFields.pop()

println newFreeFields.size()
println temporaryObjects.temporaryfreeFields[0].size()

// Umkopieren, sonst wirken Veränderungen zurück auf die Speicherung in der Klasse.
def newSudoku = []

// Aktuelles Sudoku speichern
temporaryObjects.addSudokuList(sudoku, h)

println newSudoku.flatten().size()
println temporaryObjects.temporarySudoku[0].flatten().size()

// Schleife über alle freien Zahlen des gewählten Feldes
// Die freien Zahlen des gewählten Feldes in einer Hilfsvariable zwischenspeichern
def fieldList = newSudoku[feld[0]][feld[1]]
for (j in 0..fieldList.size()-1) {
// Feld mit beliebigem freien Wert besetzen
// These changes also appear in the stored list of the class!
newSudoku[feld[0]][feld[1]] = fieldList.pop().toList()

println newSudoku.flatten().size()

suche(temporaryObjects, freeFields, setup, 0)

Most likely I made a terrible mistake. But I am a bit concerned. Thanks for helping me.


Mittie (397) [Avatar] Offline
Re: call by reference?
Hi Elmar,

thanks for your kind words. As you correctly mentioned, this forum is for the book related issues only. You will reach a wider audience for your topic by posting to user@groovy.codehaus.org (english) or http://www.groovy-forum.de (german) - and more Groovies can benefit from the answeres.

A little teaser: http://markmail.org/message/nv67ff6hno6rv3gl?q=shortest+sudoku+solver+groovy&page=1&refer=v2fizeznge56zcdk