-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
185 lines (171 loc) · 5.21 KB
/
Copy pathscript.js
File metadata and controls
185 lines (171 loc) · 5.21 KB
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
// document elements
const startCard = document.getElementById("start-card");
const endCard = document.getElementById("end-card");
const cardContainer = document.getElementById("card-container");
//operator
const operationButtons = document.querySelectorAll("#operator");
//menu bar
const newGame = document.getElementById("newGame");
const resetGame = document.getElementById("resetGame");
const leveContainer = document.getElementById("level-container");
// alert S/F
const successAlert = document.getElementById("success-alert");
const failAlert = document.getElementById("fail-alert");
const errorAlert = document.getElementById("error-alert");
//modeal
let modal = document.getElementById("help-modal");
let btn = document.getElementById("open-btn");
let button = document.getElementById("ok-btn");
// global variables
const numCard =
'<div class="flex shadow-xl rounded cursor-pointer w-4 h-4 p-9 border-2 border-gray-600 text-4xl justify-center items-center" id="card"></div>';
let level = 2;
let selectedOperation = null;
let startValue;
let endValue;
let cardValues;
function generateCards(newLevel) {
if (newLevel) cardContainer.innerHTML = "";
for (var i = 0; i < level; i++)
cardContainer.insertAdjacentHTML("afterbegin", numCard);
generateNum();
}
function generateNum() {
startValue = Math.floor(Math.random() * 20) + 1;
cardValues = generateRandomArray();
let temp = startValue;
let inifity = 0;
while (1) {
if (inifity++ > 64) {
cardValues = generateRandomArray();
inifity = 0;
}
temp = startValue;
const startingCardIndex = Math.floor(Math.random() * level);
temp = generateOperation(temp, cardValues[startingCardIndex], null);
if (temp === null) continue;
// Choose remaining cards and perform operation
const remainingCards = cardValues.filter(
(value, index) => index !== startingCardIndex
);
for (var i = 0; i < remainingCards.length; i++) {
temp = generateOperation(temp, remainingCards[i], null);
if (temp === null) break;
}
if (temp !== null && temp > 0 && temp <= 30) {
endValue = temp;
break;
} else {
continue;
}
}
WriteNumtoCards();
}
//write
function WriteNumtoCards() {
startCard.textContent = startValue;
endCard.textContent = endValue;
const cards = document.querySelectorAll("#card");
cards.forEach((element, key) => {
element.textContent = cardValues[key];
if (element.classList.contains("invisible"))
element.classList.remove("invisible");
element.addEventListener("click", handleClick);
});
alertReset();
}
// Generate random array
function generateRandomArray() {
const cardValues = [];
for (var i = 0; i < level; i++) {
let value = Math.floor(Math.random() * 20) + 1;
while (cardValues.includes(value)) {
value = Math.floor(Math.random() * 20) + 1;
}
cardValues.push(value);
}
return cardValues;
}
//Generate operation
function generateOperation(first, second, operator) {
if (operator === null) operator = Math.floor(Math.random() * 4);
switch (operator) {
case 0:
return first + second;
case 1:
return first - second;
case 2:
return first * second;
case 3:
if (first % second === 0) {
first /= second;
return first;
} else {
return null;
}
}
}
// Set hander to get the current operation
operationButtons.forEach(function (button) {
button.addEventListener("click", function (e) {
selectedOperation = parseInt(e.target.value);
});
});
// card click event handler
function handleClick() {
if (selectedOperation === null) return;
var result = generateOperation(
parseInt(startCard.textContent),
parseInt(this.textContent),
selectedOperation
);
if (result === null) {
errorAlert.classList.remove("hidden");
selectedOperation = null;
document.querySelectorAll("#card").forEach((element) => {
if (!element.classList.contains("invisible"))
element.classList.add("invisible");
});
return;
}
startCard.textContent = result;
this.classList.add("invisible");
if (document.querySelectorAll("#card[class*='invisible']").length === level) {
if (result === endValue) {
successAlert.classList.remove("hidden");
} else {
failAlert.classList.remove("hidden");
}
return;
}
selectedOperation = null;
}
//alert
function alertReset() {
if (!successAlert.classList.contains("hidden"))
successAlert.classList.add("hidden");
if (!failAlert.classList.contains("hidden"))
failAlert.classList.add("hidden");
if (!errorAlert.classList.contains("hidden"))
errorAlert.classList.add("hidden");
}
// Generate new level
function changeLevel() {
level = parseInt(this.value);
generateCards(true);
}
//modal
btn.addEventListener("click", function () {
modal.style.display = "block";
});
button.addEventListener("click", function () {
modal.style.display = "none";
});
//add new game function
newGame.addEventListener("click", generateNum);
//reset game function
resetGame.addEventListener("click", WriteNumtoCards);
// add function to level container
leveContainer.addEventListener("change", changeLevel);
// Call generateCards after the DOM has loaded
document.addEventListener("DOMContentLoaded", generateCards(false));