Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

12.8 (15 Puzzle) Write a web page that enables the user to play the game of 15.

ID: 3567202 • Letter: 1

Question

12.8 (15 Puzzle) Write a web page that enables the user to play the game of 15. There is a 4-by-4 board (implemented as an XHTML table) for a total of 16 slots. One of the slots is empty. The other slots are occupied by 15 tiles, randomly numbered from 1 through 15. Any tile next to the currently empty slot can be moved into the currently empty slot by clicking on the tile. Your program should create the board with the tiles out of order. The user's goal is to arrange the tiles in sequencial order row by row. Using the DOM and the onclick event, write a script that allows the user to swap the positions of the open position and an adjacent tile. [Hint: The onclick event should be specified for each table cell.]

12.9 Modify your solution to Exercise 12.8 to determine when the game is over, then prompt the user to determine whether to play again. If so, scramble the numbers using the Math.random method.

12.10 Modify your solution to Exercise 12.9 to use an image that is split into 16 equally sized pieces. Discard one of the pieces and randomly place the other 15 pieces in the XHTML table.

Explanation / Answer

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>puzzle</title>
<script type="text/javascript">
<!--
function puzzle()
{
for(var i = 0; i < 500; i++)
{
var moves = new Array(); // The set of valid moves
var pos = 0; // The number of candidate valid moves
/* Find blank */
for(var blankpos = 0; blankpos< 16; blankpos++)
{
if(slot(blankpos).innerText == "0")
{
/* Find moves */
if((blankpos % 4) > 0)
{
moves[pos++] = blankpos - 1; // Left
}
if((blankpos % 4) < 3)
{
moves[pos++] = blankpos + 1; // Right
}
if(blankpos > 3)
{
moves[pos++] = blankpos - 4; // Up
}
if(blankpos < 12)
{
moves[pos++] = blankpos + 4; // Down
}
}
}
/* Move random candidate */
moveSlot(moves[(Math.floor(Math.random() * moves.length))]);
}
}
function moveSlot(pos)
{
var move = -1; // The cell to which to move
if((pos % 4) > 0 && slot(pos - 1).innerText == "0")
{
move = pos - 1; // Left
}
if((pos % 4) < 3 && slot(pos + 1).innerText == "0")
{
move = pos + 1; // Right
}
if(pos > 3 && slot(pos - 4).innerText == "0")
{
move = pos - 4; // Up
}
if(pos < 12 && slot(pos + 4).innerText == "0")
{
move = pos + 4; // Down
}
if(move > -1)
{
slot(move).innerText = slot(pos).innerText;
slot(move).className = slot(pos).className;
slot(pos).innerText = "0";
slot(pos).className = "blankpos";
}
}
// -->
</script>
<style type="text/css">
b
{
font-size:100;
display:inline;
height:60;
width:60;
}
.blankpos
{
display:none;
}
</style>
<style type="text/css">
p.c2
{
text-align:center;
}
button.c1
{
font-size:20;
width:160;
}
.style1 {
color: #000066;
font-weight: bold;
}
</style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">&nbsp;</td>
<h1>Puzzle</h1>
<hr />

PART 2:

<table border="1" align="center" bgcolor="pink">
<center>
<tr>
<td><button id="slot" class="b">01 </button></td>
<td><button id="slot" class="b">02</button></td>
<td><button id="slot" class="b">03</button></td>
<td><button id="slot" class="b">04</button></td>
</tr>
</center>
<center>
<tr>
<td><button id="slot" class="b">05</button></td>
<td><button id="slot" class="b">06</button></td>
<td><button id="slot" class="b">07</button></td>
<td><button id="slot" class="b">08</button></td>
</tr>
</center>
<center>
<tr>
<td><button id="slot" class="b">09</button></td>
<td><button id="slot" class="b">10</button></td>
<td><button id="slot" class="b">11</button></td>
<td><button id="slot" class="b">12</button></td>
</tr>
</center>
<center>
<tr>
<td><button id="slot" class="b">13</button></td>
<td><button id="slot" class="b">14</button></td>
<td><button id="slot" class="b">15</button></td>
<td><button id="slot" class="blankpos">0</button></td>
</tr>
</center>
</table>
<p class="c2"><button class="c1">New Game</button></p>
</td>
</tr>
</table>

</body>
</html>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote