Menu
I have been toying with the idea of building a 15 puzzle just for fun and have been looking at some source code from David Harrison but i wish to add a shuffle images and a reset button into the content. I have been trying to use onClick events within a form button but cant get the blasted thing to work! any help would be much appreciated!!!!
here is the source code:
[code=html]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-script-type" content="text/javascript">
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<title>Game</title>
<style type="text/css">
#game img {
width:50px;
height:50px;
border:1px solid red;
font-size:3em;
}
</style>
<script type="text/javascript">
var piece, x, y;
function ReorderPuzzle() {
piece=new Array();
piece[piece.length]=new Array("pic00.jpg","pic01.jpg","pic02.jpg","pic03.jpg");
piece[piece.length]=new Array("pic10.jpg","pic11.jpg","pic12.jpg","pic13.jpg");
piece[piece.length]=new Array("pic20.jpg","pic21.jpg","pic22.jpg","pic23.jpg");
piece[piece.length]=new Array("pic30.jpg","pic31.jpg","pic32.jpg","blank.jpg");
swop();
}
function check(imgnum) {
x=parseInt(imgnum.substring(4,5)); // Retrieves the x co-ordinate of the image from it's ID.
y=parseInt(imgnum.substring(3,4)); // Retrieves the y co-ordinate of the image from it's ID.
// alert(y+':'+x); // for testing purposes
var ylow=(y-1<0)?0:y-1; // This makes sure that the lower bound of y does not drop below 0.
var yhigh=(y+1>3)?0:y+1; // This makes sure that the upper bound of y does not drop go above 3.
var xlow=(x-1<0)?0:x-1; // This makes sure that the lower bound of x does not drop below 0.
var xhigh=(x+1>3)?0:x+1; // This makes sure that the upper bound of x does not drop go above 3.
if(document.getElementById(imgnum).src!="blank.jpg") {
if(piece[ylow][x]=="blank.jpg"){piece[ylow][x]=piece[y][x];piece[y][x]="blank.jpg";} // Checks above y for a blank image and if it finds one a swop is made.
else if(piece[yhigh][x]=="blank.jpg"){piece[yhigh][x]=piece[y][x];piece[y][x]="blank.jpg";} // Checks below y for a blank image and if it finds one a swop is made.
else if(piece[y][xlow]=="blank.jpg"){piece[y][xlow]=piece[y][x];piece[y][x]="blank.jpg";} // Checks above x for a blank image and if it finds one a swop is made.
else if(piece[y][xhigh]=="blank.jpg"){piece[y][xhigh]=piece[y][x];piece[y][x]="blank.jpg";} // Checks below x for a blank image and if it finds one a swop is made.
else{alert("Invalid move!!!");} // If there is no blank image around the clicked image then the user is notified.
} else{alert("Invalid move!!!");} // If the user clicks on the blank image then they are notified.
swop();
}
function swop() {
for(var m=0;m<4;m++) {
for(var n=0;n<4;n++) {
document.getElementById("pic"+m+n).src=piece[m][n]; // Swops all the image sources for the ones in the array.
document.getElementById("pic"+m+n).alt=piece[m][n];
}
}
}
function randOrd() { return (Math.round(Math.random())-0.5); }
function RandomizePuzzle() {
var RndArr = ['00','01','02','03','10','11','12','13','20','21','22','23','30','31','32','33'];
RndArr.sort(randOrd);
RndArr.sort(randOrd); // do it again just for kicks if desired
// alert(RndArr.join(' ')); // for testing purposes
var i = 0;
for(var m=0;m<4;m++) {
for(var n=0;n<4;n++) {
var pix = 'pic'+RndArr[i]; // alert(pix);
if (pix == 'pic33') { piece[m][n] = 'blank.jpg'; }
else { piece[m][n] = pix+'.jpg'; }
document.getElementById(pix).src=piece[m][n];
document.getElementById(pix).alt=piece[m][n];
i++;
}
}
swop();
}
</script>
</head>
<body onload="ReorderPuzzle()">
<div id="game">
<img src="" id="pic00" onclick="check(this.id);" alt="">
<img src="" id="pic01" onclick="check(this.id);" alt="">
<img src="" id="pic02" onclick="check(this.id);" alt="">
<img src="" id="pic03" onclick="check(this.id);" alt=""><br>
<img src="" id="pic10" onclick="check(this.id);" alt="">
<img src="" id="pic11" onclick="check(this.id);" alt="">
<img src="" id="pic12" onclick="check(this.id);" alt="">
<img src="" id="pic13" onclick="check(this.id);" alt=""><br>
<img src="" id="pic20" onclick="check(this.id);" alt="">
<img src="" id="pic21" onclick="check(this.id);" alt="">
<img src="" id="pic22" onclick="check(this.id);" alt="">
<img src="" id="pic23" onclick="check(this.id);" alt=""><br>
<img src="" id="pic30" onclick="check(this.id);" alt="">
<img src="" id="pic31" onclick="check(this.id);" alt="">
<img src="" id="pic32" onclick="check(this.id);" alt="">
<img src="" id="pic33" onclick="check(this.id);" alt="">
</div>
<button onclick="RandomizePuzzle()">Shuffle</button>
<button onclick="ReorderPuzzle()">Reorder</button>
</body>
</html>
[/code]
[code=html]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-script-type" content="text/javascript">
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<title>Game</title>
<style type="text/css">
#game img {
width:50px;
height:50px;
padding: 0px 0px 0px 0px;
margin: 0px 0px 0px 0px;
/* border:1px solid red; */
font-size:3em;
}
</style>
<script type="text/javascript">
var piece, x, y;
function ReorderPuzzle() {
piece=new Array();
piece[piece.length]=new Array("pic00.jpg","pic01.jpg","pic02.jpg","pic03.jpg");
piece[piece.length]=new Array("pic10.jpg","pic11.jpg","pic12.jpg","pic13.jpg");
piece[piece.length]=new Array("pic20.jpg","pic21.jpg","pic22.jpg","pic23.jpg");
piece[piece.length]=new Array("pic30.jpg","pic31.jpg","pic32.jpg","blank.jpg");
swop();
}
function check(imgnum) {
x=parseInt(imgnum.substring(4,5)); // Retrieves the x co-ordinate of the image from it's ID.
y=parseInt(imgnum.substring(3,4)); // Retrieves the y co-ordinate of the image from it's ID.
// alert(y+':'+x); // for testing purposes
var ylow=(y-1<0)?0:y-1; // This makes sure that the lower bound of y does not drop below 0.
var yhigh=(y+1>3)?0:y+1; // This makes sure that the upper bound of y does not drop go above 3.
var xlow=(x-1<0)?0:x-1; // This makes sure that the lower bound of x does not drop below 0.
var xhigh=(x+1>3)?0:x+1; // This makes sure that the upper bound of x does not drop go above 3.
if(document.getElementById(imgnum).src!="blank.jpg") {
if(piece[ylow][x]=="blank.jpg"){piece[ylow][x]=piece[y][x];piece[y][x]="blank.jpg";} // Checks above y for a blank image and if it finds one a swop is made.
else if(piece[yhigh][x]=="blank.jpg"){piece[yhigh][x]=piece[y][x];piece[y][x]="blank.jpg";} // Checks below y for a blank image and if it finds one a swop is made.
else if(piece[y][xlow]=="blank.jpg"){piece[y][xlow]=piece[y][x];piece[y][x]="blank.jpg";} // Checks above x for a blank image and if it finds one a swop is made.
else if(piece[y][xhigh]=="blank.jpg"){piece[y][xhigh]=piece[y][x];piece[y][x]="blank.jpg";} // Checks below x for a blank image and if it finds one a swop is made.
else{alert("Invalid move!!!");} // If there is no blank image around the clicked image then the user is notified.
} else{alert("Invalid move!!!");} // If the user clicks on the blank image then they are notified.
swop();
}
function swop() {
for(var m=0;m<4;m++) {
for(var n=0;n<4;n++) {
document.getElementById("pic"+m+n).src="_images/"+piece[m][n]; // Swops all the image sources for the ones in the array.
document.getElementById("pic"+m+n).alt=piece[m][n];
}
}
}
function randOrd() { return (Math.round(Math.random())-0.5); }
function RandomizePuzzle() {
var RndArr = ['00','01','02','03','10','11','12','13','20','21','22','23','30','31','32','33'];
RndArr.sort(randOrd);
RndArr.sort(randOrd); // do it again just for kicks if desired
// alert(RndArr.join(' ')); // for testing purposes
var i = 0;
for(var m=0;m<4;m++) {
for(var n=0;n<4;n++) {
var pix = 'pic'+RndArr[i]; // alert(pix);
if (pix == 'pic33') { piece[m][n] = 'blank.jpg'; }
else { piece[m][n] = pix+'.jpg'; }
document.getElementById(pix).src="_images/"+piece[m][n];
document.getElementById(pix).alt=piece[m][n];
i++;
}
}
swop();
}
</script>
</head>
<body onload="ReorderPuzzle()">
<div id="game">
<img src="" id="pic00" onclick="check(this.id);" alt="" border"0">
<img src="" id="pic01" onclick="check(this.id);" alt="" border"0">
<img src="" id="pic02" onclick="check(this.id);" alt="" border"0">
<img src="" id="pic03" onclick="check(this.id);" alt="" border"0"><br>
<img src="" id="pic10" onclick="check(this.id);" alt="" border"0">
<img src="" id="pic11" onclick="check(this.id);" alt="" border"0">
<img src="" id="pic12" onclick="check(this.id);" alt="" border"0">
<img src="" id="pic13" onclick="check(this.id);" alt="" border"0"><br>
<img src="" id="pic20" onclick="check(this.id);" alt="" border"0">
<img src="" id="pic21" onclick="check(this.id);" alt="" border"0">
<img src="" id="pic22" onclick="check(this.id);" alt="" border"0">
<img src="" id="pic23" onclick="check(this.id);" alt="" border"0"><br>
<img src="" id="pic30" onclick="check(this.id);" alt="" border"0">
<img src="" id="pic31" onclick="check(this.id);" alt="" border"0">
<img src="" id="pic32" onclick="check(this.id);" alt="" border"0">
<img src="" id="pic33" onclick="check(this.id);" alt="" border"0">
</div>
<button onclick="RandomizePuzzle()">Shuffle</button>
<button onclick="ReorderPuzzle()">Reorder</button>
</body>
</html>
[/code]
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-script-type" content="text/javascript">
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
<title>Game</title>
<style type="text/css">
#game span {
padding: 0px 0px 0px 0px;
margin: 0px 0px 0px 0px;
}
#game img {
/*
width:50px;
height:50px;
*/
font-size:3em;
}
</style>
<script type="text/javascript">
var piece, x, y;
function ReorderPuzzle() {
piece=new Array();
piece[piece.length]=new Array("pic00.jpg","pic01.jpg","pic02.jpg","pic03.jpg");
piece[piece.length]=new Array("pic10.jpg","pic11.jpg","pic12.jpg","pic13.jpg");
piece[piece.length]=new Array("pic20.jpg","pic21.jpg","pic22.jpg","pic23.jpg");
piece[piece.length]=new Array("pic30.jpg","pic31.jpg","pic32.jpg","blank.jpg");
document.getElementById('MoveCnt').value = -1;
swop();
}
function check(imgnum) {
x=parseInt(imgnum.substring(4,5)); // Retrieves the x co-ordinate of the image from it's ID.
y=parseInt(imgnum.substring(3,4)); // Retrieves the y co-ordinate of the image from it's ID.
// alert(y+':'+x); // for testing purposes
var ylow=(y-1<0)?0:y-1; // This makes sure that the lower bound of y does not drop below 0.
var yhigh=(y+1>3)?0:y+1; // This makes sure that the upper bound of y does not drop go above 3.
var xlow=(x-1<0)?0:x-1; // This makes sure that the lower bound of x does not drop below 0.
var xhigh=(x+1>3)?0:x+1; // This makes sure that the upper bound of x does not drop go above 3.
if(document.getElementById(imgnum).src!="blank.jpg") {
if(piece[ylow][x]=="blank.jpg"){piece[ylow][x]=piece[y][x];piece[y][x]="blank.jpg";} // Checks above y for a blank image and if it finds one a swop is made.
else if(piece[yhigh][x]=="blank.jpg"){piece[yhigh][x]=piece[y][x];piece[y][x]="blank.jpg";} // Checks below y for a blank image and if it finds one a swop is made.
else if(piece[y][xlow]=="blank.jpg"){piece[y][xlow]=piece[y][x];piece[y][x]="blank.jpg";} // Checks above x for a blank image and if it finds one a swop is made.
else if(piece[y][xhigh]=="blank.jpg"){piece[y][xhigh]=piece[y][x];piece[y][x]="blank.jpg";} // Checks below x for a blank image and if it finds one a swop is made.
else{alert("Invalid move!!!");} // If there is no blank image around the clicked image then the user is notified.
} else{alert("Invalid move!!!");} // If the user clicks on the blank image then they are notified.
swop();
}
function swop() {
document.getElementById('MoveCnt').value = parseInt(document.getElementById('MoveCnt').value) + 1;
for(var m=0;m<4;m++) {
for(var n=0;n<4;n++) {
document.getElementById("pic"+m+n).src="_images/"+piece[m][n]; // Swops all the image sources for the ones in the array.
document.getElementById("pic"+m+n).alt=piece[m][n];
}
}
}
function randOrd() { return (Math.round(Math.random())-0.5); }
function RandomizePuzzle() {
var RndArr = ['00','01','02','03','10','11','12','13','20','21','22','23','30','31','32','33'];
RndArr.sort(randOrd);
RndArr.sort(randOrd); // do it again just for kicks if desired
// alert(RndArr.join(' ')); // for testing purposes
var i = 0;
for(var m=0;m<4;m++) {
for(var n=0;n<4;n++) {
var pix = 'pic'+RndArr[i]; // alert(pix);
if (pix == 'pic33') { piece[m][n] = 'blank.jpg'; }
else { piece[m][n] = pix+'.jpg'; }
document.getElementById(pix).src="_images/"+piece[m][n];
document.getElementById(pix).alt=piece[m][n];
i++;
}
}
document.getElementById('MoveCnt').value = -1;
swop();
}
</script>
</head>
<body onload="ReorderPuzzle()">
<div id="game">
<span><img src="" id="pic00" onclick="check(this.id);" alt="" border"0"></span>
<span><img src="" id="pic01" onclick="check(this.id);" alt="" border"0"></span>
<span><img src="" id="pic02" onclick="check(this.id);" alt="" border"0"></span>
<span><img src="" id="pic03" onclick="check(this.id);" alt="" border"0"></span><br>
<span><img src="" id="pic10" onclick="check(this.id);" alt="" border"0"></span>
<span><img src="" id="pic11" onclick="check(this.id);" alt="" border"0"></span>
<span><img src="" id="pic12" onclick="check(this.id);" alt="" border"0"></span>
<span><img src="" id="pic13" onclick="check(this.id);" alt="" border"0"></span><br>
<span><img src="" id="pic20" onclick="check(this.id);" alt="" border"0"></span>
<span><img src="" id="pic21" onclick="check(this.id);" alt="" border"0"></span>
<span><img src="" id="pic22" onclick="check(this.id);" alt="" border"0"></span>
<span><img src="" id="pic23" onclick="check(this.id);" alt="" border"0"></span><br>
<span><img src="" id="pic30" onclick="check(this.id);" alt="" border"0"></span>
<span><img src="" id="pic31" onclick="check(this.id);" alt="" border"0"></span>
<span><img src="" id="pic32" onclick="check(this.id);" alt="" border"0"></span>
<span><img src="" id="pic33" onclick="check(this.id);" alt="" border"0"></span>
</div>
<button onclick="RandomizePuzzle()">Shuffle</button>
<button onclick="ReorderPuzzle()">Reorder</button>
<input type="text" value="0" id="MoveCnt" size="3" readonly> Moves
</body>
</html>
[/code]
0.1.9 — BETA 5.18