/    Sign up×
Community /Pin to ProfileBookmark

Javascript Puzzle Game – Help Pse!

Hi!

Right – I’m trying to make a 16-piece puzzle where if you click on an image in a table next to the blank image, the blank image swaps with the image that you originally clicked on. Its like [URL=http://www.webreference.com/js/column45/puzzle.html]THIS[/URL] but in my own code.

Here’s what I have so far:-

<?xml version=”1.0″ encoding = “UTF-8”?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd“>

<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>

<head>

<title> Puzzle </title>

<script type=”text/javascript”>

function checkCells()
{

var cellId = event.srcElement.id;
var clickedCell = document.getElementById(cellId);
var blankCell = document.getElementById(“blank”);

if (blankCell -1 == clickedCell)
{
swapCells();
}
else if (blankCell -4 == clickedCell)
{
swapCells();
}
else if (clickedCell – 1 == blankCell)
{
swapCells();
}
else if (clickedCell -4 == blankCell)
{
swapCells();
}
else invalid();

}

function invalid()
{
alert(“Cell CANNOT move”);

}

function swapCells()
{

temp = clickedCell;
clickedCell = blankCell;
blankCell = temp;

}

</script>

</head>

<body>

<p>Puzzle Game</p>

<table border = “2”>

<tr>
<td id=”1″ onclick=”checkCells()”><img src=”1.gif”></td>
<td id=”2″ onclick=”checkCells()”><img src=”2.gif”></td>
<td id=”3″ onclick=”checkCells()”><img src=”3.gif”></td>
<td id=”4″ onclick=”checkCells()”><img src=”4.gif”></td>
</tr>

<tr>
<td id=”5″ onclick=”checkCells()”><img src=”5.gif”></td>
<td id=”6″ onclick=”checkCells()”><img src=”6.gif”></td>
<td id=”7″ onclick=”checkCells()”><img src=”7.gif”></td>
<td id=”8″ onclick=”checkCells()”><img src=”8.gif”></td>
</tr>

<tr>
<td id=”9″ onclick=”checkCells()”><img src=”9.gif”></td>
<td id=”10″ onclick=”checkCells()”><img src=”10.gif”></td>
<td id=”11″ onclick=”checkCells()”><img src=”11.gif”></td>
<td id=”12″ onclick=”checkCells()”><img src=”12.gif”></td>
</tr>

<tr>
<td id=”13″ onclick=”checkCells()”><img src=”13.gif”></td>
<td id=”14″ onclick=”checkCells()”><img src=”14.gif”></td>
<td id=”15″ onclick=”checkCells()”><img src=”15.gif”></td>
<td id=”blank”><img src=”blank.gif”></td>
</tr>

</table>

</body>
</html>

I want to use things like “document.srcElemement” and all that sort of stuff to point at the cell in question (be it the blank cell or the cell that was clicked on) and to grab its ID and then swap them by using a temporary variable to store one of the values until its overwritten.

So when you click on an image in the table, it calls the function “checkcells()” which then checks if the cell next to it is free, and if so calls function “swapCells()” which does the actual moving. But it doesnt work.

What am I doing wrong?

Hope someone can help.
Cheers! ?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Mar 10.2004 — define the three vars below outside of the function as shown:[code=php]var cellId;
var clickedCell;
var blankCell;[/code]
and then when redefining them, take out the var lines.

You could also pass the data of these cells to your function, but making the variables global should do it.
×

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.14,
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,
)...