-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
executable file
·217 lines (195 loc) · 5.55 KB
/
Copy pathindex.html
File metadata and controls
executable file
·217 lines (195 loc) · 5.55 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html>
<head>
<title>A Number Game</title>
<link rel="stylesheet" type="text/css" href="common.css">
<style>
.dot {
height: 20px;
width: 20px;
background-color: red;
border-radius: 50%;
display: inline-block;
}
.results{
width: 20em;
margin: auto;
}
</style>
</head>
<script>
// creates html representation of specified dot
function MakeDot(colour) {
var string = '<span class="dot" style="background-color:';
switch (colour) {
case 'red':
string += '#ff0000';
break;
case 'green':
string += '#00cc00';
break;
case 'orange':
string += '#ff9900';
break;
default:
string += '#bbb';
break;
}
string += ';"></span>';
return string;
}
</script>
<body>
<div class="nav" id="nav">
<h2>Navigation</h2>
<li><a id="game" href="index.html" target="_self">Mastermind</a></li>
</div>
<div class="content" id="content">
<h1>A Number Game</h1>
<div id="info">
Maybe you know the game Mastermind. I used to play it with my dad, but at
that time we used colour pins instead of numbers. <br/>Even if you know it,
there are different versions how you can play it. I made the more difficult
one. <br/><br/>
You have to guess five numbers, from 0 to 9.<br/>
You get hints every round. But the hints are not sorted to fit the order of
your inputs. They are simply sorted green, orange, red. <br/>
<script>document.write(MakeDot('green'));</script> Green is for a number you guessed
correctly and put in the right spot in the number array.<br/>
<script>document.write(MakeDot("orange"));</script> Orange is for a number you guessed
correctly, but is not in the right spot of the number array.<br/>
<script>document.write(MakeDot("red"));</script> Red stands for a number you guessed that
doesn't belong in the number array.<br/><br/>
</div>
<div>
Numbers:
<input type="number" min="0" max="9" maxlength="1" size="1" id="number1" autofocus>
<input type="number" min="0" max="9" maxlength="1" size="1" id="number2">
<input type="number" min="0" max="9" maxlength="1" size="1" id="number3">
<input type="number" min="0" max="9" maxlength="1" size="1" id="number4">
<input type="number" min="0" max="9" maxlength="1" size="1" id="number5">
<button id="evalBtn" onclick="Evaluate()">Evaluate</button>
</div>
<div id="results" class="results"><h2>Your Input:</h2></div>
</div>
<script>
// add enter key as trigger:
var input = [document.getElementById("number1"),
document.getElementById("number2"),
document.getElementById("number3"),
document.getElementById("number4"),
document.getElementById("number5")];
for(i=0; i<input.length; i++){
input[i].addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("evalBtn").click();
}
});
}
// generate guessing numbers:
var goal = [];
var length = 5;
goal[0] = Math.floor(Math.random() * 10);
for(i=1; i<length; ){
var val = Math.floor(Math.random() * 10);
var flag = true;
for(j=0; j<i; j++){
if(goal[j] == val){
flag = false;
break;
}
}
if(flag == false){
}else{
goal[i]=val;
i++;
}
}
var roundCounter = 0;
function Evaluate() {
// check inputs for validity:
if(CheckInputs() == false){
return;
}
roundCounter++;
// save guessed numbers:
var guess = [];
for(i=0; i<input.length; i++){
guess[i] = parseInt(input[i].value);
}
// compare guessed numbers:
var evaluation = [];
for(i=0; i<guess.length; i++){
if(guess[i] == goal[i]){
evaluation[i] = "green";
}else if (goal.indexOf(guess[i]) != (-1)) {
evaluation[i] = "orange";
}else{
evaluation[i] = "red";
}
}
// sort evaluations (skip this if easy mode)
evaluation.sort();
// make representing dots, check if all numbers were guessed correctly:
var correctCount = 0;
for(i=0; i<evaluation.length; i++){
if(evaluation[i]=="green"){
correctCount++;
}
evaluation[i] = MakeDot(evaluation[i]);
}
// output guesses and evaluation:
document.getElementById("results").innerHTML += guess +
" " +
evaluation + "</br>";
// set focus back to first number input:
document.getElementById("number1").focus();
// check if all numbers were guessed correctly:
if(correctCount==length){
document.getElementById("evalBtn").disabled = true;
alert('Woohoo, you did it! \n Took you ' + roundCounter + ' rounds!');
}
}
// returns true if inputs are valid
function CheckInputs() {
/*(Yes, I know it's risky to set them to the correct value
at the beginning)*/
// all fields were filled:
var all_filled = true;
// right ammount of characters per input:
var num_of_chars_ok = true;
// only number inputs:
var only_nums = true;
for(i=0; i<input.length; i++){
if(input[i].value == ""){
all_filled = false;
break;
}
if(input[i].value.length > 1){
num_of_chars_ok = false;
break;
}
if(isNaN(input[i].value)){
only_nums=false;
break;
}
}
if(all_filled == false){
alert('You have to input five numbers');
return false;
}
if(num_of_chars_ok == false){
alert('Only one number per input field allowed');
return false;
}
// will probably never be called, still better safe than sorry.
if(only_nums == false){
alert('You cannot use letters etc.');
return false;
}
return true;
}
</script>
</body>
</html>