-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate.js
88 lines (75 loc) · 1.63 KB
/
generate.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
var phraseArrayLength = phrases.length;
var cells = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
];
$(function() {
randomize();
displayGame();
});
function randomize() {
var usedPhrases = [];
var index;
$(".word").each(function() {
do {
var index = randInt(phraseArrayLength);
} while ($.inArray(index, usedPhrases) != -1);
usedPhrases.push(index);
$(this).html(phrases[index]);
})
}
function displayGame() {
$(".card").click(function() {
if ($(this).hasClass("spoken")) {
$(this).removeClass("spoken");
setCellValue(this, 0);
}
else {
$(this).addClass("spoken");
setCellValue(this, 1);
}
});
}
function setCellValue(card, value) {
$("#video").empty();
var row, col;
var cards = $(".card");
for(var i=0; i<cards.length; i++) {
if (cards[i] == card) {
row = Math.floor(i / 4);
col = i % 4;
break;
}
}
cells[row][col] = value;
for (var i=0; i<4; i++) {
var rowValue = 0, colValue = 0, diagValue1 = 0, diagValue2 = 0;
for (var j=0; j<4; j++) {
rowValue += cells[i][j];
colValue += cells[j][i];
diagValue1 += cells[j][j];
diagValue2 += cells[j][3-j];
if(rowValue == 4 || colValue == 4 || diagValue1 == 4 || diagValue2 == 4) {
win();
return
}
}
}
}
function win() {
var index = randInt(videos.length);
$("#video").append($("<iframe width='560' height='315' src='"+ videos[index]+"' frameborder='0' allowfullscreen></iframe>"));
}
function showAll() {
$(".card").addClass("spoken");
}
function newGame() {
$(".card").removeClass("spoken");
randomize();
displayGame();
}
function randInt(max) {
return Math.floor(Math.random() * max);
}