-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathant.js
More file actions
156 lines (132 loc) · 4.09 KB
/
Copy pathant.js
File metadata and controls
156 lines (132 loc) · 4.09 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
(function(){
antLibrary = {};
var allAntsArray = [];
var allColonyArray = [];
var xMax, yMax;
var materials = ["food", "space"]
var canvas = document.getElementById('map');
var context = canvas.getContext('2d');
moveAnt = function(Ant){
if(Ant.returningHome){
} else {
}
var xDir = Ant.forwardLocation.x === 0 ? 2 : Ant.forwardLocation.x;
var yDir = Ant.forwardLocation.y === 0 ? 2 : Ant.forwardLocation.y;
Ant.forwardLocation.x = xDir === 2 ? Math.round(Math.random() * xDir) - 1 : Math.round(Math.random() * xDir);
Ant.forwardLocation.y = yDir === 2 ? Math.round(Math.random() * yDir) - 1 : Math.round(Math.random() * yDir);
var tempMoveX = Ant.locationX + Ant.forwardLocation.x;
var tempMoveY = Ant.locationY + Ant.forwardLocation.y;
var tempMap = map[tempMoveX][tempMoveY];
if(tempMap.blockType === "food" && tempMap.density > 0){
} else {
Ant.locationX += Ant.forwardLocation.x;
Ant.locationY += Ant.forwardLocation.y;
}
if(Ant.locationY >= 99){
Ant.locationY = 98;
}
if(Ant.locationX >= 99){
Ant.locationX = 98;
}
if(Ant.locationX < 1){
Ant.locationX = 1;
}
if(Ant.locationY < 1){
Ant.locationY = 1;
}
map[Ant.locationX][Ant.locationY].antPresent = true;
map[Ant.locationX][Ant.locationY].pheromeLevel += 1;
return Ant;
}
antLibrary.getAllAnts = function(){
return allAntsArray;
}
antLibrary.createAnt = function(type){
var newAnt = new Ant(type);
allAntsArray.push(newAnt);
return newAnt;
}
antLibrary.makeSomeMoves = function(){
return allAntsArray.map(moveAnt)
}
antLibrary.clearCanvas = function(){
map.map(function(yArray){
return yArray.map(function(coord){
coord.antPresent = false;
return coord;
})
})
context.clearRect(0, 0, canvas.width, canvas.height)
}
buildMap = function(dimension){
var map = [];
for(var height = 0; height < dimension; height++){
var one = [];
for(var width = 0; width < dimension; width++){
var spaceAndDensity = getSpaceAndDensity();
one.push({x:height, y:width, material:spaceAndDensity.blockType, density:spaceAndDensity.density, antPresent:false, pheromeLevel:0})
}
map.push(one)
}
xMax = dimension;
yMax = dimension;
return map;
}
getSpaceAndDensity = function(){
var die1 = Math.round(Math.random()*100);
var die2 = Math.round(Math.random()*10 * Math.random()*10 * Math.random()*10/10);
var object = {blockType:"dirt", density:die2}
if(die1 < 10){
object.blockType = "food"
}
return object;
}
var Ant = function (type) {
this.type = type || 'forager';
this.locationX = 50;
this.locationY = 50;
this.forwardLocation={x:0,y:0}
};
antLibrary.showPositions = function(){
map.forEach(function(xRow){
xRow.forEach(function(coord){
if(coord.antPresent){
context.fillStyle = '#FF0000';
context.beginPath();
context.arc(coord.x*10, coord.y*10, 1, 0, Math.PI * 2, true);
context.fill();
}
if(coord.pheromeLevel > 0){
context.fillStyle = 'rgba(146, 59, 146,'+coord.pheromeLevel/10+')';
context.beginPath();
context.arc(coord.x*10, coord.y*10, 1, 0, Math.PI * 2, true);
context.fill();
}
if(coord.material === 'food'){
context.fillStyle = '#3E823A';
context.beginPath();
context.arc(coord.x*10, coord.y*10, 1, 0, Math.PI * 2, true);
context.fill();
}
})
})
}
var map = buildMap(100);
return antLibrary;
}())
createtonsoAnts = function(numberOfAnts){
for(var i = 0; i<numberOfAnts; i++){
antLibrary.createAnt();
}
}
makeMultipleMoves = function(numberOfMoves){
}
makeOneMoveAndDisplay = function(){
antLibrary.clearCanvas();
antLibrary.makeSomeMoves();
antLibrary.showPositions();
}
runSimulation = function(numberOfAnts, refreshRate){
createtonsoAnts(numberOfAnts || 10)
window.setInterval(makeOneMoveAndDisplay, refreshRate || 300)
}