<!--
/***************************************************************************
 * 
 * Author:  Michael Cafferata (caffem@rpi.edu), Section 3
 *   
 * File:    hw1.html
 * 
 * Purpose: To create a 8-Puzzle game using JavaScript
 *
 * Homework: 1
 * 
 * Date:   Sun Sept 30, 2001
 * 
 ***************************************************************************/

var blank = 8;		//keep track of the blank, initially at position 8
sqlocation = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8);
var myMoves = 0;
var seconds=0;

// start sets things rolling by scheduling
// the function updateTime to run once every second - thanks Dave!
function start() {
  window.setInterval("updateTime()",1000);
}
// updateTime changes the text in the tag with ID time - again, thanks!
function updateTime() {
  seconds++;
  time.innerText = seconds;
}
function Random()
{
	//	loop that simulates clicking 500 times randomly from 0-8
	var i=0;
	while(i<500) {
		swapRnd(Math.round(Math.random()*8));
		i++;
	}
	move.innerText = 0;
	myMoves = 0;
}
function swapRnd(p) {
	//	this function is the same as swap but does not alert for invalid moves
	//	and does not check for solution.
	var temp, temp1;
	var diff = Math.abs(p-blank);
	var sum = Math.abs(p+blank);

	if ((diff==3)||((diff==1)&&((sum!=5)&&(sum!=11))))
	{
		temp = document.images[p].src;						//imaged clicked
		document.images[p].src=document.images[blank].src;  //replace image at piece with blank		
		document.images[blank].src=temp;
		temp1 = sqlocation[p];
		sqlocation[p]=sqlocation[blank];
		sqlocation[blank]=temp1;		
		blank = p	;
	}
}
function swap(piece){
	var temp, temp1;
	var diff = Math.abs(piece-blank);			//determine difference between space clicked and blank
	var sum = Math.abs(piece+blank);			//determine sum of these pieces
	/* 	This code was adapted from www3.cs.cornell.edu/cs130-aux/topsites/final/dwh7/puzzlenewest.html 
		as found on google.  There were many errors in the code.  It was taken because of the
		mathematical statement.																		*/
	/*	We want a move to be performed only if it is legal.  It turns out that this happens
		whenever the difference between piece and blank is 3.  It also happens if the difference
		is one and the sum is not 5 or 11 (which happens when they are each on an edge and
		the difference is one (i.e. one on 2 and one on 3 or one on 5 and one on 6)					*/
	if ((diff==3)||((diff==1)&&((sum!=5)&&(sum!=11)))){
		temp = document.images[piece].src; //imaged clicked
		document.images[piece].src=document.images[blank].src;  //replace image at piece with blank		
		document.images[blank].src=temp;
		temp1 = sqlocation[piece];
		sqlocation[piece]=sqlocation[blank];
		sqlocation[blank]=temp1;		
		blank = piece;
		move.innerText = ++myMoves;
		if (Solution())
			alert("The solution has been found in " + myMoves + " moves in " + seconds + " seconds!\nGood Job!");
	} //end adapted code.
	else alert("Invalid move.\nTry again!");
}
//function determines whether the solution has been found
//very simple... compares two arrays one that has solution 
function Solution(){
	var i=-1;
	while(i++<7)
		if(i!=sqlocation[i]) 
			return false;
	return true;
}
// -->
