-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
170 lines (140 loc) · 3.63 KB
/
main.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
window.onload = function() {
load();
resize();
}
window.onresize = function() {
resize();
}
let bingoDiv;
let clickMap = [];
let data = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]];
function load() {
bingoDiv = document.getElementById("bingo");
shuffle(items);
for(let y = 0; y < 4; y++) {
for(let x = 0; x < 4; x++) {
let item = items[x + y * 4];
let s = new State(x, y, false);
clickMap[item] = s;
data[x][y] = s;
appendBingo(genEntry(item));
}
}
for(e of document.getElementsByClassName("text-holder")) {
e.onclick = onClick;
}
}
function genEntry(content) {
return "<div class='field border'>" +
"<div class='text-wrapper'>" +
"<div class='text-holder text-form noselect'>" +
content +
"</div></div></div>";
}
function appendBingo(str) {
bingoDiv.innerHTML += str;
}
function updateColors() {
let bingo = findBingo();
for(e of document.getElementsByClassName("text-holder")) {
let elem = clickMap[e.innerHTML];
if(elem.clicked) {
if(bingo.includes(elem)) {
e.style.backgroundColor = "#008376";
} else {
e.style.backgroundColor = "#094771";
}
} else {
e.style.backgroundColor = "transparent";
}
}
}
function resize() {
let sizeRef = Math.min(bingoDiv.parentElement.clientHeight, bingoDiv.parentElement.clientWidth);
let sizeItem = ((sizeRef - 3) / 4) - 3 - 1.5;
let fontSize = sizeItem * 0.15;
for(e of document.getElementsByClassName("field")) {
e.style.width = sizeItem + "px";
e.style.height = sizeItem + "px";
e.style.fontSize = fontSize + "px";
}
bingoDiv.style.width = sizeRef + "px";
bingoDiv.style.height = sizeRef + "px";
}
function onClick(e) {
let s = clickMap[e.currentTarget.innerHTML];
s.clicked = !s.clicked;
data[s.x][s.y] = s;
updateColors();
}
/**
* Shuffles array in place.
* @param {Array} a items An array containing the items.
*/
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
function State(x, y, clicked) {
this.x = x;
this.y = y;
this.clicked = clicked;
}
function findBingo() {
let bingo = [];
for(let x = 0; x < 4; x++) {
let found = true;
for(let y = 0; y < 4 && found; y++) {
if(!data[x][y].clicked) {
found = false;
}
}
if(found) {
for(let y = 0; y < 4; y++) {
bingo.push(data[x][y]);
}
}
}
for(let y = 0; y < 4; y++) {
let found = true;
for(let x = 0; x < 4 && found; x++) {
if(!data[x][y].clicked) {
found = false;
}
}
if(found) {
for(let x = 0; x < 4; x++) {
bingo.push(data[x][y]);
}
}
}
let found = true;
for(let x = 0; x < 4 && found; x++) {
if(!data[x][x].clicked) {
found = false;
}
}
if(found) {
for(let x = 0; x < 4; x++) {
bingo.push(data[x][x]);
}
}
found = true;
for(let x = 0; x < 4 && found; x++) {
if(!data[3 - x][x].clicked) {
found = false;
}
}
if(found) {
for(let x = 0; x < 4; x++) {
bingo.push(data[3 - x][x]);
}
}
return bingo;
}