-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (111 loc) · 2.59 KB
/
app.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
// Bingo Cards
class BingoCards {
constructor(cards) {
this.cards = cards;
}
generate() {
for (let i = 0; i < 6; i++) {
this.drawCard();
}
}
drawCard() {
let card = this.createCard();
let table = document.createElement('table');
for (let y = 0; y < 3; y++) {
let row = document.createElement('tr');
for (let x = 0; x < rowLen; x++) {
let cell = document.createElement('td');
let value = card[y][x];
cell.innerText = value;
row.appendChild(cell);
if (value !== blankCell) {
cell.addEventListener('click', (event) => {
this.toggleCell(cell);
});
} else {
cell.className = 'blank';
}
}
table.appendChild(row);
}
this.cards.appendChild(table);
}
createCard() {
let card = this.blankCard();
for (let x = 0; x < rowLen; x++) {
let [from, to] = colRanges[x];
let nums = [];
let offset = 0;
for (let y = 0; y < card.length; y++) {
if (card[y][x] == '#') {
let num = randRange(from, to-offset);
while (nums.includes(num)) {
num++;
}
nums.push(num);
offset++;
}
}
nums.sort();
let i = 0;
for (let y = 0; y < card.length; y++) {
if (card[y][x] == '#') {
card[y][x] = nums[i];
i++;
}
}
}
return card;
}
// blankCard creates a card with spaces marked '_' and numbers marked '#'
blankCard() {
let card = [];
for (let y = 0; y < 3; y++) {
let row = ['#', '#', '#', '#', '#', '#', '#', '#', '#'];
for (let i = 0; i < 4; i++) {
let space = rand(rowLen-i);
while (row[space] != '#') {
space++;
}
row[space] = blankCell;
}
card.push(row);
}
return card;
}
toggleCell(cell) {
if (cell.className == 'called') {
cell.className = '';
} else {
cell.className = 'called';
}
}
}
const blankCell = '\xa0';
const rowLen = 9;
const colRanges = [
[1, 9],
[10, 19],
[20, 29],
[30, 39],
[40, 49],
[50, 59],
[60, 69],
[70, 79],
[80, 90]
];
// rand returns a random int between 0 and n-1 (inclusive)
function rand(n) {
return Math.floor(Math.random() * n);
}
// randRange returns a random int between a and b (inclusive)
function randRange(a, b) {
let x = rand(b+1-a);
return x + a;
}
document.addEventListener('DOMContentLoaded', (event) => {
let cards = document.getElementById('cards');
bingo = new BingoCards(cards);
window.bingo = bingo;
bingo.generate();
});