RPG Character
Part of the Object Oriented Programming section of Coddy's C journey — lesson 58 of 61.
Challenge
EasyLet's build an RPG Character combat system — a classic example that brings together structs, methods, constructors, and object interaction in a clean, modular design.
You'll organize your code across three files:
character.h: Define aCharacterstruct with two integer fields:healthanddamage. Declare the function prototypes for creating a character, performing an attack, checking if a character is alive, and printing a character's status.character.c: Implement your character system:create_character— takes initial health and damage values, allocates a Character on the heap, initializes its fields, and returns a pointerattack— takes two Character pointers (attacker and target), reduces the target's health by the attacker's damageis_alive— takes a const Character pointer and returns 1 if health is greater than 0, otherwise returns 0print_status— takes a const Character pointer and prints the character's health in the formatHealth: Xfree_character— frees the allocated memory
main.c: Create a battle simulation! Read four integers representing: hero's health, hero's damage, enemy's health, and enemy's damage. Create both characters using your constructor. Then simulate combat rounds — the hero attacks first, then the enemy attacks (if still alive). Continue until one character falls. After each round, print both characters' status (hero first, then enemy). When the battle ends, print eitherHero wins!orEnemy wins!depending on who survived. Don't forget to free both characters.
Your program will receive four inputs:
- Hero's starting health
- Hero's damage per attack
- Enemy's starting health
- Enemy's damage per attack
Example output when inputs are 50, 20, 30, 15:
Health: 35
Health: 10
Health: 20
Health: -10
Hero wins!Example output when inputs are 20, 5, 100, 25:
Health: -5
Health: 95
Enemy wins!Remember to use include guards in your header file. The attack function demonstrates how one object can modify another's internal state — a key concept in object-oriented design. Use const pointers appropriately for functions that only read data.
Try it yourself
#include <stdio.h>
#include "character.h"
int main() {
// Read input values
int hero_health, hero_damage, enemy_health, enemy_damage;
scanf("%d", &hero_health);
scanf("%d", &hero_damage);
scanf("%d", &enemy_health);
scanf("%d", &enemy_damage);
// TODO: Create hero and enemy characters using create_character
// TODO: Simulate combat rounds
// - Hero attacks first
// - Enemy attacks if still alive
// - Print both characters' status after each round (hero first, then enemy)
// - Continue until one character falls
// TODO: Print winner ("Hero wins!" or "Enemy wins!")
// TODO: Free both characters
return 0;
}
All lessons in Object Oriented Programming
1Modular Programming Basics
Header FilesInclude GuardsSource FilesStatic FunctionsRecap: Modular Calculator4Encapsulation
Opaque Pointers ConceptDefining Opaque StructsGetters and SettersValidation in SettersRecap: Secret Box2Objects and Methods
Structs as ObjectsThe 'Self' PointerConst CorrectnessPointer vs ValueHelper MethodsRecap: Point Manager5Project: Simple Bank Account
Project SetupImplementation of Account3Object Lifecycle
Constructor PatternDestructor PatternStack InitializationDeep CopyRecap: String Wrapper6Inheritance via Composition
Struct EmbeddingThe First Member RuleAccessing Parent MembersUpcastingRecap: Shape Hierarchy