/    Sign up×
Community /Pin to ProfileBookmark

4X4 puzzle DOM based

Hiya i am tryin to build a 4×4 sliding puzzle the problem is i dont know how to code the part of javascript where it checks to see if the move is valid. I want it so that only a tile adjacent to a free tile can be moved into the free tile space. I know one way of doing it but it involes 48 IF statements lol surely there must be a better way. Any ideas??

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Mar 09.2004 — that might be the best way of doing it, do you have a link for us to see the code if it is too large to send, therefore we can help to see if you can cut it down.
Copy linkTweet thisAlerts:
@baz7621authorMar 09.2004 — Hiya i havent done it in javascript just in english btw i want 2 use the DOM based model if thats any help

1

If onclick cell = cell id 2 & free_cell = cell id 1 swap

If onclick cell = cell id 5 & free_cell = cell id 1 swap

2

If onclick cell = cell id 1 & free_cell = cell id 2 swap

If onclick cell = cell id 6 & free_cell = cell id 2 swap

If onclick cell = cell id 3 & free_cell = cell id 2 swap

3

If onclick cell = cell id 2 & free_cell = cell id 3 swap

If onclick cell = cell id 7 & free_cell = cell id 3 swap

If onclick cell = cell id 4 & free_cell = cell id 3 swap

4

If onclick cell = cell id 3 & free_cell = cell id 4 swap

If onclick cell = cell id 8 & free_cell = cell id 4 swap

5

If onclick cell = cell id 1 & free_cell = cell id 5 swap

If onclick cell = cell id 6 & free_cell = cell id 5 swap

If onclick cell = cell id 9 & free_cell = cell id 5 swap

6

If onclick cell = cell id 5 & free_cell = cell id 6 swap

If onclick cell = cell id 2 & free_cell = cell id 6 swap

If onclick cell = cell id 7 & free_cell = cell id 6 swap

If onclick cell = cell id 10 & free_cell = cell id 6 swap

7

If onclick cell = cell id 6 & free_cell = cell id 7 swap

If onclick cell = cell id 3 & free_cell = cell id 7 swap

If onclick cell = cell id 8 & free_cell = cell id 7 swap

If onclick cell = cell id 11 & free_cell = cell id 7 swap

8

If onclick cell = cell id 7 & free_cell = cell id 8 swap

If onclick cell = cell id 4 & free_cell = cell id 8 swap

If onclick cell = cell id 12 & free_cell = cell id 8 swap

9

If onclick cell = cell id 5 & free_cell = cell id 9 swap

If onclick cell = cell id 10 & free_cell = cell id 9 swap

If onclick cell = cell id 13 & free_cell = cell id 9 swap

10

If onclick cell = cell id 9 & free_cell = cell id 10 swap

If onclick cell = cell id 6 & free_cell = cell id 10 swap

If onclick cell = cell id 11 & free_cell = cell id 10 swap

If onclick cell = cell id 14 & free_cell = cell id 10 swap

11

If onclick cell = cell id 10 & free_cell = cell id 11 swap

If onclick cell = cell id 7 & free_cell = cell id 11 swap

If onclick cell = cell id 12 & free_cell = cell id 11 swap

If onclick cell = cell id 15 & free_cell = cell id 11 swap

12

If onclick cell = cell id 11 & free_cell = cell id 12 swap

If onclick cell = cell id 8 & free_cell = cell id 12 swap

If onclick cell = cell id 16 & free_cell = cell id 12 swap


13

If onclick cell = cell id 9 & free_cell = cell id 13 swap

If onclick cell = cell id 14 & free_cell = cell id 13 swap

14

If onclick cell = cell id 13 & free_cell = cell id 14 swap

If onclick cell = cell id 10 & free_cell = cell id 14 swap

If onclick cell = cell id 15 & free_cell = cell id 14 swap

15

If onclick cell = cell id 14 & free_cell = cell id 15 swap

If onclick cell = cell id 11 & free_cell = cell id 15 swap

If onclick cell = cell id 16 & free_cell = cell id 15 swap

16

If onclick cell = cell id 12 & free_cell = cell id 16 swap

If onclick cell = cell id 15 & free_cell = cell id 16 swap
Copy linkTweet thisAlerts:
@steelersfan88Mar 09.2004 — well, maybe when you click, it will loop through the images to find the blank one. Then maybe stored in an array would be possible pictures to click that could put you there. Then it would loop through the array that the blank image was found on and if the image clicked is found, the picture would change to blank and the one already found as blank will change to whatever, otherwise, do nothing.

i could try a code for you if you need help and send it to you!
Copy linkTweet thisAlerts:
@baz7621authorMar 09.2004 — plz mate that wud help alot
Copy linkTweet thisAlerts:
@steelersfan88Mar 09.2004 — here is a basic game i just designed to fit the criteria i think you need. Of course, you have to change the source of the pictures. This one generates the image board in javascript:[code=php]<script type="text/javascript">

/* GAME SETUP SCRIPT */
var pics = new Array
pics[0] = "1.jpg"
pics[1] = "2.jpg"
pics[2] = "3.jpg"
pics[3] = "4.jpg"
pics[4] = "5.jpg"
pics[5] = "6.jpg"
pics[6] = "7.jpg"
pics[7] = "8.jpg"
pics[8] = "9.jpg"
pics[9] = "10.jpg"
pics[10] = "11.jpg"
pics[11] = "12.jpg"
pics[12] = "13.jpg"
pics[13] = "14.jpg"
pics[14] = "15.jpg"
pics[15] = "blank.jpg" // source of blank image

/* GENERATE IMAGE BOARD */
for(var i=0;i<pics.length;i++) {
document.write('<img name="pic'+ (i - -1) +'" id="pic'+ (i - -1) +'" src="blank.jpg" height="50" width="50" onclick="moveImg('+ i +')">')
if((i - -1) % 4 == 0) {
document.write("<BR>")
}
}

document.write('<BR><BR><span id="moves">Moves: 0</span>')

var newPic = ""
var tempPics = new Array
var numMoves = 0

for(var i=0;i<pics.length;i++) {
newPic = pics[Math.floor(Math.random()*pics.length)]
tempPics[i] = newPic
for(var j=0;j<tempPics.length - 1;j++) {
if(newPic == tempPics[j]) {
newPic = pics[Math.floor(Math.random()*pics.length)]
tempPics[i] = newPic
j = -1
}
}
document.getElementById('pic'+ (i- -1)).src = newPic
}

/* IMAGE MOVEMENT */
function moveImg(image) {
var ok = new Array
ok[0] = "1,4"
ok[1] = "0,5,2"
ok[2] = "1,6,3"
ok[3] = "2,7"
ok[4] = "0,8,5"
ok[5] = "4,1,9,6"
ok[6] = "5,7,2,10"
ok[7] = "3,11,6"
ok[8] = "4,12,9"
ok[9] = "8,10,13,5"
ok[10] = "9,11,14,6"
ok[11] = "10,7,15"
ok[12] = "8,13"
ok[13] = "12,14,9"
ok[14] = "13,15,10"
ok[15] = "14,11"

for(var i=0;i<ok.length;i++) {
ok[i] = ok[i].split(',')
}

var theSrc = ""
var toMove = -1

if(window.event.ctrlKey) {
toMove = parseInt(prompt('With what space do you want to switch? (1-16)','') - 1)
ok[image][toMove] = toMove
}
else {
for(var i=0;i<ok[image].length;i++) {
toMove = -1
theSrc = document.getElementById('pic'+ (ok[image][i] - -1)).src
theSrc = theSrc.substr(theSrc.length - 6,theSrc.length)
if(theSrc == pics[15].substr(pics[15].length - 6,pics[15].length)) {
toMove = i
break;
}
}
}

if(toMove >= 0) {
var backUp = document.getElementById('pic'+ (ok[image][toMove] - -1)).src
document.getElementById('pic'+ (ok[image][toMove] - -1)).src = document.getElementById('pic'+ (image - -1)).src
document.getElementById('pic'+ (image - -1)).src = backUp
numMoves += 1
moves.innerHTML = "Moves: "+ numMoves
}
var conDit = ""
var ok = "1"

for(var i=0;i<pics.length;i++) {
temp = document.getElementById('pic'+ (i - -1)).src
temp = temp.substr(temp.length - 6,temp.length)
equ = i - -1
if(equ < 10) {
equ = "/"+ (i - -1)
}
conDit = "temp == equ + pics[i].substr(pics[i].length - 4,pics[i].length)"

if(!(eval(conDit)) && temp == pics[i].substr(pics[i].length - 6,pics[i].length)) {
conDit = "temp == pics[i].substr(pics[i].length - 6,pics[i].length)"
}
if(!eval(conDit)) {
ok = "0"
break;
}
}

if(ok == 1) {
youWin()
}
}

/* YOU WON! */
function youWin() {
alert("You win!")
}

</script>[/code]
Note, if you hold the control key when playing, it will allow you to choose the space to switch it with (just to make sure one can win the game). You can change the line to if(1 == 2) as opposed to the current line to completely ignore it!
Copy linkTweet thisAlerts:
@David_HarrisonMar 10.2004 — Although I am sure that steelersfan88's script is super-duper wonderful brill, it doesn't look very user friendly.

First off, numbering the images 0-15 doesn't help, number them in base 4:

00 01 02 03

10 11 12 13

20 21 22 23

30 31 32 33

I made a Reversi game and I had to check for something simlar on an 8x8 board, using a different number base made creating the script much easier since the patterns became more apparent. I also used a two-dimenstional array to store the data in.

I have created some simpler code that does all the checking and moving (a little bit more then you said you wanted but they are integrated quite well). All you have to do is fill in some image sources in the array at the start of the script, and the board will actually look the same as the array does.

[upl-file uuid=1c7101ce-e94a-4b0b-b18f-465ac3043124 size=4kB]game.txt[/upl-file]
Copy linkTweet thisAlerts:
@steelersfan88Mar 10.2004 — i just decided since i would loop for each picture over and over again, it would be easier to get the id of the element that way.

What do you mean when you say user friendly? (The script was just designed so i did not add any features like rest, which could simply be done by either reloading the page or rerunning the random number generator)

I had difficulty with your script, the images didn't swap at first, but it might be because I decided not to change them all from blank.jpg. Now we can just wait for baz7621 to reply to see what he likes (I'm going to guess mine for the sole fact that it requires copy and paste, no download, but thats probably it -- don't think he will care much about picture numbering.)
Copy linkTweet thisAlerts:
@baz7621authorMar 10.2004 — well i dont want any fighting now but i have used the first solution
Copy linkTweet thisAlerts:
@steelersfan88Mar 10.2004 — woohoo!! We rarely anymore fight (no longer like the old days) and while competitive, get along very well!
Copy linkTweet thisAlerts:
@David_HarrisonMar 11.2004 — [i]Originally posted by baz7621[/i]

[B]well i dont want any fighting now[/B][/QUOTE]
No-one wants that.

[i]Originally posted by baz7621[/i]

[B]i have used the first solution [/B][/QUOTE]
I expected that you would but it's always nice to have options open.

[i]Originally posted by steelersfan88[/i]

[B]...and while competitive...[/b][/quote]
Can't argue with you there, but I try not to step on anyones toes (like I think I've done here :eek: ). ?
Copy linkTweet thisAlerts:
@AntifaithDec 11.2008 — Hello - i know it's been a long time since any of you must have looked at this thread but i could do with a helping hand - i've been making a fifteen puzzle myself just for fun and i have been looking at both of your javascript examples, the one i decided to play with is David Harrisons one as i like how simple the code was.

i was just wondering if there is any way to implement a shuffle squares, and a reset button into this code, i've been trying with form buttons for hours now and basic examples such as

[code=php]function sortfunction(a, b){
return (a - b)
}[/code]


to no avail at all, any help would be much appreciated - and a cure for my ever increasing headache!!!!!?!?!?!
Copy linkTweet thisAlerts:
@JMRKERDec 12.2008 — Just noticed that this thread has been continued at:

http://www.webdeveloper.com/forum/showthread.php?t=197572
×

Success!

Help @baz7621 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...