-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileName.cpp
More file actions
399 lines (352 loc) · 12.9 KB
/
Copy pathFileName.cpp
File metadata and controls
399 lines (352 loc) · 12.9 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
//constantlar
#define MAX_ITEMS 5
#define MAX_DESC 256
#define MAX_ROOMS 4
#define MAX_CMD 50
#define MAX_SAVES 10
// Structs for Game Entities
typedef struct Item {
char name[MAX_DESC];
char description[MAX_DESC];
int health_bonus; // Oyuncunun saðlýðýný artýrýr
int strength_bonus; // Oyuncunun gücünü artýrýr
} Item;
typedef struct Creature {
char name[MAX_DESC];
int health;
int attack;
char weakness[MAX_DESC];
} Creature;
typedef struct Room {
char name[MAX_DESC];
char description[MAX_DESC];
Creature* creature;
Item* item;
struct Room* connections[4]; // [0] = north, [1] = east, [2] = south, [3] = west
} Room;
typedef struct Player {
char name[MAX_DESC];
int health;
int strength;
Item inventory[MAX_ITEMS];
int inventory_count;
Room* current_room;
} Player;
// Directions enum for readability
typedef enum { NORTH = 0, EAST, SOUTH, WEST } Directions;
// Game saving and loading
typedef struct Save {
char filepath[MAX_DESC]; // Kaydedilen oyunun dosya yolu
} Save;
// Function prototypes
void show_menu();
void initialize_game(Player* player, Room rooms[], Save saves[], int* save_count);
void describe_room(Room* room);
void handle_command(Player* player, Room rooms[], Save saves[], int* save_count, char* command); void move_player(Player* player, int direction);
void look(Player* player);
void pickup_item(Player* player);
void show_inventory(Player* player);
void attack_creature(Player* player);
void save_game(Player* player, Room rooms[], const char* filepath);
void load_game(Player* player, Room rooms[], const char* filepath);
void free_resources(Room rooms[], int num_rooms);
void list_saved_games(Save saves[], int save_count);
void apply_item_effect(Player* player, Item* item);
void move_player(Player* player, int direction);
// Main Function
int main() {
Player player;
Room rooms[MAX_ROOMS];
Save saves[MAX_SAVES];
int save_count = 0;
char command[MAX_CMD];
int menu_choice;
bool game_running = true;
while (game_running) {
show_menu();
if (scanf_s("%d", &menu_choice) != 1) {
printf("Input error!\n");
continue; // Hatalý giriþte döngüyü devam ettir
}
getchar(); // Yeni satýr karakterini al
switch (menu_choice) {
case 1:
initialize_game(&player, rooms, saves, &save_count);
printf("WELCOME TO THE DUNGEON GAME!\n");
describe_room(player.current_room);
while (player.health > 0) {
printf("\n> ");
fgets(command, MAX_CMD, stdin);
command[strcspn(command, "\n")] = 0; // Yeni satýrý sil
if (strcmp(command, "exit") == 0) {
printf("Thanks for playing!\n");
game_running = false;
break;
}
handle_command(&player, rooms, saves, &save_count, command);
}
free_resources(rooms, MAX_ROOMS);
break;
case 2:
load_game(&player, rooms, "savefile.txt");
printf("Game loaded successfully!\n");
describe_room(player.current_room);
break;
case 3:
printf("Instructions:\n");
printf("- Use 'move <direction>' to move between rooms (north, south, east, west).\n");
printf("- Use 'look' to look around the room.\n");
printf("- Use 'pickup' to pick up items in the room.\n");
printf("- Use 'inventory' to view your inventory.\n");
printf("- Use 'attack' to fight creatures.\n");
printf("- Use 'save' to save your progress.\n");
printf("- Use 'exit' to quit the game.\n");
break;
case 4:
printf("Exiting the game. Goodbye!\n");
game_running = false;
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
}
return 0;
}
// Display the game menu
void show_menu() {
printf("\n==== Dungeon Adventure Menu ==== \n");
printf("1. Start New Game\n");
printf("2. Load Game\n");
printf("3. Help\n");
printf("4. Exit\n");
printf("Choose an option: ");
}
// initialize the game setup
// Zindanda yer alacak her oda için özellikler
void initialize_game(Player* player, Room rooms[], Save saves[], int* save_count) {
// Player setup
strcpy_s(player->name, "Hero");
player->health = 100;
player->strength = 10;
player->inventory_count = 0;
// Ateþ Odasý
strcpy_s(rooms[0].name, "Fire Room");
strcpy_s(rooms[0].description, "A blazing room with walls of fire. The heat is intense.");
rooms[0].creature = (Creature*)malloc(sizeof(Creature));
strcpy_s(rooms[0].creature->name, "Fire Elemental");
rooms[0].creature->health = 80;
rooms[0].creature->attack = 20;
strcpy_s(rooms[0].creature->weakness, "Water");
rooms[0].item = NULL;
// Hava Odasý
strcpy_s(rooms[1].name, "Air Room");
strcpy_s(rooms[1].description, "The air is swirling around you with intense winds.");
rooms[1].creature = (Creature*)malloc(sizeof(Creature));
strcpy_s(rooms[1].creature->name, "Air Elemental");
rooms[1].creature->health = 70;
rooms[1].creature->attack = 15;
strcpy_s(rooms[1].creature->weakness, "Earth");
rooms[1].item = NULL;
// Su Odasý
strcpy_s(rooms[2].name, "Water Room");
strcpy_s(rooms[2].description, "A cool room with water flowing from the walls.");
rooms[2].creature = (Creature*)malloc(sizeof(Creature));
strcpy_s(rooms[2].creature->name, "Water Elemental");
rooms[2].creature->health = 90;
rooms[2].creature->attack = 18;
strcpy_s(rooms[2].creature->weakness, "Fire");
rooms[2].item = NULL;
// Toprak Odasý
strcpy_s(rooms[3].name, "Earth Room");
strcpy_s(rooms[3].description, "The room feels heavy with the scent of soil and stone.");
rooms[3].creature = (Creature*)malloc(sizeof(Creature));
strcpy_s(rooms[3].creature->name, "Earth Elemental");
rooms[3].creature->health = 100;
rooms[3].creature->attack = 25;
strcpy_s(rooms[3].creature->weakness, "Air");
rooms[3].item = NULL;
// Odalarýn Baðlantýlarý
rooms[0].connections[NORTH] = &rooms[1]; // Ateþ Odasý -> Hava Odasý
rooms[1].connections[NORTH] = &rooms[2]; // Hava Odasý -> Su Odasý
rooms[2].connections[NORTH] = &rooms[3]; // Su Odasý -> Toprak Odasý
rooms[3].connections[NORTH] = NULL; // Toprak Odasý'ndan çýkýþ yok, oyun bitiyor
// Baþlangýç odasý
player->current_room = &rooms[0];
}
// Free dynamically allocated resources
void free_resources(Room rooms[], int num_rooms) {
for (int i = 0; i < num_rooms; i++) {
if (rooms[i].creature) {
free(rooms[i].creature);
rooms[i].creature = NULL;
}
if (rooms[i].item) {
free(rooms[i].item);
rooms[i].item = NULL;
}
}
}
// Show available saved games
void list_saved_games(Save saves[], int save_count) {
printf("Saved games:\n");
for (int i = 0; i < save_count; i++) {
printf("%d. %s\n", i + 1, saves[i].filepath);
}
}
// Describe the current room
void describe_room(Room* room) {
printf("\nYou are in %s. %s\n", room->name, room->description);
if (room->creature) {
printf("A %s is lurking here with %d health!\n", room->creature->name, room->creature->health);
}
else {
printf("The room is clear, no enemies here!\n");
}
}
// Handle player commands
void handle_command(Player* player, Room rooms[], Save saves[], int* save_count, char* command) {
if (strncmp(command, "move", 4) == 0) {
if (strstr(command, "north")) move_player(player, NORTH);
else if (strstr(command, "south")) move_player(player, SOUTH);
else if (strstr(command, "east")) move_player(player, EAST);
else if (strstr(command, "west")) move_player(player, WEST);
else printf("Invalid direction!\n");
}
else if (strcmp(command, "look") == 0) {
look(player);
}
else if (strcmp(command, "inventory") == 0) {
show_inventory(player);
}
else if (strncmp(command, "pickup", 6) == 0) {
pickup_item(player);
}
else if (strcmp(command, "attack") == 0) {
attack_creature(player);
}
else if (strncmp(command, "save", 4) == 0) {
save_game(player, rooms, "savefile.txt");
}
else if (strncmp(command, "load", 4) == 0) {
load_game(player, rooms, "savefile.txt");
}
else if (strcmp(command, "list") == 0) {
list_saved_games(saves, *save_count);
}
else {
printf("Unknown command!\n");
}
}
// Command implementations
void move_player(Player* player, int direction) {
if (player->current_room->connections[direction]) {
player->current_room = player->current_room->connections[direction];
describe_room(player->current_room);
}
else {
printf("You can't move in that direction!\n");
}
}
void look(Player* player) {
describe_room(player->current_room);
}
void pickup_item(Player* player) {
if (player->current_room->item) {
if (player->inventory_count < MAX_ITEMS) {
player->inventory[player->inventory_count++] = *player->current_room->item;
printf("You picked up %s!\n", player->current_room->item->name);
apply_item_effect(player, player->current_room->item);
free(player->current_room->item);
player->current_room->item = NULL;
}
else {
printf("Your inventory is full!\n");
}
}
else {
printf("There are no items here.\n");
}
}
void show_inventory(Player* player) {
printf("Your inventory:\n");
for (int i = 0; i < player->inventory_count; i++) {
printf("%d. %s\n", i + 1, player->inventory[i].name);
}
}
void apply_item_effect(Player* player, Item* item) {
player->health += item->health_bonus;
player->strength += item->strength_bonus;
printf("Item effects applied: Health +%d, Strength +%d\n", item->health_bonus, item->strength_bonus);
}
void attack_creature(Player* player) {
Creature* creature = player->current_room->creature;
if (creature) {
// Yaratýðý saldýrarak zayýflatýyoruz
creature->health -= player->strength;
printf("You attacked the %s! It has %d health left.\n", creature->name, creature->health);
// Eðer yaratýk öldüyse
if (creature->health <= 0) {
printf("You defeated the %s!\n", creature->name);
free(creature);
player->current_room->creature = NULL;
// Eðer odadaki yaratýk öldü ise bir sonraki odaya geç
if (player->current_room->connections[NORTH]) {
player->current_room = player->current_room->connections[NORTH];
describe_room(player->current_room);
}
else {
printf("You've defeated the Earth Elemental! You've completed the dungeon!\n");
player->health = 0; // Oyunun bitmesi için oyuncuyu öldürüyoruz
}
}
else {
// Yaratýðýn saldýrýsý
player->health -= creature->attack;
printf("The %s attacked you! You have %d health left.\n", creature->name, player->health);
}
}
else {
printf("There's nothing to attack here!\n");
}
}
// Save game state to a file
void save_game(Player* player, Room rooms[], const char* filepath) {
FILE* file;
errno_t err = fopen_s(&file, filepath, "w"); // fopen_s kullanýmý
if (err != 0 || file == NULL) {
printf("Failed to save game!\n");
return;
}
// Oyuncu bilgilerini dosyaya yaz
fprintf(file, "%s %d %d %d\n", player->name, player->health, player->strength, player->inventory_count);
// Envanter bilgilerini dosyaya yaz
for (int i = 0; i < player->inventory_count; i++) {
fprintf(file, "%s %s\n", player->inventory[i].name, player->inventory[i].description);
}
fclose(file);
printf("Game saved!\n");
}
// Load game state from a file
void load_game(Player* player, Room rooms[], const char* filepath) {
FILE* file;
errno_t err = fopen_s(&file, filepath, "r"); // fopen_s kullanýmý
if (err != 0 || file == NULL) {
printf("Failed to load game!\n");
return;
}
// Oyuncu bilgilerini dosyadan oku
fscanf_s(file, "%s %d %d %d\n", player->name, (unsigned int)sizeof(player->name),
&player->health, &player->strength, &player->inventory_count);
// Envanter bilgilerini dosyadan oku
for (int i = 0; i < player->inventory_count; i++) {
fscanf_s(file, "%s %s\n", player->inventory[i].name, (unsigned int)sizeof(player->inventory[i].name),
player->inventory[i].description, (unsigned int)sizeof(player->inventory[i].description));
}
fclose(file);
printf("Game loaded!\n");
}