• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Problem with method updating node I don't want it to

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A bit of a continuation from this topic, but I've narrowed down the cause to something completely different, but I did put together a SSCCE for this one.

https://coderanch.com/t/511451/java/java/contains-method-PriorityQueue-class

Here's my problem. In the code below, I'm trying to generate successors to a starting node that contains a 9 int array that represents a 9 square puzzle board, with the "0" representing the blank space.

The key method is 'generateSuccessor'. I'm trying to generate all the possible moves (at least 2, up to 4) from a given starting position. In any given puzzle, there can only be one blank space. My problem is that for some reason, when I swap the values of the array, it updates the reference array in the PuzzleNode. Which throws off the second swap, and so on. So I end up with an array with more than 1 zero, which is NOT what I want. I can't figure out how to dereference that array so it just updates my temporary array (swapArray). I've been spinning my wheels on this for a handful of hours now, and I need some help. Thanks!

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Couple of things:Shouldn't this be one line of code?

Giant if statement in generateSuccessors: Ew ew ew ew ew.

An SSCCE would really just have the array and swap code: are you working on the original array? It's almost impossible to find the part of the code you're trying to debug.
 
Jim Hester
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem really lies in this snipped of executing code, particularly in:

This is updating 'n' with the new array, when what I really want it to do is to leave 'n' alone, and update swapArray so I can pass that array into a new Node. Make sense? Does posting this snippet of the above code help?

is there a better way of going through the movement options than this giant if statement? I couldn't really think of one, hence this.. I'm still fairly new at this, so any feedback and different ideas are welcome.

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Hester wrote:This is updating 'n' with the new array, when what I really want it to do is to leave 'n' alone, and update swapArray so I can pass that array into a new Node.


Well, I don't know what "n" is, but that aside: I was asking if you're operating on a single array. If you're passing around a reference to a single array, then there's only one array to operate on, and it's the only one that will be modified. If you want to keep the original array, and operate only on a duplicate (without altering the original), you'd need to copy it.

is there a better way of going through the movement options than this giant if statement?


Almost certainly. The trick is to identify what's the same, and what's different, in each block. From a cursory examination it looks like it's just a couple of indices that change based on the position, leading me to believe that the other numbers could be generated using math.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/32840-problem-method-updating-node-i-dont-want.html
 
Jim Hester
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate the responses. I created a clone/copy of the array and that method started working like I wanted it to. Now just to fix the others.

I apologize if I offended anyone by cross-posting. I didn't realize there was a code against that, and just wanted to get perspectives of folks that may visit different forums. I'll be forthright about doing so in the future.
 
Marshal
Posts: 80639
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apology accepted
Please tell the people on the other forum you have posted here, too.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic