-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.js
116 lines (95 loc) · 3.33 KB
/
memory.js
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
window.onload= init;
function init()
{
var images= document.getElementsByTagName("img");
for(var i=0; i<images.length; i++)
{
images[i].onclick= handleClick;
}
model.generateImages();
}
function handleClick(e)
{
var image=e.target;
var id=image.id;
for(var i=0; i<model.boardsize;i++)
{
if(id == model.hiddenlocations[i].loc)
{
image.src=model.hiddenlocations[i].src;
controller.isSameImage(image,controller.clicks);
}
}
}
var controller={
url:[],
clicks:1,
image1:{},
image2:{},
isSameImage: function(image,clicks){
if(clicks==1)
{
this.image1=image;
this.url[clicks]=image.src;
this.clicks++;
}
if(clicks==2)
{
this.image2=image;
this.url[clicks]=image.src;
if(this.url[1]!=this.url[2])
{setTimeout(this.facetile,2000,this.image1,this.image2);}
this.clicks=1;
}
},
facetile:function(image1,image2)
{
image1.src="twitterBird.png";
image2.src="twitterBird.png";
}
};
var model={
boardsize:16,
urls:[],
locations:[],
hiddenlocations:[],
generateImages: function(){ console.log("enter");
for(var i=1; i<=(this.boardsize/2); i++)
{
var code = i;
this.urls.push(code);
this.urls.push(code);
}
this.checkLocation();
},
generateLocation: function(){
var row= Math.floor(Math.random()*4);
var column= Math.floor(Math.random()*4);
var newlocation= row + "" + column;
return newlocation;
},
checkLocation: function(){
var location;
for(var i=0; i<this.boardsize; i++)
{
do{
location=this.generateLocation();
}while(this.collision(location));
this.locations[i]=location;
}
this.assignUrl();
},
collision: function(location){
if(this.locations.indexOf(location)>=0)
{
return true;
}
return false;
},
assignUrl: function(){
for(var i=0;i<this.boardsize;i++)
{
this.hiddenlocations.push({loc:this.locations[i],src:"images/fruit-"+this.urls[i]+".jpeg"});
}
}
};