-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
48 lines (48 loc) · 1.3 KB
/
game.js
File metadata and controls
48 lines (48 loc) · 1.3 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
var bulls, cows;
function rnd(a, b){
return Math.floor(Math.random()*(b-a+1+a));
}
function makeNum(){
var nn = ""; // making nonrepeatable numbers
for (let i = 0; i < 4; i++){
do{
var c = rnd(0, 9)
}
while (nn.indexOf(c) >= 0);
nn = nn + c;
}
return nn;
}
function analize(knownNumber, attempt){
bulls = 0;
cows = 0;
for (let i = 0; i < 4; i++){
if (knownNumber[i] == attempt[i])
bulls++;
else if(knownNumber.indexOf(attempt[i]) >= 0)
cows++;
}
}
var compNumbers = makeNum();
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Input number: ", function(writtenNumber) {
for (let i = 0; i < 10; i++){
analize(compNumbers, writtenNumber);
var result = bulls + " Bulls " + cows + " Cows";
if(writtenNumber === compNumbers){
console.log("Numbers entered by computer: "+ compNumbers);
console.log("You win!");
break;
}
else{
console.log("Numbers entered by computer: "+ compNumbers);
console.log("The result is: " + result)
break;
}
}
rl.close();
});