Yusuf Vindhani

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromYusuf Vindhani
ToMe
SubjectJava Script
Date9 March 2004 12:01
The Author,

I was just wondering whther you knew how to create a puzzle game using java
script.  The game has to have one black square and be able to randomise the
the pictures at the click of a button and also solve the puzzle at the click
of a button.

If so can u please give me some sort of code on how to do this.

Regards
FromMe
ToYusuf Vindhani
SubjectRe: Java Script
Date9 March 2004 12:47
Yusuf,

I have not written a script to do this, but it is a fairly easy thing to do.
You take a picture, copy it into 'n' DIV elements, then clip each one of
them so that only a set square of each will be shown. When you position
them correctly, it makes a picture.

You would need to keep a couple of arrays of positions to work out what
square was beside the blank square, and allow them to move. The usual
approach is to detect the click event on the div and make it swap places
with the blank square if it is beside it.

Once all elements are in the right places, they have won. To solve the
puzzle, remember every move that has happened (assuming you start with the
complete puzzle then randomly move squares to prepare it), and play them in
reverse order using timers.

If you want to write your own script, you may find that using my 'generic
DHTML do everything library' is helpful:
http://www.howtocreate.co.uk/jslibs/

If you want a complete script, there is one on [dynamicdrive]
but sadly it is poorly written, and will not work properly cross browser.


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.