/
smiley.c
474 lines (396 loc) · 8.93 KB
/
smiley.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include "smiley.h"
#include <stdio.h>
#include <stdlib.h>
/* Define Error Messages' Case */
#define WRONG_USAGE 1
#define FILE_READ_ERROR 2
#define FILE_FORMAT_ERROR 3
#define PICKED_EMPTY 4
#define BLOCKED 5
#define INVALID_INPUT 6
/* Define Characters on The Ouput Screen */
#define SMILEY_N 'N'
#define SMILEY_S 'S'
#define SMILEY_W 'W'
#define SMILEY_E 'E'
#define WALL '='
#define NWALL '.'
#define EAST 0
#define NORTH 1
#define WEST 2
#define SOUTH 3
#define DELAY_TIME 1
/* Global Variables */
int M=0, N=0; //size of the map
int **Map, **RWall, **CWall;
int PosR=0, PosC=0;
int Facing=EAST;
/*
* Function: setup()
* Setup the map from the file entered via argv.
*/
void setup(int argc, char *argv[]){
// Chekc weather the user enter the right command
if(argc!=2){
printErrorAndExit(WRONG_USAGE);
}
// Scan in the size of the map
printf("Read file map from: %s\n",argv[1]);
FILE *fin = fopen(argv[1],"r");
if(fin == NULL) printErrorAndExit(FILE_READ_ERROR);
if(fscanf(fin,"%d%d", &M, &N)!=2)
printErrorAndExit(FILE_FORMAT_ERROR);
// Setup Smiley's starting position on the bottom-left
setSmileyPos();
// Setup the map and the wall array
Map = malloc2DArray(M,N);
RWall = malloc2DArray(M-1,N);
CWall = malloc2DArray(M,N-1);
// Scan in the whole map
readMap(fin);
readWalls(fin);
// Print out the map for debugging
printMap();
printWalls();
// Function end here
}
/*
* Function: readMap
* Read the map from input file and store in Map array
*/
void readMap(FILE *fin){
int i,j;
printf("\nMap:\n");
for(i=0;i<M;i++){
for(j=0;j<N;j++){
if(fscanf(fin,"%d",&Map[i][j])!=1)
printErrorAndExit(FILE_FORMAT_ERROR);
}
}
}
/*
* Function: readWalls
* Read the wall setting of this map and store in the RWall
* ans CWall arrays
*/
void readWalls(FILE *fin){
int i,j;
//read RWall
for(i=0;i<M-1;i++){
for(j=0;j<N;j++){
if(fscanf(fin,"%d",&RWall[i][j])!=1)
printErrorAndExit(FILE_FORMAT_ERROR);
}
}
//read CWall
for(i=0;i<M;i++){
for(j=0;j<N-1;j++){
if(fscanf(fin,"%d",&CWall[i][j])!=1)
printErrorAndExit(FILE_FORMAT_ERROR);
}
}
}
/*
* Function: printErrorAndExit()
* Print out the usage of this program depend on different
* cases and exit the program immediately.
*/
void printErrorAndExit(int errorCase){
//Fine the correspond error message
printf("[ERROR]");
switch(errorCase){
case WRONG_USAGE:
printf("Wrong Usage!\n"); break;
case FILE_READ_ERROR:
printf("File can't be read!\n");
printf("Please enter a map file as program argument.\nFor example: ./Smiley map.txt\n");
break;
case FILE_FORMAT_ERROR:
printf("File is not in a right format!\n"); break;
case PICKED_EMPTY:
printf("This spot is already empty.\n"); break;
case BLOCKED:
printf("The wall has blocked Smiley's way, he's died on the wall :(\n"); break;
case INVALID_INPUT:
printf("Wrong input.\n"); break;
}
//Exit the program immediately
printf("Exit Program.\n");
exit(1); }
/*
* Function: malloc2DArray
* Initialize a 2D array by given row and col.
* Exception: More error checking can be added.
*/
int** malloc2DArray(int row, int col){
int i;
int **arr;
arr = (int **)malloc(row * sizeof(int *));
for(i=0;i<row;i++){
arr[i] = malloc(col * sizeof(int));
}
return arr;
}
/*
* Function: printMap
* Print out the current map for debugging usage or for user.
*/
void printMap(){
int i,j;
for(i=0;i<M;i++){
for(j=0;j<N;j++){
printf("%d ", Map[i][j]);
}
printf("\n");
}
}
/*
* Function: printWalls
* Print out the wall setting which is same as input file for
* debugging.
*/
void printWalls(){
int i,j;
printf("\nWalls\n");
//Row Wall print
printf("\nRow Wall:\n");
for(i=0;i<M-1;i++){
for(j=0;j<N;j++){
printf("%d ", RWall[i][j]);
}
printf("\n");
}
//Col Wall print
printf("\nCol Wall:\n");
for(i=0;i<M;i++){
for(j=0;j<N-1;j++){
printf("%d ", CWall[i][j]);
}
printf("\n");
}
}
/*
* Function: printMapAndWalls
* Print out the real scene for Smiley's world including Walls and Flashes
*/
void printMapAndWalls(){
int i,j;
printf("\n");
//print out the top wall
printRowWall();
//middle part
char nextWall;
for(i=0;i<M-1;i++){
//place can be stood by Smiley
printf("%c",WALL);
for(j=0;j<N-1;j++){
nextWall=(CWall[i][j])?WALL:NWALL;
if(isSmileyHere(i,j)){
printf("%c%c",smileyFacing(),nextWall);
continue;
}
printf("%d%c",Map[i][j],nextWall);
}
if(isSmileyHere(i,j)){
printf("%c%c\n",smileyFacing(),WALL);
}
else{
printf("%d%c\n",Map[i][j],WALL);
}
//print a row of WALL or NWALL
printf("%c",WALL);
for(j=0;j<N;j++){
nextWall=(RWall[i][j])?WALL:NWALL;
printf("%c%c",nextWall,WALL);
}
printf("\n");
}
//place can be stood by Smiley(last row)
printf("%c",WALL);
for(j=0;j<N-1;j++){
nextWall=(CWall[i][j])?WALL:NWALL;
if(isSmileyHere(i,j)){
printf("%c%c",smileyFacing(),nextWall);
continue;
}
printf("%d%c",Map[i][j],nextWall);
}
if(isSmileyHere(i,j)){
printf("%c%c\n",smileyFacing(),WALL);
}
else{
printf("%d%c\n",Map[i][j],WALL);
}
//print out the bottom wall
printRowWall();
//print out other informations
printf("\n");
printf("Number of the flashed that is under Smiley's current position : %d\n", Map[PosR][PosC]);
//puase for a while
//this function seems doesn't work well
//sleep(DELAY_TIME);
}
/*
* Function: printRowWall
* Print out a row of wall
*/
void printRowWall(){
int i;
for(i=0;i<2*N+1;i++){
printf("%c",WALL);
}
printf("\n");
}
/*
* Function: isSmileyHere
* Check whether the Smiley is here or not.
*/
int isSmileyHere(int r, int c){
return (r==PosR && c==PosC);
}
/*
* Function: smileyFacing
* This function return the char which can be displayed
* on the screen corresponding to Smiley's current facing
* direction.
*/
char smileyFacing(){
if(Facing == EAST) return SMILEY_E;
if(Facing == NORTH) return SMILEY_N;
if(Facing == WEST) return SMILEY_W;
return SMILEY_S;
}
/*
* Function: setSmileyPos
* Setup Smiley's starting position to the bottom-left of
* the map.
*/
void setSmileyPos(){
PosR = M-1;
PosC = 0;
}
/*
* Function: blocked
* To determine whether it is blocked or not before actually
* move on.
*/
int blocked(){
//blocked by the outer wall
if(Facing == EAST && PosC==N-1) return 1;
if(Facing == WEST && PosC==0) return 1;
if(Facing == NORTH && PosR==0) return 1;
if(Facing == SOUTH && PosR==M-1) return 1;
//blocked by the inner wall
if(Facing == EAST && CWall[PosR][PosC]) return 1;
if(Facing == WEST && CWall[PosR][PosC-1]) return 1;
if(Facing == NORTH && RWall[PosR-1][PosC]) return 1;
if(Facing == SOUTH && RWall[PosR][PosC]) return 1;
return 0;
}
/*
* Function: move (Smiley's Basic Function)
* Walk one step depending on the current direction.
*/
void move(){
printf("[Smiley] move\n");
if(blocked()) printErrorAndExit(BLOCKED);
switch(Facing){
case EAST: PosC++; break;
case NORTH: PosR--; break;
case WEST: PosC--; break;
case SOUTH: PosR++; break;
}
//print out the current status
printMapAndWalls();
}
/*
* Function: turnLeft (Smiley's Basic Function)
* Let Smiley turn left!
*/
void turnLeft(){
printf("[Smiley] turnLeft\n");
Facing++;
Facing %= 4;
printMapAndWalls();
}
/*
* Function: putFlash
* Smiley is really cool, he can put flashes on his current spot.
*/
void putFlash(){
printf("[Smiley] putFlash\n");
Map[PosR][PosC] ++;
printMapAndWalls();
}
/*
* Function: pickFlash
* Smiley can also pick up the flashes on his current spot.
* Isn't it cool?
*/
void pickFlash(){
printf("[Smiley] pickFlash\n");
if(Map[PosR][PosC]<=0) printErrorAndExit(PICKED_EMPTY);
Map[PosR][PosC] --;
printMapAndWalls();
}
/*
* Functions: selectRunMode
* Let user to select the program mode. Return 1 is the user chose
* "run mode".
*/
int selectRunMode(){
printf("There are two modes to select, please pick one.\n");
printf("[1] Run the script in the code.\n");
printf("[2] Run the interactive mode to control smiley.\n");
printf(">> ");
int select=0;
if(scanf("%d",&select)!=1){
printErrorAndExit(INVALID_INPUT);
}
return (select==1)?1:0;
}
/*
* Functions: interactiveMode
* Interactive Mode of Smiley Program. User are allowed to control
* Smiley by entering mothed which on the list interactively.
*/
int interactiveMode(){
//output information
printf("Next step for Smiley:\n");
printf("[1] move();\n");
printf("[2] turnLeft();\n");
printf("[3] putFlash();\n");
printf("[4] pickFlash();\n");
printf("[0] Exit Smiley.\n");
//let user choose
int select=0;
printf(">> ");
while(scanf("%d",&select)!=1){
printf("Invalid input, please try again.\n");
}
switch(select){
case 1:
move(); break;
case 2:
turnLeft(); break;
case 3:
putFlash(); break;
case 4:
pickFlash(); break;
case 0:
return 0;
break;
default:
printf("Invalid input, please try agin.\n");
interactiveMode();
}
return 1;
}
/*
* Function: endProgram
* End Program.
*/
void endProgram(){
printf("Thank you for playing with Smiley :)\tGoodbye!\n");
}
//end