/*
 * STANDARD DISCLAIMER:
 *  You may freely copy, distribute and reuse the code in this program.
 *  Trirex disclaims any warranty of any kind, expressed or implied, as to
 *  its fitness for any particular use.
 */

/* Generated by Interface Builder */

#import "Puzzle.h"
#import <appkit/Matrix.h>
#import <libc.h>

@implementation Puzzle

- init
{
	[super init];

	blankPieceRow = blankPieceCol = 3;
	return self;
}

- appDidInit:sender
{
	[window makeKeyAndOrderFront: NULL];
	return self;
}

- movePiece:sender
{
	id piece;
	int dx, dy;
 
	int oldRow = [sender selectedRow];
	int oldCol = [sender selectedCol];

	/* Make sure that it's a legal move */
	dx = abs(oldRow - blankPieceRow);
	dy = abs(oldCol - blankPieceCol);

	if (dx > 1 || dy > 1) return self;
	if (dx == 1 && dy ==1) return self;

	/* Get the id of the button on which the user clicked */
	piece = [sender selectedCell];

	/* Swap the blank piece with the piece on which the user clicked */
	[matrix putCell:piece at:blankPieceRow :blankPieceCol];
	[matrix putCell:blank at:oldRow :oldCol];

	[matrix display];

	// Remember where the blank piece is located
	blankPieceRow = oldRow;
	blankPieceCol = oldCol;
	return self;
}


@end
