-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathant.js
80 lines (61 loc) · 1.61 KB
/
ant.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
// ant class
// will not have a show function as the world is shown
let ANTUP =0;
let ANTRIGHT =1;
let ANTDOWN =2;
let ANTLEFT =3;
class Ant{
constructor(x,y,clr){
this.x = x;
this.y =y;
this.clr =clr; // color is either 1 or 2 as it is marks which ant in the ant world
this.dir = 0;
this.sz = 10;
}
turnRight(){
this.dir++;
if (this.dir > ANTLEFT){
this.dir = ANTUP;
}
}
turnLeft(){
this.dir--;
if (this.dir < ANTUP){
this.dir = ANTLEFT;
}
}
moveForward(){
if (this.dir == ANTUP){
this.y--;
}else if (this.dir == ANTDOWN){
this.y++;
}else if (this.dir == ANTRIGHT){
this.x++;
}else if (this.dir == ANTLEFT){
this.x--;
}
// check edges
if (this.x > 80-1) {
this.x =0;
} else if (this.x < 0){
this.x = 80-1;
}
if (this.y > 80-1) {
this.y =0;
} else if (this.y < 0){
this.y = 80-1;
}
}
moveAnt(){
let state = antworld[this.x][this.y];
if (state == 0){
this.turnRight();
antworld[this.x][this.y] = this.clr; // places ant symbol
this.moveForward();
}else if (state >= 1){ // one or two for ant symbol
this.turnLeft();
antworld[this.x][this.y] = 0;
this.moveForward();
}
}
}