-
Notifications
You must be signed in to change notification settings - Fork 1
/
ex19.c
253 lines (208 loc) · 5.06 KB
/
ex19.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
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "ex19.h"
#include <assert.h>
int Monster_attack(void *self,int damage)
{
assert(self!=NULL);
Monster *monster=self;
printf("You attack %s!\n",monster->_(description)); //转换为monster->proto.description,这样就可以使用结构体Object的description
monster->hit_points-=damage;
if(monster->hit_points>0){
printf("It is still alive.\n");
return 0;
}
else{
printf("It is dead!\n");
return 1;
}
}
int Monster_init(void *self)
{
assert(self!=NULL);
Monster *monster=self;
monster->hit_points=10; //初始化Monster中的hit_points
return 1;
}
Object MonsterProto={
.init=Monster_init,
.attack=Monster_attack
};
void *Room_move(void *self,Direction direction)
{
assert(self!=NULL);
Room *room=self;
Room *next=NULL; //初始化next指针
if(direction==NORTH && room->north){ //结构体Direction在object.h中
printf("You go north,into:\n");
next=room->north;
}
else if(direction==SOUTH && room->south){
printf("You go south,into:\n");
next=room->south;
}
else if(direction==EAST && room->east){
printf("You go east,into:\n");
next=room->east;
}
else if(direction==WEST && room->west){
printf("You go west,into:\n");
next=room->west;
}
// else{
// printf("不存在这个房间\n");
// exit(1);
// }
if(next){
next->_(describe)(next);
}
// else{
// printf("指针错误\n");
// exit(1);
// }
return next;
}
int Room_attack(void *self,int damage)
{
assert(self!=NULL);
Room *room=self;
Monster *monster=room->bad_guy;
if(monster){
monster->_(attack)(monster,damage);
return 1;
}
else{
printf("You flail in the air at nothing.Idiot.\n");
return 0;
}
}
Object RoomProto={ //创建结构体RoomProto
.move=Room_move,
.attack=Room_attack
};
void *Map_move(void *self,Direction direction)
{
assert(self!=NULL);
Map *map=self;
Room *location=map->location; //map->location实际就是指向结构体Room的指针
Room *next=NULL;
next=location->_(move)(location,direction);
if(next){
map->location=next;
}
// else{
// printf("赋值出错\n");
// exit(1);
// }
return next;
}
int Map_attack(void *self,int damage)
{
assert(self!=NULL);
Map *map=self;
Room *location=map->location;
assert(location!=NULL);
return location->_(attack)(location,damage);
}
int Map_init(void *self)
{
assert(self!=NULL);
Map *map=self;
// make some rooms for a small map
Room *hall=NEW(Room,"The great Hall");
Room *throne=NEW(Room,"The throne room");
Room *arena=NEW(Room,"The arena,with the minotaur");
Room *kitchen=NEW(Room,"Kitchen,you have the knife now");
Room *basement=NEW(Room,"Basement,you find many treasure");
assert(hall!=NULL);
assert(throne!=NULL);
assert(arena!=NULL);
assert(kitchen!=NULL);
// put the bad guy in the arena
arena->bad_guy=NEW(Monster,"The evil minotaur");
assert(arena->bad_guy!=NULL);
// setup the map rooms
hall->north=throne;
throne->west=arena;
throne->east=kitchen;
throne->south=hall;
arena->east=throne;
kitchen->west=throne;
arena->north=basement;
basement->south=arena;
// start the map and the character off in the hall
map->start=hall;
map->location=hall;
return 1;
}
Object MapProto={
.init=Map_init,
.move=Map_move,
.attack=Map_attack
};
int process_input(Map *game) //根据用户输入执行不同的函数
{
assert(game!=NULL);
printf("\n>");
// char ch=getchar(); //getchar读取用户输入的第一个字符,若出错会返回-1
// getchar(); //eat ENTER
//assert(ch=='n' || ch=='s' || ch=='e' || ch=='w' || ch=='a' || ch=='l'); //解决一开始输入fd等等的字符串后出现的各种bug
char *ch;
scanf("%s",ch);
int damage=rand()%4; //rand()生成伪随机数
//assert(damage>0);
if(ch[1]){
printf("只能输入一个字母\n");
}
switch(ch[0]){
case -1:
printf("Giving up?You suck.\n");
return 0;
break;
case 'n':
game->_(move)(game,NORTH);
break;
case 's':
game->_(move)(game,SOUTH);
break;
case 'e':
game->_(move)(game,EAST);
break;
case 'w':
game->_(move)(game,WEST);
break;
case 'a':
game->_(attack)(game,damage);
break;
case 'l':
printf("You can go:\n");
if(game->location->north)
printf("NORTH\n");
if(game->location->south)
printf("SOUTH\n");
if(game->location->east)
printf("EAST\n");
if(game->location->west)
printf("WEST\n");
break;
default:
printf("What?:%s\n",ch);
}
return 1;
}
int main(int argc,char *argv[])
{
// simple way to setup the randomness
srand(time(NULL)); //用当前时间初始化随机数
// make our map to work with
Map *game=NEW(Map,"The Hall of the Minotaur.");
assert(game!=NULL);
printf("You enter the ");
game->location->_(describe)(game->location);
while(process_input(game)){
}
return 0;
}