Pasted as C++ [Remove this paste ]
Description: No description
URL: http://davr.org/p/2So7TJ13.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*Doesnt look like im tracking which images are set at each tile
*for if tile 3 has image 7 in it, its not being tracked.
*so when someone clicks on tile 3 were trying to put that
image in the blank square, we dont know what image to put there
*/
 
/*Naruto Images*/
/***********************************************************************
				My Images Array
************************************************************************/
	  var narutoImages=new Array("images/blank.jpg", 
	  "images/naruto-top-middle.jpg",
	  "images/naruto-top-right.jpg",
	  "images/naruto-middle-left.jpg",
	  "images/naruto-middle-middle.jpg",
	  "images/naruto-middle-right.jpg",
	  "images/naruto-bottom-left.jpg",
	  "images/naruto-bottom-middle.jpg",
	  "images/naruto-bottom-right.jpg");
 
 
	  var openSpace=0;
	  var order=new Array(0,1,2,3,4,5,6,7,8); //need an array if i want to keep track of where openspace is
	  var initFlag=0;/*create a var to tell us if the initialization is done*/
		var board;
 
 
 
 
 
 
function start(){
	alert("Maskanios Jigsaw Puzzle, match the images with the one on the right!");
		 }
 
 
/*************************************************************************
this function checks which slot they have clicked to see if it is one that
can be swapped with the open space
*************************************************************************/
function move(ndx)
{
	if(openSpace==ndx){
	return false;
	}
 
	if  ((openSpace==0 &&(ndx==3||ndx==1))||
 
		(openSpace==1 &&(ndx==0||ndx==2||ndx==4))||
 
		(openSpace==2 &&(ndx==1||ndx==5))||
 
		(openSpace==3 &&(ndx==0||ndx==4||ndx==6))||
 
		(openSpace==4 &&(ndx==3||ndx==1||ndx==5||ndx==7))||
 
		(openSpace==5 &&(ndx==2||ndx==4||ndx==8))||
 
		(openSpace==6 &&(ndx==3||ndx==7))||
 
		(openSpace==7 &&(ndx==4||ndx==6||ndx==8))||
 
		(openSpace==8 &&(ndx==5||ndx==7))){
 
	swap(ndx);
	checkOrder();
 
	return false;
	}
}
 
 
/*************************************************************************
This function swaps the open space with the space pointed to by the argument
*************************************************************************/
function swap(dex)
{	
 
	 temp=order[openSpace];
	 order[openSpace]=order[dex];
	 order[dex]=temp;
	 openSpaceSource=document.getElementById(openSpace);
	 openSpaceSource.setAttribute('src',(narutoImages[dex])); 
	 dexSource=document.getElementById(dex);
	 dexSource.setAttribute('src',('images/blank.jpg'))
 
	 checkOrder();	
 
 
    /*swap the order array elements*/
    /*swap out the sources in the html elements for these boxes*/
    openSpace=dex;/*update the openSpace variable*/
}
 
 
/*************************************************************************
This function checks to see if the order array is in the order 0,1,2,3,4,5,6,7,8
If so, the puzzle is solved
*************************************************************************/
function checkOrder()
{
    var i;
 
 
    for(i=0; i<=8; i++){ /*if initialization is not finished, return false
      this keeps it from being solved while it is being randomized*/
 
	return false;/* If the value at spot order[i] is not the same as i, return false
      because the puzzle is not solved yet*/
 
	alert("You complete the puzzle, great job!");/*if we have not returned, the puzzle is solved
      display an alert to let the user know*/
   }
}
 
 
 
 
/*************************************************************************
Shuffle the tiles here
*************************************************************************/
function setup(){
 
    var randomNum;
    var i;
    var gameboard;
	 var j;
    board=document.getElementById('gameboard');		
     for(var i=0;i<1000;i++)
    {
      randomNum=Math.floor((Math.random() * 9));
		move(randomNum);
    }
 
 
 
	initflag=true;
}
 
 
 
function addLoadEvent(func) 
{
  var oldonload = window.onload;
 
  if (typeof window.onload != 'function') 
    {
      window.onload = func;
    } 
  else 
    {
      window.onload = function() 
	{
	  oldonload();
	  func();
	}
    }
}
 
addLoadEvent(setup);
addLoadEvent(start);