-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
355 lines (303 loc) · 10.5 KB
/
game.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
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#define MAX_INPUT 50
#define MAX_ITEMS 10
#define SCREEN_WIDTH 80
#define SCREEN_HEIGHT 25
#define MAP_WIDTH 11
#define MAP_HEIGHT 11
typedef struct {
char description[100];
int north, south, east, west;
char item[20];
} PizzeriaRoom;
PizzeriaRoom pizzeriaRooms[] = {
{"You are in the Kitchen, full of delicious smells.", 1, -1, 2, -1, "cheese"},
{"You enter the cool Storage Room, where pepperoni is kept.", -1, 0, -1, -1, "pepperoni"},
{"You walk into the Dining Area with tables set.", -1, 3, -1, 0, "plates"},
{"This is the Oven Room, where pizzas are baked to perfection.", 2, -1, -1, -1, "pizza dough"}
};
int currentLocation = 0;
char ingredientsBag[MAX_ITEMS][20];
int itemCount = 0;
void clearScreen() {
clrscr(); // Use Turbo C's built-in screen clear function
}
void gotoxy(int x, int y) {
union REGS regs;
regs.h.ah = 0x02;
regs.h.bh = 0x00;
regs.h.dh = y;
regs.h.dl = x;
int86(0x10, ®s, ®s);
}
void drawBox() {
int i; // Declaration moved to the top
gotoxy(0, 0);
putch(218); // Top-left corner
for (i = 1; i < SCREEN_WIDTH - 1; i++) putch(196); // Top border
putch(191); // Top-right corner
for (i = 1; i < SCREEN_HEIGHT - 1; i++) {
gotoxy(0, i);
putch(179); // Left border
gotoxy(SCREEN_WIDTH - 1, i);
putch(179); // Right border
}
gotoxy(0, SCREEN_HEIGHT - 1);
putch(192); // Bottom-left corner
for (i = 1; i < SCREEN_WIDTH - 1; i++) putch(196); // Bottom border
putch(217); // Bottom-right corner
}
void printWrapped(int x, int y, const char* text, int width) {
int curX = x, curY = y;
int len = strlen(text);
int i = 0, j; // Declaration moved to the top
while (i < len) {
j = 0;
while (j < width && i + j < len && text[i + j] != '\n') j++;
if (i + j < len && text[i + j] != '\n') {
while (j > 0 && text[i + j - 1] != ' ') j--;
}
gotoxy(curX, curY);
printf("%.*s", j, &text[i]);
i += j;
if (text[i] == '\n' || text[i] == ' ') i++;
curY++;
}
}
void drawCompass() {
int compassX = SCREEN_WIDTH - 10;
int compassY = SCREEN_HEIGHT - 8;
// North direction
gotoxy(compassX + 3, compassY);
if (pizzeriaRooms[currentLocation].north != -1) {
textcolor(YELLOW);
putch('N');
} else {
textcolor(RED);
putch('N');
}
// South direction
gotoxy(compassX + 3, compassY + 4);
if (pizzeriaRooms[currentLocation].south != -1) {
textcolor(YELLOW);
putch('S');
} else {
textcolor(RED);
putch('S');
}
// East direction
gotoxy(compassX + 6, compassY + 2);
if (pizzeriaRooms[currentLocation].east != -1) {
textcolor(YELLOW);
putch('E');
} else {
textcolor(RED);
putch('E');
}
// West direction
gotoxy(compassX, compassY + 2);
if (pizzeriaRooms[currentLocation].west != -1) {
textcolor(YELLOW);
putch('W');
} else {
textcolor(RED);
putch('W');
}
// Connectors for the compass (fixed lines)
textcolor(WHITE); // Set connectors back to white
gotoxy(compassX + 3, compassY + 1); putch('|');
gotoxy(compassX + 3, compassY + 3); putch('|');
gotoxy(compassX + 1, compassY + 2); putch('-');
gotoxy(compassX + 5, compassY + 2); putch('-');
gotoxy(compassX + 3, compassY + 2); putch('+'); // Center of the compass
}
void displayCommands() {
int commandsX = SCREEN_WIDTH - 20;
int commandsY = 2;
gotoxy(commandsX, commandsY);
printf("Commands:");
gotoxy(commandsX, commandsY + 1);
printf(" north - Move north");
gotoxy(commandsX, commandsY + 2);
printf(" south - Move south");
gotoxy(commandsX, commandsY + 3);
printf(" east - Move east");
gotoxy(commandsX, commandsY + 4);
printf(" west - Move west");
gotoxy(commandsX, commandsY + 5);
printf(" take item - Collect item");
gotoxy(commandsX, commandsY + 6);
printf(" quit - Exit game");
}
void displayBackpack() {
int i; // Declare the loop variable here
gotoxy(2, 12); // Display backpack below the current item
printf("Backpack: ");
if (itemCount == 0) {
printf("Empty");
} else {
for (i = 0; i < itemCount; i++) { // Properly declare and use 'i'
if (i > 0) printf(", ");
printf("%s", ingredientsBag[i]);
}
}
}
void drawMap() {
// Map coordinates
int mapX = (SCREEN_WIDTH - 11) / 2; // Center horizontally
int mapY = SCREEN_HEIGHT - 8; // Position closer to the bottom
// Drawing the map header
gotoxy(mapX, mapY);
textcolor(WHITE);
printf("Pizzeria Map:");
// Row 1: Kitchen (Room 0)
gotoxy(mapX + 4, mapY + 1); // Centered for Kitchen
if (currentLocation == 0) {
textcolor(GREEN); // Player's current position
printf("K");
} else if (pizzeriaRooms[0].north == currentLocation || pizzeriaRooms[0].south == currentLocation ||
pizzeriaRooms[0].east == currentLocation || pizzeriaRooms[0].west == currentLocation) {
textcolor(YELLOW); // Accessible room (adjacent to current)
printf("K");
} else {
textcolor(WHITE); // Inaccessible room
printf("K");
}
// Row 2: Storage Room (Room 1) and Dining Area (Room 2)
gotoxy(mapX, mapY + 2); // Left for Storage Room
if (currentLocation == 1) {
textcolor(GREEN); // Player's current position
printf("S");
} else if (pizzeriaRooms[1].north == currentLocation || pizzeriaRooms[1].south == currentLocation ||
pizzeriaRooms[1].east == currentLocation || pizzeriaRooms[1].west == currentLocation) {
textcolor(YELLOW); // Accessible room
printf("S");
} else {
textcolor(WHITE); // Inaccessible room
printf("S");
}
gotoxy(mapX + 8, mapY + 2); // Right for Dining Area
if (currentLocation == 2) {
textcolor(GREEN); // Player's current position
printf("D");
} else if (pizzeriaRooms[2].north == currentLocation || pizzeriaRooms[2].south == currentLocation ||
pizzeriaRooms[2].east == currentLocation || pizzeriaRooms[2].west == currentLocation) {
textcolor(YELLOW); // Accessible room
printf("D");
} else {
textcolor(WHITE); // Inaccessible room
printf("D");
}
// Row 3: Oven Room (Room 3)
gotoxy(mapX + 4, mapY + 3); // Centered for Oven Room
if (currentLocation == 3) {
textcolor(GREEN); // Player's current position
printf("O");
} else if (pizzeriaRooms[3].north == currentLocation || pizzeriaRooms[3].south == currentLocation ||
pizzeriaRooms[3].east == currentLocation || pizzeriaRooms[3].west == currentLocation) {
textcolor(YELLOW); // Accessible room
printf("O");
} else {
textcolor(WHITE); // Inaccessible room
printf("O");
}
// Reset to default color after drawing the map
textcolor(WHITE);
}
void displayRoom() {
clearScreen();
drawBox();
// Display room description
gotoxy(2, 6);
printf("You are in: ");
printWrapped(2, 7, pizzeriaRooms[currentLocation].description, SCREEN_WIDTH - 4);
// Display current item in the room
gotoxy(2, 10);
if (strcmp(pizzeriaRooms[currentLocation].item, "none") != 0) {
printf("Current Item: %s", pizzeriaRooms[currentLocation].item);
} else {
printf("Current Item: None");
}
// Display backpack contents between current item and command input
displayBackpack();
// Draw compass and commands
drawCompass();
displayCommands();
drawMap(); // Draw the map at the bottom
}
void takeItem() {
if (strcmp(pizzeriaRooms[currentLocation].item, "none") != 0) {
if (itemCount < MAX_ITEMS) {
strcpy(ingredientsBag[itemCount], pizzeriaRooms[currentLocation].item);
itemCount++;
printf("\nYou took %s!\n", pizzeriaRooms[currentLocation].item);
strcpy(pizzeriaRooms[currentLocation].item, "none"); // Remove the item from the room
} else {
printf("\nBackpack is full!\n");
}
} else {
printf("\nThere is no item to take!\n");
}
}
void move(char direction) {
int newLocation = -1;
switch (direction) {
case 'n':
newLocation = pizzeriaRooms[currentLocation].north;
break;
case 's':
newLocation = pizzeriaRooms[currentLocation].south;
break;
case 'e':
newLocation = pizzeriaRooms[currentLocation].east;
break;
case 'w':
newLocation = pizzeriaRooms[currentLocation].west;
break;
}
if (newLocation != -1) {
currentLocation = newLocation;
printf("You move %c.\n", direction);
} else {
printf("You can't go that way!\n");
}
}
void customDelay(int milliseconds) {
delay(milliseconds); // Adjust this if needed for your environment
}
void checkWinCondition() {
// Implement win condition logic here
}
int main() {
char input[MAX_INPUT];
while (1) {
displayRoom();
gotoxy(2, SCREEN_HEIGHT - 2); // Adjust if necessary based on map position
printf("Enter command: ");
fgets(input, MAX_INPUT, stdin);
input[strcspn(input, "\n")] = '\0';
if (strcmp(input, "quit") == 0) {
break;
} else if (strcmp(input, "north") == 0) {
move('n');
} else if (strcmp(input, "south") == 0) {
move('s');
} else if (strcmp(input, "east") == 0) {
move('e');
} else if (strcmp(input, "west") == 0) {
move('w');
} else if (strcmp(input, "take item") == 0) {
takeItem();
} else if (strcmp(input, "show backpack") == 0) {
displayBackpack();
} else {
printf("Unknown command. Please try again.\n");
}
checkWinCondition();
customDelay(1000); // 1-second delay between screen updates
}
return 0;
}