WebDevelopersJournal.comTips on Web Page Design, HTML and Graphics
SITE SEARCH
Newsletters
Java/Open Source Daily



Jobs at webdeveloper.com

Resources By Subject
Technical
Graphical
Authoring
Business
WDJ resources
Archive

internet.com

internet.commerce
  • Partner With Us
















Developer Channel


Find a web host with:
CGI Access DB Support Telnet Access
NT Servers UNIX Servers



Semi-automatic?

JavaScript
JavaScript Helper:
Meet Paige Turner, the least geeky geek we've ever come across.

Variables and Operators Explained:
First of a three part guide to JavaScript basics.

Controlling Forms:
Enhance your HTML forms with a touch of JS.

DHTML:
Forget how it works, let's see some in action!


Anatomy of a JavaScript Picture Puzzle (2)

by Guy Laor

Apparent Slicing

<< Back to Page One

Block Movement

We are close to completing our JavaScript game. The only thing missing is the JavaScript code that moves the blocks and allows the game to be played. Since each block has only one direction it can move (into the empty space), there's no need for a Drag & Drop movement. Instead they move when they're clicked on.

The script makes an Array that will keep the position of the empty block, which starts in sixteenth place.

AName = new Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);

Then, each time the user clicks on a block, the function "pop" is called.

function pop(val) {
for (a=0; a<AName.length; a++) {
if (AName[a] == val) {
Cell = a;
}}
if (AName[Cell-1] == 15 || AName[Cell+1] == 15 || AName[Cell+4] == 15 || AName[Cell-4] == 15 ) {

The last line checks if the block that the user clicks on is before or after the fifteenth block, or if it is four blocks away, which means it is either above or below the empty block. Then it changes the position of the clicked block with the position of the empty one. Finally it changes the position of the fifteenth block inside the array, so the next time the user clicks on a block the script can check it.

var NewDiv = eval("document.all.Holder"+AName[Cell]);
var NewTop = NewDiv.style.top;
var NewLeft = NewDiv.style.left;
var OldDiv = eval("document.all.Holder"+AName[OldNum]);
var OldTop = OldDiv.style.top;
var OldLeft = OldDiv.style.left;
OldDiv.style.left = NewLeft;
OldDiv.style.top = NewTop;
NewDiv.style.top = OldTop;
NewDiv.style.left = OldLeft;
var save = AName[OldNum];
AName[OldNum] = AName[Cell];
AName[Cell] = save;
OldTop = NewTop = OldLeft = NewLeft = OldNum = null;

Here's the game itself.

Finally, here the full script source code for you to read and run. Watch out for extra linebreaks introduced by the constraints of this Web page.


<script language="JavaScript1.2">
// script by guy laor - all rights reserved 2000
var cells = 16;
var rows = 4;
var lines = 4;
var Ihight, Iwidth, cellH, cellW;
HoldDiv = new Array;
InnerDiv = new Array;
RandNum = new Array;
RandNum.length =0;
var Itop = Ileft = 100;
var Ctop = Cleft = 0;

function init(valueimg)
{
if (document.layers) {
alert("No Netscape users please! Try Internet Explorer for this page.");
return;
}
BaseImg = new Image;
IMAGE = valueimg;
BaseImg.src = valueimg; //"birth_of_venus.jpg";
Ihight = BaseImg.height;
Iwidth = BaseImg.width;

cellH = parseFloat(Ihight/rows);
cellW = parseFloat(Iwidth/lines);
var Divnum=0;
for (i=0; i<rows; i++) {
for (a=0; a<lines; a++) {
Makeholder(Divnum, Itop, Ileft, cellH, cellW);
Ileft = Ileft+cellW;
MakeInner(Divnum, Ctop, Cleft);
Cleft = Cleft-cellW;
Divnum++;
}
Ileft = 100;
Cleft = 0;
Itop = Itop+cellH;
Ctop = Ctop-cellH;
}
MakeRound();
document.write('<html><body vlink="Gray" link="Gray" alink="Gray">');
document.write('<BR><a href="game.html"><font face="Verdana">Start Again</font></a>');
document.write('<div style="visibility:hidden"><img src="campbell.jpg" width="1" height="1" border="0" alt=""><img src="birth_of_venus.jpg" width="1" height="1" border="0" alt=""><img src="dali3.jpg" width="1" height="1" border="0" alt=""><img src="faceofmick.jpg" width="1" height="1" border="0" alt=""></div>');
document.write(Scrip);
for (q=0; q<cells; q++) {
document.write(HoldDiv[q]);
var Rnumber = RandNum[q];
document.write(InnerDiv[Rnumber]);
if (q != cells-1){
document.write('<img src="'+IMAGE+'" name="mag'+q+'" border="0">');
eval ("document.mag"+q+".src = BaseImg.src");
}
document.write("</div></div>");

}


}

function Makeholder(number, top, left, ctop, cleft)
{
HoldDiv[number] = '<div id="Holder'+number+'" style="position: absolute; top: '+top+'; left:'+left+'; clip : rect(0px, '+cleft+'px, '+ctop+'px, 0px)" onclick="pop('+number+')" >';

}

function MakeInner(number, top, left) {
InnerDiv[number] = '<div id="Inn'+number+'" style="position: absolute; top:'+top+'; left:'+left+';">';
}

function MakeRound() {

RandNum = new Array(7,12,14,3,9,6,0,8,5,10,4,11,1,13,2,15,16);

}

var Scrip = '<script language="JavaScript1.2">';
Scrip += 'AName = new Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);';
Scrip += 'var Cell=null;';
Scrip += 'function pop(val) {';
Scrip += 'for (a=0; a<AName.length; a++) {';
Scrip += 'if (AName[a] == val) {';
Scrip += 'Cell = a; ';
Scrip += '}}';
Scrip += 'if (AName[Cell-1] == 15 || AName[Cell+1] == 15 || AName[Cell+4] == 15 || AName[Cell-4] == 15 ) {';
Scrip += 'var NewDiv = eval("document.all.Holder"+AName[Cell]);';
Scrip += 'var NewTop = NewDiv.style.top;';
Scrip += 'var NewLeft = NewDiv.style.left;';
Scrip += 'var OldDiv = eval("document.all.Holder"+AName[OldNum]);';
Scrip += 'var OldTop = OldDiv.style.top;';
Scrip += 'var OldLeft = OldDiv.style.left;';
Scrip += 'AHolder[OldNum] = 1;';
Scrip += 'AHolder[Cell] = 0;';
Scrip += 'OldDiv.style.left = NewLeft;';
Scrip += 'OldDiv.style.top = NewTop;';
Scrip += 'NewDiv.style.top = OldTop;';
Scrip += 'NewDiv.style.left = OldLeft;';
Scrip += 'var save = AName[OldNum];';
Scrip += 'AName[OldNum] = AName[Cell];';
Scrip += 'AName[Cell] = save;';
Scrip += 'OldTop = NewTop = OldLeft = NewLeft = OldNum = null;';
Scrip += '}}';
Scrip += '</';
Scrip += 'script>';

</script>

Guy Laor is webmaster at PictureVision Israel R&D.
Suits PonytailsPropheadsContact WDJDiscussWeb AudioSearch


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers