-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeptember20.cpp
More file actions
316 lines (273 loc) · 8.55 KB
/
Copy pathSeptember20.cpp
File metadata and controls
316 lines (273 loc) · 8.55 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MAPROW 100
#define MAPCOL 200
#define WALLSIZE 1
#define DIRECTIONS 4
typedef struct Direction
{
int nLine;
int nCol;
};
typedef struct Trof
{
int nIT;
int nJT;
int nSx;
Direction sDx[DIRECTIONS + 1];
};
int TestTrons(Trof* sTrof, Direction sDrc,
int* nTronAmount, int mMat[][MAPCOL + WALLSIZE * 2]);
int TrofCheckCells(int mMat[][MAPCOL + WALLSIZE * 2],
Trof* sTrof, int* nTronAmount, Direction sDrc);
void GetTrof(Trof* sTrof);
void TrofLoop(Trof sTrof, int mMat[][MAPCOL + WALLSIZE * 2], int nTronAmount);
void GetTrons(int mMat[][MAPCOL + WALLSIZE * 2], int* nTronAmount);
void TickInt(int* nNum, int* bRising);
void SetDirections(Trof* sTrof);
//-----------------------------------------------------------------------------
// TRON AND TROF
// -----------
//
// General : This is a simulation of the TRON and TROF war.
//
// Input : Infromation about the TRONs and TROF.
//
// Process : Simulate the war.
//
// Output : Prints events in the war and the results.
//
//-----------------------------------------------------------------------------
// Programmer : Gonen Cohen
// Student No : N/A
// Date : 20.09.2020
//-----------------------------------------------------------------------------
int main()
{
int mMap[MAPROW + WALLSIZE * 2][MAPCOL + WALLSIZE * 2] = {0};
int nTronAmount = 0;
Trof sTrof;
SetDirections(&sTrof, -1, 0, 0, 1);
GetTrons(mMap, &nTronAmount);
TrofLoop(sTrof, mMap, nTronAmount);
return 0;
}
//-----------------------------------------------------------------------------
// TestTrons
// ---------
//
// General : Checks if the next point has TRONs and outputs relevant results
//
// Parameters :
//
// sTrof - TROF structure (I\O)
// sDrc - The structure for the wanted direction with the next point.
// nTronAmount - The total amount of trons on the map (Out)
// mMat - The general matrix for the map (I\O)
//
// Return value - Boolean that indicates if TROF is alive
//
//-----------------------------------------------------------------------------
int TestTrons(Trof* sTrof, Direction sDrc,
int* nTronAmount, int mMat[][MAPCOL + WALLSIZE * 2])
{
int nTrons;
int nYOffset = sTrof->nIT + sDrc.nLine;
int nXOffset = sTrof->nJT + sDrc.nCol;
// Check if cell is inside the wall
if (nXOffset >= WALLSIZE && nXOffset <= MAPROW + WALLSIZE - 1 &&
nYOffset >= WALLSIZE && nYOffset <= MAPCOL + WALLSIZE - 1)
{
nTrons = mMat[nXOffset][nYOffset];
printf("\nTROF advanced to (%d, %d) and encountered %d TRONS\n",
nXOffset, nYOffset, nTrons);
if (nTrons > 2)
{
printf("\nTROF tried to eat too many TRONs and died.\n");
mMat[nXOffset][nYOffset] = nTrons - 2;
*nTronAmount -= 2;
printf("%d TRONs were left. Game Over.\n", *nTronAmount);
return 0;
}
else
{
printf("TROF ate %d TRONs and decided to keep going.\n", nTrons);
mMat[nXOffset][nYOffset] = 0;
*nTronAmount -= nTrons;
}
sTrof->nIT = nYOffset;
sTrof->nJT = nXOffset;
}
else
{
printf("\nTROF stopped at the border of the planet.\n");
}
return 1;
}
//-----------------------------------------------------------------------------
// TrofCheckCells
// ---------
//
// General : Checks if TROFs path has TRONs and outputs relevant results
//
// Parameters :
//
// mMat - The general matrix for the map (I\O)
// sTrof - TROF structure (I\O)
// nTronAmount - The total amount of trons on the map (Out)
// sDrc - The structure for the wanted direction with the next point.
//
// Return value - Boolean that indicates if TROF is alive
//
//-----------------------------------------------------------------------------
int TrofCheckCells(int mMat[][MAPCOL + WALLSIZE * 2],
Trof* sTrof, int* nTronAmount, Direction sDrc)
{
// Check if this is the first arrival of TROF or taking steps
if (sTrof->nSx)
{
for (int i = 1; i <= sTrof->nSx; i++)
{
if (!TestTrons(sTrof, sDrc, nTronAmount, mMat)) return 0;
}
}
else
{
TestTrons(sTrof, sDrc, nTronAmount, mMat);
}
return 1;
}
//-----------------------------------------------------------------------------
// GetTrof
// ---------
//
// General : Get initial info about TROF
//
// Parameters :
// sTrof - TROF structure (I\O)
//
//-----------------------------------------------------------------------------
void GetTrof(Trof* sTrof)
{
printf("\nInsert X location (1 - %d) for TROF: ", MAPROW);
scanf("%d", &sTrof->nIT);
printf("Insert Y location (1 - %d) for TROF: ", MAPCOL);
scanf("%d", &sTrof->nJT);
}
//-----------------------------------------------------------------------------
// TrofLoop
// ---------
//
// General : The main loop of input and output about TROF
//
// Parameters :
// sTrof - TROF structure (I\O)
// mMat - The general matrix for the map (I\O)
// nTronAmount - The total amount of trons on the map (Out)
//
//-----------------------------------------------------------------------------
void TrofLoop(Trof sTrof, int mMat[][MAPCOL + WALLSIZE * 2], int nTronAmount)
{
GetTrof(&sTrof);
int nDirection = 0, nTrons = mMat[sTrof.nIT][sTrof.nJT];
sTrof.nSx = 0;
char c;
do
{
// Check if TROF is still alive after taking steps or arriving
if(!TrofCheckCells(mMat, &sTrof, &nTronAmount, sTrof.sDx[nDirection]))
return;
printf("\nInsert number of steps to take: ");
scanf("%d", &sTrof.nSx);
printf("Insert direction for steps (1 - 4 clockwise): ");
scanf(" %c", &c);
nDirection = c - '0';
} while (sTrof.nSx && nDirection);
printf("\nTROF got tired and doesn't want to move anymore.\n");
printf("%d TRONs are left. Game Over.\n", nTronAmount);
}
//-----------------------------------------------------------------------------
// GetTrons
// ---------
//
// General : Process input of number of TRONs and their locations.
//
// Parameters :
// mMat - The general matrix for the map (I\O)
// nTronAmount - The total amount of trons on the map (Out)
//
//-----------------------------------------------------------------------------
void GetTrons(int mMat[][MAPCOL + WALLSIZE * 2], int* nTronAmount)
{
int nIx, nJx;
printf("Insert number of TRONs: ");
scanf("%d", nTronAmount);
printf("\nThe map size is %d rows and %d columns\n", MAPROW, MAPCOL);
for(int i = 1; i <= *nTronAmount; i++)
{
printf("\nInsert X location (1 - %d) for TRON #%d: ", MAPROW, i);
scanf("%d", &nIx);
printf("Insert Y location (1 - %d) for TRON #%d: ", MAPCOL, i);
scanf("%d", &nJx);
mMat[nIx + WALLSIZE - 1][nJx + WALLSIZE - 1]++;
}
}
//-----------------------------------------------------------------------------
// TickInt
// ---------
//
// General : Generates the pattern needed to generate directions.
//
// Parameters :
// nNum - Direction value to modify (I\O)
// bRising - Boolean modifier for nNum to check if it's rising or falling
//
//-----------------------------------------------------------------------------
void TickInt(int* nNum, int* bRising)
{
switch (*nNum)
{
// If rising rise until 1 and start to fall again
// Like a clockwise motion in a matrix
case 0:
if (*bRising) *nNum = 1;
else *nNum = -1;
break;
case 1:
*nNum = 0;
*bRising = 0;
break;
case -1:
*nNum = 0;
*bRising = 1;
break;
default:
break;
}
}
//-----------------------------------------------------------------------------
// SetDirections
// ---------
//
// General : Generates the direction array for TROF
//
// Parameters :
// sTrof - TROF structure (I\O)
// nX - Initial X value for pattern (In)
// nY - Initial Y value for pattern (In)
// nModX - Initial X Modifier for pattern (In)
// nModY - Initial Y Modifier for pattern (In)
//
//-----------------------------------------------------------------------------
void SetDirections(Trof* sTrof, int nX, int nY, int nModX, int nModY)
{
sTrof->sDx[0].nLine = 0;
sTrof->sDx[0].nCol = 0;
for (int i = 1; i <= DIRECTIONS; i++)
{
sTrof->sDx[i].nLine = nX;
sTrof->sDx[i].nCol = nY;
TickInt(&nX, &nModX);
TickInt(&nY, &nModY);
}
}