-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
391 lines (305 loc) · 11.3 KB
/
game.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
var board; // הגדרת הערך שבהמשך יהיה מטריצה ללוח הסודוקו
function strgame() {
{
document.body.innerHTML = getTableString(9);//קריאה ליצרת טבלת משחק
$("input").on("focusout", changeNum);// מצמיד לכול הinput את האירועה
$("button").on("click", evntlis); // מצימיד ארועה למקשי המשחק
$(".b_show").on("mouseover", ShowNum);//מצמיד את האירועה של הצגת המספרים בטבלה
$(".ckgame").on("click",checIfWin);//מצימד אירועה של בדיקת ניצחון
//הוספת קווים הדגשה ליצירת טבלאות 3*3
var i = 0;
for (i = 2 ; i < 9; i++) {
for (var j = 0; j < 9; j++) {
if (!(i == 8))
$('#input-' + i + "-" + j).css("border-bottom-color", "black").css("border-bottom-width", "10px");
}
i = i + 2;
}
for (i = 3 ; i < 9; i++) {
for (var j = 0; j < 9; j++) {
$('#input-' + j + "-" + i).css("border-left-color", "black").css("border-left-width", "10px");
}
i = i + 2;
}
}
}
//בחירת רמה של המשחק מוחק את הדף ויוצר דף חדש
function evntlis() {
//מוחק את דף
document.body.innerHTML = "";
var testgame = $(this).attr("id").toString();// תופס את הid
if (testgame == "btn_e") {
setLevel(20);
}
if (testgame == "btn_m") {
setLevel(40);
}
if (testgame == "btn_h") {
setLevel(60);
}
strgame();
StopClock()
StartClockGame()
$('#movCalc').on('click', function () {
window.location.href = "../clac/index.html";
})
$('#movForm').on('click', function () {
window.location.href = "../Form/index.html";
})
}
function setLevel(level)//קובע את הרמה של המשחק
{
var r = 0,i = 0, c = 0;
var min = 1, max = 3;
//עיגול כלפי מטה
rnd = Math.floor(Math.random() * max) + min;
Create_Table(rnd)//בחירת לוח משחק
while (i < level) {
r = Math.floor(Math.random() * 9);
c = Math.floor(Math.random() * 9);
board[r][c] = "";
i++;
}
}
//יצירת טבלה וכפתורים
function getTableString(dim) {
debugger;
var retString ='<div id="the_name">'+"project javascrupt Meir Moshe Linoy"+'</div>'//כפתור מעבר בין דפים
//כפתורים של רמת קושי
+'<div class="btns">' + '<button id="btn_e" value="' + 1 + '">' + "Easy" + '</button>' + '<br><br>' +
'<button id="btn_m" value="' + 2 + '">' + "Medium" + '</button>' + '<br><br>' + '<button id="btn_h" value="' + 3 + '">' + "Hard" + '</button>' + '<br><br>' +
'<label id="clock"> ' + "00" + ':' + "00" + '<br>' + '</label>'+'</div>' +//תצוגת שעון
'<div id="gameBoard">' + '<br>';//מסגרת למשחק
//כפתורים שנוצרים בלולאה כפתורי עזר
for (var num = 1; num < 10; num++)
retString += '<input type="button"class="b_show" value="' + num + '"></input>';
//כפתור של בדיקה אם המשחק נגמר
retString += '<input type="button"class="ckgame" value="' + "check" + '"></input>';
//יצירת טבלת משחק
retString += '<table>'
for (var i = 0; i < dim; i++) {
retString += '<tr>' //בנוסף למה שהיה תוסיף את זה
for (var j = 0; j < dim; j++)
if (board[i][j] == '') // הכנסת אחת המטריצות לתוך הטבלה לאחר שחלק ממנה נמחק
retString += '<td >' + '<input id="input-' + i + '-' + j + '" type="text" value="' + board[i][j] + '">' + '</input></td>';
else
retString += '<td >' + '<input id="input-' + i + '-' + j + '" type="text" value="' + board[i][j] + '" readonly class="read">' + '</input></td>';// בשחור הערך יהיה בתוך התא
retString += '</tr>'
}
retString += '</br>';
retString += '</table>'+'</div>'; //סוף הטבלה
return retString;//שליחת הטבלה לדף
/*****************************************************************/
}
//בחירה באחת מלוחות המשחק
function Create_Table(option) {
board1 = [[1,9,6,8,4,5,3,2,7],
[8,5,7,2,3,1,4,6,9],
[3,4,2,7,6,9,5,1,8],
[6,8,3,5,9,4,1,7,2],
[5,7,1,3,2,6,8,9,4],
[9,2,4,1,8,7,6,5,3],
[7,3,8,6,5,2,9,4,1],
[4,1,5,9,7,8,2,3,6],
[2,6,9,4,1,3,7,8,5]]
board2= [[5,3,4,6,7,8,9,1,2],
[6,7,2,1,9,5,3,4,8],
[1,9,8,3,4,2,5,6,7],
[8,5,9,7,6,1,4,2,3],
[4,2,6,8,5,3,7,9,1],
[7,1,3,9,2,4,8,5,6],
[9,6,1,5,3,7,2,8,4],
[2,8,7,4,1,9,6,3,5],
[3,4,5,2,8,6,1,7,9]]
board3= [[3,6,4,7,8,5,1,2,9],
[1,8,2,4,3,9,6,5,7],
[7,9,5,2,1,6,8,3,4],
[4,7,1,5,9,2,3,6,8],
[9,5,6,3,7,8,4,1,2],
[8,2,3,6,4,1,7,9,5],
[2,1,8,9,6,4,5,7,3],
[6,3,9,8,5,7,2,4,1],
[5,4,7,1,2,3,9,8,6]]
if(option==1)
board = board1;
else if(option==2)
board = board2;
else
board = board3;
}
/*****************************************************************/
// function changeNum() מחליפה מספר כאשר יוצאים מהתא
function changeNum() {
debugger;
var temp = $(this).attr("id").toString();// תופס את הid
temp = temp.split("-"); //example: 'input-0-0' becomes [input, 0, 0]-- מפרק את המחרוזת הזמנית ל ל=מבנה כזה
board[parseInt(temp[1])][parseInt(temp[2])] = $(this).val();//
Checking()
}
// בדיקה של חוקי הסודוקו function Checking()
function Checking() {
// בדיקה לרוחב
/********************************************************/
i = 0;
j = 0;
var my_test1 = 0
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
my_test1 = 0;
for (x = 0; x < 9; x++) {
if (board[i][j] == board[i][x]) {
my_test1++;
if (my_test1 > 1 && board[i][j] != "")
$('#input-' + i + "-" + j).css("background-color", "coral");
else
$('#input-' + i + "-" + j).css("background-color", "white");
}
}
}
}
// בדיקה לאורך
/************************************************************/
var my_test2 = 0
for (i = 0; i < 9; i++) {
for (j = 0; j < 9; j++) {
my_test2 = 0;
for (x = 0; x < 9; x++) {
if (board[j][i] == board[x][i]) {
my_test2++;
if (my_test2 > 1 && board[j][i] != "")
$('#input-' + j + "-" + i).css("background-color", "coral");
}
}
}
}
/************************************************************/
// בדיקה בתוך הקוביה הקטנה
testCube(0, 0);
}
/*******************תחלית בדיקת הטבלאות הקטנות **********************/
function testCube(r_i, c_i) //בדיקה לפי קופסא --> תחילה קופסא שמאלית עליונה
{
r = r_i;
c = c_i;
m = r_i;
my_test3 = 0;
for (r = r_i; r < r_i + 3; r++) {
for (c = c_i; c < c_i + 3; c++) {
my_test3 = 0;
for (m = r_i; m < r_i + 3; m++) {
for (x = c_i; x < c_i + 3; x++) {
if (board[r][c] == board[m][x])// צריך לבדוק שוב אם צריך
{
my_test3++;
if (my_test3 > 1 && board[r][c] != "")
$('#input-' + r + "-" + c).css("background-color", "coral");
}
}
}
}
}
if (c_i == 0) test1(r_i, c_i + 3);//בדיקה קוביה אמצעית עליונה
if (c_i == 3) test1(r_i, c_i + 3);//ימנית עליונה
if (c_i == 6 && r_i == 6) return 0; //כדי לא למנוע פעולה אין סופית של בדיקות
if (c_i == 6) test1(r_i + 3, c_i = 0);// בעזזרת רקורסיה בדקיה על שני שרות הקוביות
}
/*********************שעון למשחק *******************/
var myVar;
sic = 0;
minute = 0;
flag = 0;
function StartClockGame() {
if (flag == 1) return 0; //הגנה לשעון שהכפתור לא יריץ את הזמן יותר מדי מהר
flag = 1;
myVar = setInterval(function () { alertFunc() }, 1000);
}
function alertFunc() {
sic++;
sic = checkTime1(sic);
sic = checkTime(sic);
if (sic >= 59)
minute++;
document.getElementById("clock").innerHTML = "0" + minute + ":" + sic;
}
function StopClock() {
flag = 0;
clearInterval(myVar);
sic = 0;
minute = 0;
}
function StopClock2() {
clearInterval(myVar);
}
function checkTime(i) {
if (i < 10) { i = "0" + i }; // add zero in front of numbers < 10
return i;
}
function checkTime1(i) {
if (i > 59) { i = 0 }; // add zero in front of numbers < 10
return i;
}
/***********************סוף שעון ********************************/
/**********************************/
$(document).ready(function ()//זה רק העלה ראשונית
{
$('#btn_e').on('click', function () {
setLevel(20);
strgame(9);
StartClockGame()
});
$('#btn_m').on('click', function () {
setLevel(40);
strgame(9);
StartClockGame()
});
$('#btn_h').on('click', function () {
setLevel(60);
strgame(9);
StartClockGame()
});
});
function ShowNum() {
var number = $(this).val();
$('input[id^="input-"][value="' + number + '"]').css("background", "yellow");
var color = setTimeout(function () {
$('input[id^="input-"][value="' + number + '"]').css("background", "white");
}, 1000 * 1);
}
function checIfWin()
{
var max=0
var t_r=0
var t_c=0
for(t_r=0;t_r<9;t_r++)
{
for(t_c=0;t_c<9;t_c++)
{
var mxm=$('#input-' + t_r + "-" + t_c).css("background-color")
if (board[t_r][t_c] != "" && mxm!="rgb(255, 127, 80)")
max++;
}
}
if(max==81)
{
StopClock2()
won();
}
else
alert("לא סיימתה את המשחק או שיש לך טעות ")
}
/***************************************************************/
function won()//במקרה ומישהו זוכה
{
for (i = 1; i <= 5; i++)
{
$('<div/>', {
text: "You Won!!! Congrats!!!!",
id: 'div' + i
}).appendTo("body");
}
$("div").delay('slow').fadeIn();
$("<div/>",{
text: "הזמן שלך הוא "+"0" + minute + ":" + sic,
id:"element"
}).appendTo("body");
}//end of won() function