// JScript source code

var grx = new Array(0,0,0,0,0,0,0,0)    //x location of rocks
var gry = new Array(0,0,0,0,0,0,0,0)   //y location of rocks
var gEnd = false;  //will end step function when set to true

var row0 = new Array(0,0,0,0,0,0,0,0,0,0);
var row1 = new Array(0,0,0,0,0,0,0,0,0,0);
var row2 = new Array(0,0,0,0,0,0,0,0,0,0);
var row3 = new Array(0,0,0,0,0,0,0,0,0,0);
var row4 = new Array(0,0,0,0,0,0,0,0,0,0);
var row5 = new Array(0,0,0,0,0,0,0,0,0,0);
var row6 = new Array(0,0,0,0,0,0,0,0,0,0);
var row7 = new Array(0,0,0,0,0,0,0,0,0,0);
var row8 = new Array(0,0,0,0,0,0,0,0,0,0);
var row9 = new Array(0,0,0,0,0,0,0,0,0,0);
var gt = 0; //used in pause function
//var gx = 1;   //x position
//var gy = 1;   //y position
//var gd = 2;  //direction

var grid = new Array(row0,row1, row2, row3,row4,row5,row6,row7,row8,row9);
var icounter = 0;
var imgs = new Array(new Image(), new Image(), new Image(), new Image(), new Image(), new Image(), new Image())
imgs[0].src = "ball.bmp"
imgs[5].src = "nothing.bmp"
imgs[1].src = "north.jpg"
imgs[2].src = "south.jpg"
imgs[3].src = "east.jpg"
imgs[4].src = "west.jpg"
imgs[6].src = "rock.jpg"
//***************************
function initializeRocks() {
var i,j
var unique = false
for (i = 0; i<=numRocks - 1; i++) {
	unique = false
	while(unique == false) {
		unique = true
		grx[i] = 1 + Math.floor(9*Math.random())
		gry[i] = 1 + Math.floor(9*Math.random())
		for (j = 0; j <= i-1; j++) {
			if ((grx[i] == grx[j] && gry[i] == gry[j]) || (grx[i] == gx && gry[i] == gy)) {unique = false}
		}
	}
	grid[grx[i]][gry[i]] = 6
	
//	alert(grx[i] + " " + gry[i])
	}
	
}
//*************************
function faceNorth() {
gd = 1
changeImage(gx, gy, gd)
}
//****************************
function faceSouth() {
gd = 2
changeImage(gx, gy, gd)
}
//****************************
function faceEast() {
gd = 3
changeImage(gx, gy, gd)
}
//****************************
function faceWest() {
gd = 4
changeImage(gx, gy, gd)
}
//****************************
function face(d) {
if (d == "N" || d == "n") {gd = 1};
else if (d == "S" || d == "s") {gd = 2};
else if (d == "E" || d == "e") {gd = 3};
else if (d == "W" || d == "w") {gd = 4};
else {alert("Invalid direction in face command.  Must be N,n,S,s,E,e, or W,w")}
changeImage(gx, gy,gd)
}
//***************************8
function facingNorth() {
if (gd == 1) {return(true)}
else {return(false)};
}
//***************************
function facingSouth() {
if (gd == 2) {return(true)}
else {return(false)};
}
//***************************
function facingEast() {
if (gd == 3) {return(true)}
else {return(false)};
}
//***************************
function facingWest() {
if (gd == 4) {return(true)}
else {return(false)};
}
//***************************
function directionIs(d) {
if ((d == 'N' || d == 'n') && gd == 1) {return(true)}
else if ((d == 'S' || d == 's') && gd == 2) {return(true)}
else if ((d == 'E' || d == 'e') && gd == 3) {return(true)}
else if ((d == 'W' || d == 'w') && gd == 4) {return(true)}
else {return(false)}
}
//****************************
function xPositionIs(x) {
if (isNaN(x)) {
	alert("ERROR - xPosition requires a number between 1 and 9")
	return(false)
}
var xx = parseInt(x)
if (xx > 9 || xx < 1) {
	alert("ERROR - xPosition requires a number between 1 and 9")
	return(false)
}
if (gx == xx) {return(true)}
else {return(false)}
}
//***************************
function yPositionIs(y) {
if (isNaN(y)) {
	alert("ERROR - yPosition requires a number between 1 and 9")
	return(false)
}
var xx = parseInt(y)
if (xx > 9 || xx < 1) {
	alert("ERROR - xPosition requires a number between 1 and 9")
	return(false)
}
if (gy == xx) {return(true)}
else {return(false)}
}
//***************************


function alongWall(wall) {
if ((wall == "N" || wall == "n") && gy == 1) {return(true)}
else if ((wall == "S" || wall == "s") && gy == 9) {return(true)}
else if ((wall == "E" || wall == "e") && gx == 9) {return(true)}
else if ((wall == "W" || wall == "w") && gx == 1) {return(true)}
else {return(false)}
}
//***************************8
function pathIsClear() {
if (gd == 1 && gy == 1) {return(false)}
else if (gd == 2 && gy == 9) {return(false)}
else if (gd == 3 && gx == 9) { return(false)}
else if (gd == 4 && gx == 1) {return(false)}
else {
	if (gd == 1 && grid[gx][gy-1] == 6){return(false)}
	else if (gd == 2 && grid[gx][gy+1] == 6){return(false)}
	else if (gd == 3 && grid[gx + 1][gy] == 6){return(false)}
	else if (gd == 4 && grid[gx-1][gy] == 6){return(false)}
	else {return(true)}
	}
return(true)
}
//***************************8
function pathIsNotClear() {
if (gd == 1 && gy == 1) {return(true)}
else if (gd == 2 && gy == 9) {return(true)}
else if (gd == 3 && gx == 9) { return(true)}
else if (gd == 4 && gx == 1) {return(true)}
else {
	if (gd == 1 && grid[gx][gy-1] == 6){return(true)}
	else if (gd == 2 && grid[gx][gy+1] == 6){return(true)}
	else if (gd == 3 && grid[gx + 1][gy] == 6){return(true)}
	else if (gd == 4 && grid[gx-1][gy] == 6){return(true)}
	else {return(false)}
	}
return(true)
}
//***************************
function turnLeft(){
if (gd == 1) {gd = 4; changeImage(gx,gy,gd); return}
else if (gd == 2) {gd = 3;changeImage(gx,gy,gd); return}
else if (gd == 3) {gd = 1;changeImage(gx,gy,gd);return}
else {gd = 2;changeImage(gx,gy,gd); return}
}
//***************************
function turnRight(){
if (gd == 1) {gd = 3;changeImage(gx,gy,gd); return}
else if (gd == 2) {gd = 4; changeImage(gx,gy,gd);return}
else if (gd == 3) {gd = 2; changeImage(gx,gy,gd);return}
else {gd = 1; changeImage(gx,gy,gd);return}
}
//**************************8
function move() {
var imgx;
var oldx, oldy;
oldx = gx; oldy = gy;


//imgx = eval("i" + gx + gy) //old position of robot
//imgx.src = imgs[5].src;  //sets previous position to nothing
if (gd == 1) {gy--};
else if (gd == 2){gy++};
else if (gd == 3){gx++};
else {gx--};
if (gx < 1 || gx > 9 || gy < 1 || gy > 9) {
	alert("You ran into the wall.  Your lawn mower is trashed!")
	gx = oldx;
	gy = oldy;
	btnRun.disabled = true
	gEnd = true
	return
}
else {
	if(grid[gx][gy] == 6) {
		alert("You ran into a gnome.  Now you're in trouble!")
		gx = oldx;
		gy = oldy;
		btnRun.disabled = true
		gEnd = true	
		return
	}
	else {
		grid[oldx][oldy] = 5;
		grid[gx][gy] = 5;
		changeImage(oldx, oldy, 5);
		changeImage(gx, gy, gd);
	}
}
//imgx = eval("i" + gx + gy)
//imgx.src = imgs[gd].src;  //sets new position
}
//*******************************************
function goX() {
var x = setTimeout("alert()", 1500)

}
//**********************************************
function moreToCut(){
var i,j;
for (i = 1; i<= 9; i++) {
	for (j = 1; j<=9; j++) {
	    if (gEnd == true) return(false);
		if (grid[i][j] == 0) {return(true)}
	}
}
return(false)
}
//*********************************************
function nextNeedsToBeCut() {
if (gd == 1 && gy == 1) {return(false)}
if (gd == 2 && gy == 9) {return(false)}
if (gd == 3 && gx == 9) {return(false)}
if (gd == 4 && gx == 1) {return(false)}
if (gd == 1 && grid[gx][gy-1] == 0) {return(true)} 
if(gd == 2 && grid[gx][gy+1] == 0) {return(true)} 
if(gd == 3 && grid[gx+1][gy] == 0) {return(true)} 
if(gd == 4 && grid[gx - 1][gy] == 0) {return(true)}
return(false)
}
//**********************************************
function nothingAdjacentNeedsCut() {
if (gx > 1 && gx < 9 && gy > 1 && gy < 9){
	if (grid[gx+1][gy] == 0 || grid[gx-1][gy] == 0 || grid[gx][gy+1] == 0 || grid[gx][gy-1] == 0) {
		return(false)}
	else {return(true)}
	}
if (gx == 1) {
	if (gy != 1 && gy != 9) {
		if (grid[gx+1][gy] == 0 || grid[gx][gy+1] == 0 || grid[gx][gy-1] == 0) {
			return(false)}
		else {return(true)}
	}
	if (gy == 1) {
		if (grid[gx+1][gy] == 0 || grid[gx][gy+1] == 0) {
			return(false)}
		else {return(true)}
	}
	if (gy == 9) {
		if (grid[gx+1][gy] == 0 || grid[gx][gy-1] == 0) {
			return(false)}
		else {return(true)}
	}
}
if (gx == 9) {
	if (gy != 1 && gy != 9) {
		if (grid[gx-1][gy] == 0 || grid[gx][gy+1] == 0 || grid[gx][gy-1] == 0) {
			return(false)}
		else {return(true)}
	}
	if (gy == 1) {
		if (grid[gx-1][gy] == 0 || grid[gx][gy+1] == 0) {
			return(false)}
		else {return(true)}
	}
	if (gy == 9) {
		if (grid[gx-1][gy] == 0 || grid[gx][gy-1] == 0) {
			return(false)}
		else {return(true)}
	}
}

if (gy == 1) {
	if (gx != 1 && gx != 9) {
		if (grid[gx+1][gy] == 0 || grid[gx][gy+1] == 0 || grid[gx-1][gy] == 0) {
			return(false)}
		else {return(true)}
	}
}

if (gy == 9) {
	if (gx != 1 && gx != 9) {
		if (grid[gx+1][gy] == 0 || grid[gx][gy-1] == 0 || grid[gx-1][gy] == 0) {
			return(false)}
		else {return(true)}
	}
}

}
//*********************************************
function pause() {
		alert()
		
		var t = new Date
		var to = t.getMilliseconds()
		var tf = 0
		var tt = 0
		
		if (to >= 500) {
			tf = to - 500
		//document.writeln(tf)
			do {
				var tt = new Date() 
				//document.write(tt.getMilliseconds() + "<br>" )
				}
			while((tt.getMilliseconds()< tf || tt.getMilliseconds() >= to))
		//alert("to = " + to + " tf = " + tf + " tt = " + tt.getMilliseconds())
			
			}
		else {
			tf = to + 500
			do {
				var tt = new Date()  }
			while(tt.getMilliseconds() < tf) 
		}
		
		}
//********************************************

//********************************************
function initializeGrid() {
var sgrid = ""
var i,j
for (i = 1; i<= 9; i++) {
	for (j = 1; j<=9; j++) {
	    
		//	sgrid = sgrid + "<div class = griddiv id = g" + i + j + " style = 'left:" + (i * 40) + "; top: " + (20+(j-1)*40) +"'>x</div>"
		 //}
		sgrid = sgrid + "<img id = i" + i + j + " style = 'left:" + (i * 40) + "; top: " + (20+(j-1)*40) + "' src = " + imgs[grid[i][j]].src + ">" //ball.bmp>"
		}
	}
return (sgrid)
}
//************************************************
function changeImage(x, y, d) {
var imgx;
imgx = eval("i" + x + y)
imgx.src = imgs[d].src;  //sets new position
}
//************************************************
function loadGuy() {
changeImage(gx,gy,gd)
initializeRocks();
var i
for (i=0; i <= numRocks - 1; i++) {
	changeImage(grx[i], gry[i], 6) 
}
btnRun.style.visibility = "visible"
btnInitial.style.visibility = "hidden"
}
//************************************************
function drawGrid() {
var sgrid = ""
var i,j
for (i = 1; i<= 9; i++) {
	for (j = 1; j<=9; j++) {
	    
			sgrid = sgrid + "<div class = griddiv id = g" + i + j + " style = 'left:" + (300+(i * 40)) + "; top: " + (20+(j-1)*40) +"'></div>"
		}
		//sgrid = sgrid + "<img id = i" + i + j + " left=" + i * 20 + " top= " + j*20 +" src = ball.bmp>"

	}
return (sgrid)
}

//*************************8
function replaceX(xo, yo, direction, numberRocks){

				var sS = "<HTML>" + "\n" 
				sS = sS + "\n<HEAD>\n"

				sS = sS + "<SCRIPT>\n";
					sS = sS + "var gx = " + xo + ";"
					sS = sS + "var gy = " + yo + ";"
					sS = sS + "var gd = " + direction + ";"
					sS = sS + "var numRocks = " + numberRocks + ";"
				sS = sS + "function step(){\n";
				sS = sS + Textarea1.value;
				sS = sS + "\n}"
				sS = sS + "\n<" + "\/" + "SCRIPT>";
				sS = sS + "<SCRIPT src = gojo.js><" + "\/" + "SCRIPT>"
				
				sS = sS + "<link rel='stylesheet' type='text" + "\/" + "css' href='gojo.css'" + "\/" + ">" 

				
				
				sS = sS + "\n<\/HEAD>\n<BODY><img id = iwall src = wall.gif><div id = griddiv>" 
				sS = sS + initializeGrid();
				sS = sS + "<\/div>"
				sS = sS + "\n" 
				//sS = sS + "\n<textarea id = Textarea1 disabled rows = 20 cols = 20>" + Textarea1.value + "<\/textarea><br>"
				sS = sS + "\n<Input type='button' value='Initialize' ID='btnInitial' NAME='btnInitial' onclick = 'loadGuy()'>";
				sS = sS + "\n<Input type='button' value='Execute Code' ID='btnRun' NAME='btnRun' onclick = 'step()'>";
			
					sS = sS + "\n\n<\/BODY>\n<" + "\/" + "HTML>";
				//alert(sS);
				
			//	var oNewDoc = document.open("text/html");
			return(sS)	
			
			//	oNewDoc.write(sS);
				}