Sliding Puzzle Design

Hello,

I am currently creating a tradiational sliding puzzle tile game but I am having problems with some of the design.

In order to create a list of random numbers, I have created a List (Collections API) and initialised it as an ArrayList.

Each element in the list in an Integer object counting from 0 to 15.

I then shuffle this using the Collections.shuffle method.

The problem occurs when I select a lower level (i.e from a 5x5 grid to a 4x4 one).

I have to remove elements thus wasting processing time so that I can shuffle the correct number of elements needed for a 4x4 grid.

The arraylist has to be resized every time a different level is selected.

I guess there no way to shuffle a given range of elements in a collection?

What would be a good solution to the design problem that I am having?

Hope someone can help.

--

Andrew

[913 byte] By [ANDREWP7a] at [2007-11-27 0:02:55]
# 1

Create a method that will do it for you.

public int[] getShuffledTileNums(int gridDim)

{

int[] tileNums = new int[(gridDim * gridDim) - 1];

//insert numbers

//shuffle

//return

}

I couldn't think of a very good name for the method, but you get the idea.

CaptainMorgan08a at 2007-7-11 15:56:00 > top of Java-index,Other Topics,Java Game Development...
# 2

> The problem occurs when I select a lower level (i.e

> from a 5x5 grid to a 4x4 one).

> I have to remove elements thus wasting processing

> time

This is no place to worried about wasting processing. Worry about efficiency when you have code that executes 10 million times in a loop. Don't worry about it for code that happens once when the user makes some kind of selection. It takes the user far more time to move his finger to the Enter key or move a mouse than it does to wipe out and refill one ArrayList.

BillKriegera at 2007-7-11 15:56:00 > top of Java-index,Other Topics,Java Game Development...