Queue/Linked List in Java not setting properly -
i creating program complete "one tough puzzle" game using depth first search (irrelevant). have algorithm pretty complete. however, when poll() off queue, middle pieces same (explained below)
i using 1d array length of 9 type piece state class hold current board, available pieces, parent node
board gets set by:
board = new piece[9]; for(int = 0; < 9; i++) board[i] = new piece();
this code adds each piece middle of board, , adds queue (puzzle)
for(int = 0; < 9; i++) { piece[] b = board; b[4] = pieces[i]; piece[] p = removeelement(pieces, pieces[i]); // remove array puzzle.add((new state(b, p, null))); }
just simple test me show myself wasn't going crazy, used code
while((s = puzzle.poll()) != null) { string ss = getavailable(s.returnpieces()); system.out.println("middle:"+s.returnboard()[4].getpieceid() + " :::: "+ ss); }
this returned
middle:9 :::: 2 3 4 5 6 7 8 9 middle:9 :::: 1 3 4 5 6 7 8 9 middle:9 :::: 1 2 4 5 6 7 8 9 middle:9 :::: 1 2 3 5 6 7 8 9 middle:9 :::: 1 2 3 4 6 7 8 9 middle:9 :::: 1 2 3 4 5 7 8 9 middle:9 :::: 1 2 3 4 5 6 8 9 middle:9 :::: 1 2 3 4 5 6 7 9 middle:9 :::: 1 2 3 4 5 6 7 8
as can see, b[4] 9, pieceid's correct. why on write every board object? no matter set last piece[#] to, uses pieceid every 1 of them.
pieces[7] = (new piece(diamond_in, heart_out, club_out, club_in, 8)); pieces[8] = (new piece(diamond_out, spade_out, diamond_in, heart_in, 9));
Comments
Post a Comment