-
Notifications
You must be signed in to change notification settings - Fork 5
/
city-building-game.c
144 lines (125 loc) · 3.87 KB
/
city-building-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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Constants
#define MAX_RESOURCES 100
#define MAX_BUILDINGS 10
#define MAX_BUILDING_NAME_LENGTH 20
// Structs
typedef struct
{
int food;
int water;
int power;
} Resources;
typedef struct
{
char name[MAX_BUILDING_NAME_LENGTH];
int cost;
Resources production;
} Building;
// Function prototypes
void display_resources(Resources resources);
void display_buildings(Building *buildings, int num_buildings);
int get_choice(int num_options);
void build_building(Building *building, Resources *resources);
// Main function
int main()
{
// Declare and initialize resources
Resources resources = {10, 10, 10};
// Declare and initialize buildings
Building buildings[MAX_BUILDINGS] = {
{"Farm", 10, {2, 0, 0}},
{"Well", 10, {0, 2, 0}},
{"Power Plant", 20, {0, 0, 3}},
{"House", 5, {0, 0, 0}}};
int num_buildings = 4;
// Main game loop
while (1)
{
// Display resources
printf("\nResources:\n");
display_resources(resources);
// Display available buildings
printf("\nAvailable buildings:\n");
display_buildings(buildings, num_buildings);
// Get user input
printf("\nWhat do you want to do? ");
int choice = get_choice(num_buildings + 1);
// Build a building
if (choice == num_buildings)
{
// Get user input
printf("\nWhich building do you want to build? ");
int building_choice = get_choice(num_buildings);
// Check if resources are sufficient
if (resources.food >= buildings[building_choice].cost &&
resources.water >= buildings[building_choice].cost &&
resources.power >= buildings[building_choice].cost)
{
// Build the building and update resources
build_building(&buildings[building_choice], &resources);
printf("\n%s built!\n", buildings[building_choice].name);
}
else
{
printf("\nNot enough resources to build %s.\n", buildings[building_choice].name);
}
}
// Exit game
else if (choice == num_buildings + 1)
{
break;
}
// Display building info
else
{
printf("\n%s:\n", buildings[choice].name);
printf("Cost: %d\n", buildings[choice].cost);
printf("Production:\n");
display_resources(buildings[choice].production);
}
}
return 0;
}
// Function to display the current resources
void display_resources(Resources resources)
{
printf("Food: %d\n", resources.food);
printf("Water: %d\n", resources.water);
printf("Power: %d\n", resources.power);
}
// Function to display the available buildings
void display_buildings(Building *buildings, int num_buildings)
{
for (int i = 0; i < num_buildings; i++)
{
printf("%d. %s\n", i + 1, buildings[i].name);
}
printf("%d. Build a new building\n", num_buildings + 1);
printf("%d. Exit game\n", num_buildings + 2);
}
// Function to get user choice from a list of options
int get_choice(int num_options)
{
int choice;
scanf("%d", &choice);
// Check if choice is valid
while (choice < 1 || choice > num_options)
{
printf("Invalid choice. Please enter a number between 1 and %d: ", num_options);
scanf("%d", &choice);
}
return choice - 1;
}
// Function to build a new building and update resources
void build_building(Building *building, Resources *resources)
{
resources->food -= building->cost;
resources->water -= building->cost;
resources->power -= building->cost;
resources->food += building->production.food;
resources->water += building->production.water;
resources->power += building->production.power;
}