-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
204 lines (180 loc) · 6.3 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include "newSleep.h"
#include "random.h"
#include "conio.h"
#include "color.h"
int m,n;
int a1i,a1j,a2i,a2j;
char position1,position2;
int c;//temp
void display(char *arr){
system("CLS"); // clearing screen
printf("\n\n\t+");//outer box
for(int i=0;i<=n;i++) printf("-");
printf("+");
printf("\n");
for(int i=0;i<m;i++){
printf("\t| ");
for(int j=0;j<n;j++){
char color_code = *(arr + i * n + j); // get the color status of cell
switch (color_code){ //set that color for background [ next character ]
case '0':
setBackground("black");
break;
case '1':
setBackground("green");
break;
case '2':
setBackground("red");
break;
case '3':
setBackground("blue");
break;
default:
break;
}
if(a1i == i && a1j == j) printf("%c",position1); // if ant present at cell print ant position
else if(a2i == i && a2j == j) printf("%c",position2);
else printf(" "); // else print special character " "
}
setBackground("black");
printf("|\n");
}
printf("\t+");//outer box
for(int i=0;i<=n;i++) printf("-");
printf("+\n\n");
}
// moveAntOne function updates the positions and direction of ant 1
void moveAntOne(char *arr){
if(*(arr + a1i * n + a1j) =='0'){ //anti-clockwise
// updating color of cell in map where ant is present
*(arr + a1i * n + a1j)='2';
// updating the position and location variables of ant
if(position1=='^' && a1j!=0 && (a1i!=a2i || a2j!=(a1j-1))){
position1='<';
a1j--;
}else if (position1=='v' && a1j!=n-1 && (a1i!=a2i || a2j!=(a1j+1))){
position1='>';
a1j++;
}else if(position1=='>' && a1i!=0 && (a1i!=a2i || a2j!=(a1j-1))){
position1='^';
a1i--;
}else if (position1=='<' && a1i!=m-1 && (a1i!=a2i || a2j!=(a1j+1))){
position1='v';
a1i++;
}
}else {// clockwise
// updating color of cell in map where ant is present
*(arr + a1i * n + a1j)='0';
// updating the position and location variables of ant
if(position1=='v' && a1j!=0 && (a1i!=a2i || a2j!=(a1j-1))){
position1='<';
a1j--;
}else if (position1=='^' && a1j!=n-1 && (a1i!=a2i || a2j!=(a1j+1))){
position1='>';
a1j++;
}else if(position1=='<' && a1i!=0 && (a1i!=a2i || a2j!=(a1j-1))){
position1='^';
a1i--;
}else if (position1=='>' && a1i!=m-1 && (a1i!=a2i || a2j!=(a1j+1))){
position1='v';
a1i++;
}
}
}
// moveAntTwo function updates the positions and direction of ant 2
void moveAntTwo(char *arr){
initRandom();
int r = random(1,4);
// updating color of cell in map where ant is present
if(*(arr + a2i * n + a2j)=='0') *(arr + a2i * n + a2j)='3';
else *(arr + a2i * n + a2j)='0';
// updating the position and location variables of ant
switch (r){
case 1://right
if(a2j!=n-1 && (a1i!=a2i || a1j!=(a2j+1))){
position2='>';
a2j++;
}
break;
case 2://left
if(a2j!=0 && (a1i!=a2i || a1j!=(a2j-1))){
position2='<';
a2j--;
}
break;
case 3://top
if(a2i!=0 && (a1j!=a2j || a1i!=(a2i+1))){
position2='^';
a2i--;
}
break;
case 4://bottom
if(a2i!=m-1 && (a1j!=a2j || a1i!=(a2i-1))){
position2='v';
a2i++;
}
break;
default:
break;
}
}
int main(int argc,char *argv[]){
//-------------------------------------------------+ ERROR CHECKS
if(argc != 4){
printf("\n\t+-------------------------------------------+\n");
printf("\t+ ERROR IN COMMAND LINE ARGUMENTS +\n");
printf("\t+-------------------------------------------+\n\n");
return 0;
}
int niterations = atoi(argv[2]);
if(niterations <0) return 0;
float tsleep = atof(argv[3]);
if(tsleep <0) return 0;
//-------------------------------------------------+ READING FILE
FILE *fp;
fp = fopen(argv[1],"r");
if(fp == NULL){
printf("\n\t+-------------------------------------------+");
printf("\n\t+ ERROR IN OPEANING %s FILE +",argv[1]);
printf("\n\t+-------------------------------------------+\n\n");
return 0;
}
// FIRST LINE OF FILE M=Rows N=column
fscanf(fp,"%d ",&m);
fscanf(fp,"%d\n",&n);
// SECOND LINE OR FILE ant one positions
fscanf(fp,"%d ",&a1i);
fscanf(fp,"%d ",&a1j);
fscanf(fp, "%c\n", &position1);
// THIRD LINE OR FILE ant two positons
fscanf(fp,"%d ",&a2i);
fscanf(fp,"%d ",&a2j);
fscanf(fp, "%c\n", &position2);
// GRID SCANNING
char *arr = (char *)malloc(m * n * sizeof(char));
for(int i=0;i<m;i++){
for(int j=0;j<n-1;j++){
fscanf(fp, "%c ", (arr + i * n + j));
}
fscanf(fp, "%c\n", (arr + i * n + n-1));
}
// making positon vectors index competable
a1j--;
a1i--;
a2i--;
a2j--;
//--------------------------------------------------------+ CYCLE
// this displays the map 2 times in 1 cycle
// because 2 ant updates are there which are not simultaneous
for(int i=0; i<niterations; i++){
display(arr);
newSleep(tsleep);
moveAntOne(arr);
display(arr);
newSleep(tsleep);
moveAntTwo(arr);
} // display -> sleep -> update ant1 -> display ->sleep ->update ant2
display(arr);
}