In: Computer Science
Follow these steps to implement the following browser-based
puzzle game:(Javascript/html/css)
1. Get a photo of yourself and save it as an image file
2. Use a image-splitting program such as splitter.imageonline.co to
break the image into 9 roughly equal parts (3 x 3). Save those
files in a directory
3. Write Javascript that takes these nine images and randomly
rearranges them in a 3 x 3 grid.
4. Each cell in the grid will also have a checkbox.
5. At each turn, the user will click two checkboxes, press a Swap
button, and the program will swap the two images that were
checked.
6. Play continues until all the sub-images are in their correct
positions.
7. The program will state how many turns (swaps) it took to solve
the puzzle.
8. Use CSS to make your puzzle game aesthetically (stylistically)
pleasing
9. Upload your game (and the photo pieces) to your website.
10. Provide the URL of your puzzle game where it can be tested.
<html>
<title> Image puzzle </title>
<center><head> Welcome to image puzzle
game </head></center>
</br>
</br>
<script>
function puzzle(){
var check =
document.getElementsByName('check');
var i = 0,j =
0;
for(i = 0;i <
check.length; i++){
if(check[i].checked)
j++;
}
if(j == 2
){
var index = new Array();
var table_rows = new Array();
for(i = 0;i < check.length; i++){
if(check[i].checked){
if(i <
3){
var r = 0;
table_rows.push(r);
if(i == 0){
r = 0;
index.push(r);}
else if(i == 1){
r = 1;
index.push(r);}
else if(i == 2){
r = 2;
index.push(r);}
}
else if(i
> 2 && i < 6){
var r = 1;
table_rows.push(r);
if(i == 3){
r = 0;
index.push(r);}
else if(i == 4){
r = 1;
index.push(r);}
else if(i == 5){
r = 2;
index.push(r);}
}
else if(i
> 5 && i < 9){
var r = 2;
table_rows.push(r);
if(i == 6){
r = 0;
index.push(r);}
else if(i == 7){
r = 1;
index.push(r);}
else if(i == 8){
r = 2;
index.push(r);}
}
}
}
alert("Selected rows = "+table_rows +"\nindexes
= "+index);
var tab =
document.getElementById('puzzle');
var temp =
tab.rows[table_rows[0]].cells[index[0]].innerHTML;
tab.rows[table_rows[0]].cell[index[0]].innerHTML =
tab.rows[table_rows[1]].cell[index[1]].innerHTML;
tab.rows[table_rows[1]].cell[index[1]].innerHTML = temp;
}
else if(j <
2){
alert("Please select two images to swap");
}
else{
alert("Please select two images only to
swap");
}
}
</script>
<body>
<table id="game" align =
"center">
<tr>
<td>
<b
style="color:blue">Original image</b>
<div id = "origianlimage"
>
<img
src = "/labrador.jpg" width = "250" height = "250"/>
</div>
</td>
<td>
<b
style="color:green">Puzzled Image</b>
<div id =
"puzzledimage">
<table
id="puzzle">
<?php
$count = 0;
$numbers=range(1,9);
shuffle($numbers);
for($i = 0; $i < 3;
$i++){
?>
<tr>
<?php
for($j =
0; $j < 3; $j++){
?>
<td id = "<?php echo 'td'.($count+1);
?>">
<input type = "checkbox"
name = "check" id = "<?php echo 'ch'.($j);?>"/>
<img
id =
"<?php echo 'img'.($count+1);?>"
src =
"<?php echo '/'.($numbers[$count]).'.jpg';?>" height="100"
width="100" />
</td>
<?php
$count++;
}
?>
</tr>
<?php
}
?>
</table>
</div>
</td>
</tr>
<tr>
<td></td>
<td>
<input type = "submit"
value = "Swap" id = "swap" name = "swap" onclick = "return
puzzle();"/>
</td>
</tr>
</table>
</body>
</html>