-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_show.c
104 lines (98 loc) · 1.94 KB
/
maze_show.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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <string.h>
#define H 4
#define W 6
//maze map
char tiny_maze[H][W] = { "+-+-+",
"| |#|",
"| |",
"+---+" };
void draw ()
{
int i, j;
for (i = 0; i < H; i++)
{
for (j = 0; j < W; j++)
printf ("%c", tiny_maze[i][j]);
printf ("\n");
}
printf ("\n");
}
void refresh(char *program, int length){
int x, y; //Player position
int ox, oy; //Old player position
x = 1;
y = 1;
tiny_maze[y][x]='X';
draw();
int index=0;
while(index<length){
ox = x; //Save old player position
oy = y;
bool break_out_wile = false;
switch (program[index])
{
case 'w':
y--;
break;
case 's':
y++;
break;
case 'a':
x--;
break;
case 'd':
x++;
break;
default:
break_out_wile = true;
break;
}
if (tiny_maze[y][x] != ' ') {
x = ox;
y = oy;
}
system("clear");
tiny_maze[y][x]='X';
draw();
usleep(250000);
index++;
if (break_out_wile == true) break;
}
//char command[1024];
//strncpy(command,program,index);
printf("Command is: %s", program);
printf("and this window will be shut down within 3 seconds");
sleep(3);
}
//argv[1]:program, the first argv in command line.
//argv[2]:filepath, the second argv in command line.
int main(int argc, char *argv[]){
if(argc==2){
refresh(argv[1],sizeof(argv[1])/sizeof(argv[1][0]));
}
else if(argc==3){
char *filepath = argv[2];
FILE *fp;
fp=fopen(filepath, "r");
if(!fp)printf("%s open error", filepath);
fseek(fp,0L,SEEK_END);
int flen = ftell(fp);//get the length of characters
char *p = (char *)malloc(flen+1);
if(p == NULL)//it is a empty file
{
fclose(fp);
printf("it is a empty file.");
return 0;
}
fseek(fp,0L,SEEK_SET);
fread(p,flen,1,fp);
p[flen] = '\0';//set the last one as '\0'
refresh(p,flen);
}
}