-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgptgame.c
136 lines (119 loc) · 3.85 KB
/
chatgptgame.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
// Player structure
typedef struct {
char name[50];
int health;
int gold;
int attackPower;
} Player;
// Monster structure
typedef struct {
char name[50];
int health;
int attackPower;
} Monster;
// Function prototypes
void battle(Player *player, Monster monster);
Monster generateMonster();
void explore(Player *player);
void showStats(Player *player);
int main() {
Player player;
int choice;
// Initialize random number generator
srand(time(0));
printf("Enter your name, adventurer: ");
fgets(player.name, 50, stdin);
player.name[strcspn(player.name, "\n")] = '\0'; // Remove newline character
player.health = 100;
player.gold = 0;
player.attackPower = 10;
printf("Welcome, %s! Your adventure begins now...\n", player.name);
while (player.health > 0) {
printf("\n1. Explore\n2. View Stats\n3. Rest (Quit Game)\n");
printf("Choose an action: ");
scanf("%d", &choice);
switch (choice) {
case 1:
explore(&player);
break;
case 2:
showStats(&player);
break;
case 3:
printf("You rest and decide to end your journey.\n");
player.health = 0; // End the game
break;
default:
printf("Invalid choice. Try again.\n");
}
}
printf("Game Over. Thank you for playing, %s!\n", player.name);
return 0;
}
// Function to handle battle
void battle(Player *player, Monster monster) {
printf("\nA wild %s appears!\n", monster.name);
while (player->health > 0 && monster.health > 0) {
printf("\nYour health: %d, %s's health: %d\n", player->health, monster.name, monster.health);
printf("1. Attack\n2. Run\n");
printf("Choose an action: ");
int choice;
scanf("%d", &choice);
if (choice == 1) {
printf("You attack the %s for %d damage!\n", monster.name, player->attackPower);
monster.health -= player->attackPower;
if (monster.health > 0) {
printf("The %s attacks you for %d damage!\n", monster.name, monster.attackPower);
player->health -= monster.attackPower;
} else {
printf("You defeated the %s!\n", monster.name);
int loot = rand() % 20 + 10; // Random gold between 10-30
printf("You found %d gold!\n", loot);
player->gold += loot;
}
} else if (choice == 2) {
printf("You ran away safely.\n");
return;
} else {
printf("Invalid action. The %s attacks you!\n", monster.name);
player->health -= monster.attackPower;
}
}
if (player->health <= 0) {
printf("You were defeated by the %s...\n", monster.name);
}
}
// Function to generate a random monster
Monster generateMonster() {
Monster monsters[] = {
{"Goblin", 30, 5},
{"Orc", 50, 10},
{"Troll", 70, 15}
};
return monsters[rand() % 3]; // Return a random monster
}
// Function to explore
void explore(Player *player) {
printf("\nYou venture into the unknown...\n");
if (rand() % 2 == 0) {
Monster monster = generateMonster();
battle(player, monster);
} else {
int gold = rand() % 50 + 10; // Random gold between 10-60
printf("You find a treasure chest with %d gold!\n", gold);
player->gold += gold;
}
}
// Function to show player stats
void showStats(Player *player) {
printf("\n-- Player Stats --\n");
printf("Name: %s\n", player->name);
printf("Health: %d\n", player->health);
printf("Gold: %d\n", player->gold);
printf("Attack Power: %d\n", player->attackPower);
printf("-------------------\n");
}