OK - So I must be bored...
<title>Javascript Tile Puzzle</title>
<script>
function mixEm(){
document.getElementById("spanPlace".innerHTML = ""
tempArr = new Array()
for (x=0; x<dLevel*dLevel; x++){
tempArr[x] = x
if (x==0){
newSpans = "<span class=tile id=t0></span>"
}
else{
newSpans += "<span class=tile id=t" + x + " onClick='moveEm(this)'>" + x + "</span>"
}
}
document.getElementById("spanPlace".innerHTML = newSpans
window.tileArr = new Array()
//now mix them up
numElems = tempArr.length
for (x=0; x<numElems; x++){
ranNum = Math.floor(Math.random() * tempArr.length)
tileArr.push(tempArr[ranNum])
tempArr.splice(ranNum,1)
}
}
function showEm(){
allInputs = document.getElementsByTagName("input"
for (x=0; x<allInputs.length; x++){
if (allInputs[x].checked){
window.dLevel = parseInt(allInputs[x].value)
break;
}
}
mixEm()
window.numClicks = 0
curRow = 0
curCol = 0
for (x=0; x<tileArr.length; x++){
if ( x % dLevel == 0 ){
curRow = 1
curCol ++
}
thisTile = document.getElementById("t" + tileArr[x])
thisTile.style.top = curCol * 50
thisTile.style.left = curRow * 50
curRow ++
if (tileArr[x] > 0){
tileArr[x] % 2 == 0 ? color = "yellow" : color = "green"
thisTile.style.backgroundColor = color
}
}
checkEm()
}
function moveEm(inObj){
zeroTile = document.getElementById("t0"
zeroXpos = parseInt(zeroTile.style.left)
zeroYpos = parseInt(zeroTile.style.top)
inXpos = parseInt(inObj.style.left)
inYpos = parseInt(inObj.style.top)
if (zeroXpos == inXpos){
if (Math.abs(zeroYpos - inYpos) == 50){
zeroTile.style.top = inYpos
inObj.style.top = zeroYpos
numClicks ++
}
}
else if (zeroYpos == inYpos){
if (Math.abs(zeroXpos - inXpos) == 50){
zeroTile.style.left = inXpos
inObj.style.left = zeroXpos
numClicks ++
}
}
checkEm()
}
function checkEm(){
curRow = 0
curCol = 0
numWrong = 0
for (x=1; x<tileArr.length; x++){
if ((x-1) % dLevel == 0 ){
curRow = 1
curCol ++
}
thisTile = document.getElementById("t" + x)
if (parseInt(thisTile.style.top) == curCol * 50 && parseInt(thisTile.style.left) == curRow * 50){
thisTile.style.color = "black"
}
else{
thisTile.style.color = "red"
numWrong ++
}
curRow ++
}
window.status = numWrong + " wrong -- " + numClicks + " moves."
if (numWrong == 0){
alert("Solved in " + numClicks + " clicks"
}
}
</script>
<style>
body {font-family: arial;}
span {text-align:center;}
.tile {font-size: 26pt; border: solid blue 3px; width: 50px; height: 50px; position:absolute; }
</style>
<body onLoad="showEm()" id="bodyNode">
<div id="spanPlace">
<span class=tile id=t0></span>
</div>
<div style="position: absolute; top: 50; left:400;">
<form name="gameForm">
<P><input type=radio name="levelPicker" value=3 onClick="showEm()"> Easy (3x3)</P>
<P><input type=radio name="levelPicker" value=4 onClick="showEm()" Checked> Medium (4x4)</P>
<P><input type=radio name="levelPicker" value=5 onClick="showEm()"> Hard (5x5)</P>
<P><input type=radio name="levelPicker" value=6 onClick="showEm()"> Impossible (6x6)</P>
<input type=button onClick="showEm()" value="Start Over">
</form>
</div>
</body>
Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)
<title>Javascript Tile Puzzle</title>
<script>
function mixEm(){
document.getElementById("spanPlace".innerHTML = ""
tempArr = new Array()
for (x=0; x<dLevel*dLevel; x++){
tempArr[x] = x
if (x==0){
newSpans = "<span class=tile id=t0></span>"
}
else{
newSpans += "<span class=tile id=t" + x + " onClick='moveEm(this)'>" + x + "</span>"
}
}
document.getElementById("spanPlace".innerHTML = newSpans
window.tileArr = new Array()
//now mix them up
numElems = tempArr.length
for (x=0; x<numElems; x++){
ranNum = Math.floor(Math.random() * tempArr.length)
tileArr.push(tempArr[ranNum])
tempArr.splice(ranNum,1)
}
}
function showEm(){
allInputs = document.getElementsByTagName("input"
for (x=0; x<allInputs.length; x++){
if (allInputs[x].checked){
window.dLevel = parseInt(allInputs[x].value)
break;
}
}
mixEm()
window.numClicks = 0
curRow = 0
curCol = 0
for (x=0; x<tileArr.length; x++){
if ( x % dLevel == 0 ){
curRow = 1
curCol ++
}
thisTile = document.getElementById("t" + tileArr[x])
thisTile.style.top = curCol * 50
thisTile.style.left = curRow * 50
curRow ++
if (tileArr[x] > 0){
tileArr[x] % 2 == 0 ? color = "yellow" : color = "green"
thisTile.style.backgroundColor = color
}
}
checkEm()
}
function moveEm(inObj){
zeroTile = document.getElementById("t0"
zeroXpos = parseInt(zeroTile.style.left)
zeroYpos = parseInt(zeroTile.style.top)
inXpos = parseInt(inObj.style.left)
inYpos = parseInt(inObj.style.top)
if (zeroXpos == inXpos){
if (Math.abs(zeroYpos - inYpos) == 50){
zeroTile.style.top = inYpos
inObj.style.top = zeroYpos
numClicks ++
}
}
else if (zeroYpos == inYpos){
if (Math.abs(zeroXpos - inXpos) == 50){
zeroTile.style.left = inXpos
inObj.style.left = zeroXpos
numClicks ++
}
}
checkEm()
}
function checkEm(){
curRow = 0
curCol = 0
numWrong = 0
for (x=1; x<tileArr.length; x++){
if ((x-1) % dLevel == 0 ){
curRow = 1
curCol ++
}
thisTile = document.getElementById("t" + x)
if (parseInt(thisTile.style.top) == curCol * 50 && parseInt(thisTile.style.left) == curRow * 50){
thisTile.style.color = "black"
}
else{
thisTile.style.color = "red"
numWrong ++
}
curRow ++
}
window.status = numWrong + " wrong -- " + numClicks + " moves."
if (numWrong == 0){
alert("Solved in " + numClicks + " clicks"
}
}
</script>
<style>
body {font-family: arial;}
span {text-align:center;}
.tile {font-size: 26pt; border: solid blue 3px; width: 50px; height: 50px; position:absolute; }
</style>
<body onLoad="showEm()" id="bodyNode">
<div id="spanPlace">
<span class=tile id=t0></span>
</div>
<div style="position: absolute; top: 50; left:400;">
<form name="gameForm">
<P><input type=radio name="levelPicker" value=3 onClick="showEm()"> Easy (3x3)</P>
<P><input type=radio name="levelPicker" value=4 onClick="showEm()" Checked> Medium (4x4)</P>
<P><input type=radio name="levelPicker" value=5 onClick="showEm()"> Hard (5x5)</P>
<P><input type=radio name="levelPicker" value=6 onClick="showEm()"> Impossible (6x6)</P>
<input type=button onClick="showEm()" value="Start Over">
</form>
</div>
</body>
Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)