-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArithmeticBoi.js
197 lines (169 loc) · 5.66 KB
/
ArithmeticBoi.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
var currentResult;
var points = 0;
var rounds = 0;
var level = 1;
var plus = true;
var minus = false;
var times = false;
var divided = false;
function askMathProblem() {
// startet das Spiel und jede weitere Runde
// zudem werden Aufgaben durch die Funktion gestellt
var firstNumber;
var secondNumber;
var operator;
var mathProblem;
resetInputFieldColor("input");
// Inhalt des Eingabefeldes wieder resetten damit eine neue Runde gestartet werden kann
document.getElementById("input").value = "";
rounds++;
if (points > level * 5) {
level++;
}
// Aufgabe und Operator random generieren
var firstNumber = Math.round(Math.random() * level * 5 + 0.5);
var secondNumber = Math.round(Math.random() * level * 5 + 0.5);
var operator = generateRandomOperator();
switch (operator) {
case 1:
mathProblem = firstNumber + " + " + secondNumber + " = ";
currentResult = firstNumber + secondNumber;
break;
case 2:
if (firstNumber < secondNumber) {
tempValue = firstNumber;
firstNumber = secondNumber;
secondNumber = tempValue;
}
mathProblem = firstNumber + " - " + secondNumber + " = ";
currentResult = firstNumber - secondNumber;
break;
case 3:
mathProblem = firstNumber + " * " + secondNumber + " = ";
currentResult = firstNumber * secondNumber;
break;
case 4:
mathProblem = firstNumber + " : " + secondNumber + " = ";
currentResult = firstNumber / secondNumber;
break;
default:
alert("Es wurde kein Operator gefunden.");
}
document.getElementById("Frage").innerHTML = mathProblem;
document.getElementById("Level").innerHTML = "Level " + level;
document.getElementById("Points").innerHTML = "Points " + points;
document.getElementById("Rounds").innerHTML = " Rounds " + rounds;
}
function generateRandomOperator() {
do {
result = Math.round(Math.random() * 4 + 0.5);
} while (result == 1 && !this.plus || result == 2 && !this.minus || result == 3 && !this.times || result == 4 && !this.divided);
return result;
}
function checkInput() {
// prüft die Eingabe des Spielers
// bei richtigem Ergebnis Steigerung der Punkte
var currentInput = document.getElementById("input").value;
console.log("inputted Value " + document.getElementById("input").value);
if (currentInput == currentResult) {
document.getElementById("input").style.background = "#60c78b";
points++;
} else {
document.getElementById("input").style.background = "#ff6060";
}
setTimeout(askMathProblem, 500);
return false;
}
function resetInputFieldColor(id) {
document.getElementById(id).style.background = "white";
}
function saveGameState() {
localStorage.setItem("Points", points);
localStorage.setItem("Rounds", rounds);
localStorage.setItem("Level", level);
alert("Saved Game State...");
}
function loadGameState() {
if (localStorage.getItem("Level")) {
points = parseInt(localStorage.getItem("Points"));
rounds = parseInt(localStorage.getItem("Rounds"));
level = parseInt(localStorage.getItem("Level"));
askMathProblem();
alert("Save Game loaded...")
}
}
function deleteGameState() {
localStorage.clear();
points = 0;
rounds = 0;
level = 1;
askMathProblem();
}
function toggleOperator(button) {
if (button.style.backgroundColor == "white") {
button.style.background = "#e083b6";
} else {
button.style.background = "white";
}
selectOperator(button.innerHTML, button);
}
function isOperationSelected(id) {
var button = document.getElementById(id);
return (button.style.backgroundColor != "white");
}
function selectOperator(operator, button) {
switch (operator) {
case "+":
if (plus && !minus && !times && !divided) {
var message = alert("At least one operator must be switched on.");
button.style.background = "#e083b6";
return;
} else {
if (!plus) {
plus = true;
} else if (plus) {
plus = false;
}
}
break;
case "-":
if (!plus && minus && !times && !divided) {
var message = alert("At least one operator must be switched on.");
button.style.background = "#e083b6";
return;
} else {
if (!minus) {
minus = true;
} else if (minus) {
minus = false;
}
}
break;
case "*":
if (!plus && !minus && times && !divided) {
var message = alert("At least one operator must be switched on.");
button.style.background = "#e083b6";
return;
} else {
if (!times) {
times = true;
} else if (times) {
times = false;
}
}
break;
case ":":
if (!plus && !minus && !times && divided) {
var message = alert("At least one operator must be switched on.");
button.style.background = "#e083b6";
return;
} else {
if (!divided) {
divided = true;
} else if (divided) {
divided = false;
}
}
break;
}
}