-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboggle.js
216 lines (191 loc) · 6.44 KB
/
boggle.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
function BStone(sides) {
this.sides = sides;
this.location;
this.init();
}
BStone.prototype = {
init: function() {
console.log("New stone!");
$.each(this.sides, function(id, side) { console.log("Stone has: "+side)});
},
set_location: function(x,y) {
console.log("Stone location: x: "+x+" y: "+y+"");
this.location = {x: x, y: y};
},
show: function(element) {
var container_height = 500;
var stone_size = container_height/4 - 5;
var x_position = (stone_size + 5) * this.location.x;
var y_position = (stone_size + 5) * this.location.y;
var letter = this.sides[Math.floor(Math.random()*6)];
var rotation = Math.floor((Math.random()*3)+0) * 90 + (Math.floor((Math.random()*3)+0) - 1.5);
var html = "<div class='boggle_stone' id='"+this.location.x+"-"+this.location.y+"' style='top:"+y_position+"px;left:"+x_position+"px; width: "+stone_size+"px; height:"+stone_size+"px;display:block;-webkit-transform:rotate("+rotation+"deg)'>"+letter+"</div>";
console.log("drawing:"+html+" on element:"+element);
$(element).append(html);
}
}
BStone.prototype.constructor = BStone;
function BStoneManager(stones) {
this.stones = stones;
}
BStoneManager.prototype = {
show_stones: function(element) {
this.stones.sort(function() { return 0.5 - Math.random() });
console.log("Showing stones!");
$(element).html("");
var grid_width = 4;
var grid_height = 4;
var stone_number = 0 ;
for(var x = 0; x<grid_width; x++) {
for(var y = 0; y<grid_height; y++) {
this.stones[stone_number].set_location(x, y);
stone_number++;
}
}
$.each(this.stones, function(id, stone) {
stone.show(element);
});
}
}
BStoneManager.prototype.constructor = BStoneManager;
function BBoard(stonemanager, timer) {
this.timer = timer;
this.stone_manager = stonemanager;
this.init();
}
BBoard.prototype = {
init: function() {
console.log("Board initialized.");
jQuery('#start_boggle_button').bind('click',{context: this} , this.start);
},
start: function(event) {
var self = event.data.context;
self.stone_manager.show_stones("#stone_container");
self.timer.start("#stone_container");
console.log("Start boggle!");
}
}
BBoard.prototype.constructor = BBoard;
BTimer = function(container, countdown_container, start_time, countdown_time) {
this.container = container;
this.countdown_container = countdown_container;
this.start_time = start_time;
this.countdown_time = countdown_time;
this.time;
this.timeout;
}
BTimer.prototype = {
start: function() {
console.log("Timer started at "+this.start_time+".");
$(this.container).html("Get ready!");
this.time = this.countdown_time;
var self = this;
clearTimeout(this.timeout);
this.count_countdown();
$("#stone_container").css("opacity", "0");
$(this.countdown_container).css("display", "block");
$(this.countdown_container).mouseover(function() {});
$(this.countdown_container).mouseout(function() {});
this.initialized = 1;
},
make_minutes: function(total_seconds) {
var minutes = Math.floor(total_seconds/60);
var seconds = total_seconds - (minutes*60);
if(seconds <10) {
seconds = "0"+seconds;
}
return {minutes: minutes, seconds: seconds};
},
count_main: function() {
this.show_time();
if(this.time > 0) {
this.time--;
var self = this;
this.timeout = setTimeout(function() { self.count_main(); }, 1000);
}
else {
console.log("animate");
$(this.countdown_container).css("display", "block");
$("#stone_container").css("opacity", "0");
$(this.countdown_container).animate({
opacity: 1,
}, 250, function() {
// Animation complete.
});
self = this;
$(this.countdown_container).mouseover(function() {
$(self.countdown_container).css("opacity", "0");
$("#stone_container").css("opacity", "1");
});
$(this.countdown_container).mouseout(function() {
$(self.countdown_container).css("opacity", "1");
$("#stone_container").css("opacity", "0");
});
$(self.countdown_container).html("Stop writing!");
$(self.container).html("");
}
},
count_countdown: function () {
this.show_countdown_time();
console.log("show time:"+this.time);
if(this.time > 0) {
this.time--;
var self = this;
this.timeout = setTimeout(function() { self.count_countdown(); }, 1000);
}
else {
var self = this;
$(this.countdown_container).animate({
opacity: 0,
}, 1000, function() {
$(self.countdown_container).css("display", "none");
// Animation complete.
});
$("#stone_container").animate({
opacity: 1,
}, 1000, function() {
// Animation complete.
});
this.time = this.start_time;
this.count_main();
}
},
show_countdown_time: function() {
if(this.time <1) {
show_time = "";
}
else {
show_time = this.time;
}
$(this.countdown_container).html(show_time);
},
show_time: function() {
current_time = this.make_minutes(this.time);
$(this.container).html(current_time.minutes+":"+current_time.seconds);
}
}
BTimer.prototype.constructor = BTimer;
$(document).ready(function() {
console.log("Document ready.");
var stones = new Array(
new BStone(new Array("N","S","T","E","D","O")),
new BStone(new Array("P","M","E","A","D","C")),
new BStone(new Array("O","B","Q","J","M","D")),
new BStone(new Array("A","R","M","S","I","I")),
new BStone(new Array("R","G","L","U","W","E")),
new BStone(new Array("G","U","Y","E","J","N")),
new BStone(new Array("N","T","U","E","P","L")),
new BStone(new Array("N","L","I","B","T","A")),
new BStone(new Array("E","F","H","S","E","I")),
new BStone(new Array("H","S","R","N","E","I")),
new BStone(new Array("K","X","I","R","F","A")),
new BStone(new Array("A","D","V","N","Z","E")),
new BStone(new Array("V","N","T","I","E","G")),
new BStone(new Array("R","A","E","S","H","C")),
new BStone(new Array("A","O","W","I","A","E")),
new BStone(new Array("N","E","T","Z","K","O"))
);
var stone_manager = new BStoneManager(stones);
var timer = new BTimer("#timer_container","#countdown_container", 180, 3);
var board = new BBoard(stone_manager, timer);
});