Design
---


I'd create a separate class for `Grid` and `Position` at the very least. Especially when separating out the `Grid` the game code will be significantly reduced. Smaller classes should generally be preferred when they make sense.

With the removal of `movecount` (see below), you'd have just two fields: `currentGrid` and `goalGrid`, both of type `Grid`.

Code review
---

I'll handle the lines in order, skipping many, with the remarks below the code.


    import java.util.Arrays;
    public class FifteenPuzzle
    { 

Please use at least a white line between import statements and the class declaration.

The class should probably be made `final` unless you create it for extension.

Using `{` on a new line instead of "Egyptian braces" is often acceptable, but it is not very much used for Java. If they are used, then use them **consistently** (which is not the case here). Most IDE's contain code formatting nowadays, use that if you don't like doing it yourself.

    private int xspace;   // xspace,yspace are the current coordinates of the space
    private int yspace;

What space? The empty space? Please be precise. If two variables are needed for one thing, try and aggregate them together, e.g. into a `Position` class (immutable with `equals` and `hashCode` please).

    private final int size; // the number of tiles across and down 

Not required, `grid.length` will do that. Generally, try to minimize the amount of fields. However, for readability reasons I guess this is kinda OK.

    private int movecount = 1000;

The field `movecount` (or rather `moveCount`) should be a constant; it doesn't change within the game logic.

I'm a bit worried about the naming though, what about, say, `edgeSize` or `dimension`?

    grid = initialGrid;

Why isn't the size of the grid checked here? It is not symmetric with the constructor that just takes the grid size. Furthermore, there is no check that the width and height are identical.

    public void scramble() {

Hey, is the user allowed to call `scramble`? If not, then why is the method `public` instead of `private`? Same for other methods.

    done = true;

Use `while (true)` or `for(;;)` and `break` / `continue` instead. May look weird initially, but it removes one variable from the equation, and you can remove redundant checks.

    for (int a = 1; a <= size; a++) {

No, no, no. Always use zero based indexing:

    for (int a = 0; a < size; a++) {

unless there are specific reasons not to. It seems OK for the move counter (as you're doing move 1 initially, not move 0 I suppose).

And what about that naming, why `a` and `b` for coordinates? `x` and `y` is used correctly in the rest of the code.

    public int[][] setGoal() {

A "setter" is a specific function that sets a field to a given value. It takes a single argument and returns `void`. `createGoal` is probably a better name.

    public int[][] getGrid()

This is a correct "getter" declaration. No arguments, single return of a field.

    return grid;

Never ever return a mutable reference to something that makes up the internal state of a class, violating encapsulation principles. Copy or clone the data instead, or provide read only access (iterator, lambda).

    if (x < 0 || y < 0 || x > size-1 || y > size-1) {

This I can read somewhat.

    return ( (x == xspace && (y == yspace - 1 || y == yspace + 1)) || (y == yspace && (x == xspace - 1 || x == xspace + 1)) );

This I cannot. The line size is too large as well. Compare to reading this:

    return insideGrid(x, y) && nextToEmpty(x, y);

and then implementing those functions. With a bit of help of the IDE that's not even that much work.

    if (!legalClick(x, y)) {
        return;
    }

Just returning (or worse, just returning `null` or `0` for methods that return data) is almost always a bad decision. In this case an exception should be thrown.

If the calling code first wants to check if the move is valid, it can call `legalClick` itself. You may be calling `legalClick` twice that way, but the JIT compiler will probably notice if it starts to take too much time.