-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokemon.js
More file actions
98 lines (74 loc) · 2.42 KB
/
Pokemon.js
File metadata and controls
98 lines (74 loc) · 2.42 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
// JavaScript source code
(function FridayFunday() {
var moves = 0;
var trainerPosition = {
x: 0,
y: 6
};
//for algorithm to work pokemon must start above the trainer and to the right.
var pokemonPosition = {
x: 6,
y: 0
};
var trainerAi = function () {
var absX = Math.abs(pokemonPosition.x - trainerPosition.x);
var absY = Math.abs(pokemonPosition.y - trainerPosition.y);
if (absX >= absY) {
if (pokemonPosition.x > trainerPosition.x ) {
console.log("Trainer moves to the right");
trainerPosition.x++;
return;
}
console.log("Trainer moves to the left");
trainerPosition.x--;
return;
}
if (pokemonPosition.y > trainerPosition.y) {
console.log("Trainer moves down");
trainerPosition.y++;
return;
}
console.log("Trainer moves up");
trainerPosition.y--;
return;
}
//not an intelligent ui so it doesn't prove that the pokemon has to move first.
var pokemonAi = function () {
do {
var rand = Math.random() * 5;
if (rand < 1 && pokemonPosition.y > 0) {
pokemonPosition.y--;
console.log("Pokemon moves up");
return;
}
if (rand < 2 && pokemonPosition.y < 6) {
pokemonPosition.y++;
console.log("Pokemon moves down");
return;
}
if (rand < 3 && pokemonPosition.x > 0) {
pokemonPosition.x--;
console.log("Pokemon moves left");
return;
}
if (pokemonPosition.x < 6) {
pokemonPosition.x++;
console.log("Pokemon moves right");
return;
}
} while (1 === 1);
};
var catchPokemon = function()
{
while (pokemonPosition.x !== trainerPosition.x && pokemonPosition.y !== trainerPosition.y && moves < 1000) {
pokemonAi();
trainerAi();
console.log("Move: " + (++moves)+
"\nTrainer: "+trainerPosition.x+","+trainerPosition.y+
"\nPokemon: " + pokemonPosition.x + "," + pokemonPosition.y
);
}
console.log("You got a pokemon!");
}
catchPokemon();
})();