I tried making code to simulate boss fights. Firstly initialize the array of struct, making pointer out of it, and passing it into function.
#include <stdio.h>
struct machine_gun{
char name[10];
int rounds;
int damage;
};
enum states{
Tidak,
Ya
};
void battle(struct machine_gun* wpn);
int main(){
struct machine_gun weapon_list[2] = {
{"Holger", 400, 20},
{"M4LMG", 320, 25}
};
struct machine_gun *ptr = NULL;
ptr = &weapon_list;
int choice;
printf("Boss has appeared! Enter 1 to fight the boss, 0 to run\n"); scanf("%d", &choice);
if(choice == 1){
battle(&ptr);
}
else if(choice != 1){
printf("You ran away.");
}
}
void battle (struct machine_gun* wpn){
int choice;
int weapon_choice;
int boss_hp = 1000;
enum states state;
printf("###Weapon Lists###\n");
for(int i = 0; i < 2; i++){
printf("Name: %s\n", wpn[i]->name);
printf(" Rounds: %d\n", wpn[i]->rounds);
printf(" Damage: %d\n", wpn[i]->damage);
}
printf("\nBoss HP is 1000, are you sure you want to fight?\n1. Yes\n2. No\n"); scanf("%d", &choice);
if(choice == 1){
enum states state = Ya;
}
else{
enum states state = Tidak;
}
switch(state){
case Tidak:
printf("Game Over!\nYou've been killed by the Boss.");
case Ya:
printf("Choose your weapon!\n");
printf("1. %s with %d ammo and does %d damages\n", wpn[0]->name, wpn[0]->rounds, wpn[0]->damage);
printf("2. %s with %d ammo and does %d damages\n", wpn[1]->name, wpn[1]->rounds, wpn[1]->damage);
scanf("%d", &weapon_choice);
}
if(weapon_choice == 1){
printf("You shot the boss with %d bullets\nCongratulation! You killed the boss, thanks for playing!\n", (1000/wpn[0]->damage));
wpn[0]->rounds = wpn[0]->rounds - (1000/wpn[0]->damage);
printf("Your ammo remains: %d", wpn[0]->rounds);
}
else if(weapon_choice == 2){
printf("You shot the boss with %d bullets\nCongratulation! You killed the boss, thanks for playing!\n", (1000/wpn[1]->damage));
wpn[1]->rounds = wpn[1]->rounds - (1000/wpn[1]->damage);
printf("Your ammo remains: %d", wpn[1]->rounds);
}
}
I tried to code like this, i know i messed up somewhere when making the pointers. Anyways, where did i do wrong? Is it because i didnt use typedef? Been trying to search the similar problem in stackoverflow but i just cant understand it properly