forked from lifting-bits/mcsema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.c
157 lines (134 loc) · 3.69 KB
/
Maze.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
/*
* Copyright (c) 2018 Trail of Bits, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* It's a maze!
* Use a,s,d,w to move "through" it.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* Dimensions of the Maze */
enum {
kWidth = 11,
kHeight = 7
};
/* Hard-coded maze */
char maze[kHeight][kWidth] = {
{'+', '-', '+', '-', '-', '-', '+', '-', '-', '-', '+'},
{'|', ' ', '|', ' ', ' ', ' ', ' ', ' ', '|', '#', '|'},
{'|', ' ', '|', ' ', '-', '-', '+', ' ', '|', ' ', '|'},
{'|', ' ', '|', ' ', ' ', ' ', '|', ' ', '|', ' ', '|'},
{'|', ' ', '+', '-', '-', ' ', '|', ' ', '|', ' ', '|'},
{'|', ' ', ' ', ' ', ' ', ' ', '|', ' ', ' ', ' ', '|'},
{'+', '-', '-', '-', '-', '-', '+', '-', '-', '-', '+'},
};
/**
* Draw the maze state in the screen!
*/
void draw(void) {
int i, j;
for (i = 0; i < kHeight; i++) {
for (j = 0; j < kWidth; j++) {
printf("%c", maze[i][j]);
}
printf("\n");
}
printf("\n");
}
enum {
kMaxNumPlayerMoves = 28
};
/**
* The main function
*/
int main(int argc, char *argv[]) {
int x, y; /* Player position */
int ox, oy; /* Old player position */
int i = 0; /* Iteration number */
char program[kMaxNumPlayerMoves];
/* Initial position */
x = 1;
y = 1;
maze[y][x] = 'X';
/* Print some info. */
printf("Maze dimensions: %dx%d\n", kWidth, kHeight);
printf("Player position: %dx%d\n", x, y);
printf("Iteration no. %d\n", i);
printf("Program the player moves with a sequence of 'w', 's', 'a' and 'd'\n");
printf("Try to reach the price(#)!\n");
/* Draw the maze */
draw();
/* Read the directions 'program' to execute... */
read(STDIN_FILENO, program, kMaxNumPlayerMoves);
/* Iterate and run 'program'. */
while (i < kMaxNumPlayerMoves) {
/* Save old player position */
ox = x;
oy = y;
/* Move player position depending on the actual command */
switch (program[i]) {
case 'w':
y--;
break;
case 's':
y++;
break;
case 'a':
x--;
break;
case 'd':
x++;
break;
default:
printf("Wrong command, only w,s,a,d are accepted!)\n");
printf("You lose!\n");
exit(EXIT_FAILURE);
}
/* If hit the price, You Win!! */
if (maze[y][x] == '#') {
printf("You win!\n");
printf("Your solution <%42s>\n", program);
exit(EXIT_SUCCESS);
}
/* If something is wrong do not advance. */
if (maze[y][x] != ' '
&& !((y == 2 && maze[y][x] == '|' && x > 0 && x < kWidth))) {
x = ox;
y = oy;
}
/* Print new maze state and info... */
printf("Player position: %dx%d\n", x, y);
printf("Iteration no. %d. Action: %c. %s\n", i, program[i],
((ox == x && oy == y) ? "Blocked!" : ""));
/* If crashed to a wall! Exit, you lose */
if (ox == x && oy == y) {
printf("You lose\n");
exit(EXIT_FAILURE);
}
/* put the player on the maze... */
maze[y][x] = 'X';
/* draw it */
draw();
/* increment iteration */
i++;
/* me wait to human */
sleep(1);
}
/* You couldn't make it! You lose! */
printf("You lose\n");
return EXIT_FAILURE;
}