r/C_Programming • u/Adorable_Eggplant792 • Jun 27 '26
Review Created a battle simulator
I am a beginner at C and I learn best by creating mini projects. I created a battle simulator to practice pointers and I wanted to know if this project properly taught me pointers and if not I would like some project ideas to keep practicing. I also would like projects to practice malloc as well. I created three characters that would battle each other. Everyone has a 80% hit chance, the damage you deal depends on your strength. I’m not the best at math so I wanted to keep it very simple. Anyways here is my code:
`
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Creating a structure that contains the information for a player
typedef struct {
char *name;
int hp;
int strength;
} Player;
// Creating a function to handle battle
void battle(Player *opponent1, Player *opponent2){
// Decide what players hits first
int roll = rand() % 2;
Player *opponents[2] = {opponent1, opponent2};
Player *attacker = opponents[roll];
Player *defender = opponents[1 - roll];
do{
int hitChance = (rand() % 100) + 1;
int defence = (rand() % 5) + 1;
printf("%s turn!\n", attacker->name);
if(hitChance < 81){
int damage = (attacker->strength + 10) - defence;
defender->hp = defender->hp - damage;
if(defender->hp < 0){
defender->hp = 0;
}
printf("%s damage done: %d\n", attacker->name, damage);
printf("%s HP: %d\n%s HP: %d\n", opponent1->name, opponent1->hp, opponent2->name, opponent2->hp);
}
else{
printf("%s missed!\n", attacker->name);
}
if(attacker == opponent1){
defender = opponent1;
attacker = opponent2;
} else{
defender = opponent2;
attacker = opponent1;
}
}while(opponent1->hp > 0 && opponent2->hp > 0);
if(attacker->hp > 0){
printf("%s wins!\n", attacker->name);
} else{
printf("%s wins!\n", defender->name);
}
}
int main(){
srand(time(NULL));
// Creating premade characters using Player struct
Player galaxyChar = {.name = "Galaxy", .hp = 100, .strength = 10};
Player termixChar = {.name = "Termix", .hp = 100, .strength = 15};
Player valChar = {.name = "Val", .hp = 100, .strength = 8};
// Creating pointer variables for premade characters
Player *galaxyPoint = &galaxyChar;
Player *termixPoint = &termixChar;
Player *valPoint = &valChar;
// Storing pointer variables in array
// This is an array of pointers to Player
Player *premadeCharacters[3] = {galaxyPoint, termixPoint, valPoint};
// Printing out contents of premadeCharacters for testing
//for(int i = 0; i < sizeof(premadeCharacters)/sizeof(premadeCharacters[0]); i++){
//printf("%s's Memory Address: %p\n", premadeCharacters[i]->name, premadeCharacters[i]);
//}
battle(valPoint, termixPoint);
}
`
6
u/Direct_Chemistry_179 Jun 27 '26
I would suggest code review stack exchange website because it’s dedicated to code reviews.
Also if you go into your ide and put 4 spaces before every line it should format correctly.
5
u/TheOtherBorgCube Jun 27 '26
Formatted.
In your function, your use of pointers is fine.
In
main, the pointer variables add no value. You could have just done this.Actual pointer variables come in when you start using malloc, eg
And +1 for also calling
srandexactly once at the start ofmain.1
u/Adorable_Eggplant792 Jun 27 '26
Not sure why it’s not formatting? I’m on mobile.
-12
u/aalmkainzi Jun 27 '26
Ai slop
-3
u/stianhoiland Jun 27 '26
Did you use AI to create this?