Menu
Coddy logo textTech

RPG Character

Part of the Object Oriented Programming section of Coddy's C journey — lesson 58 of 61.

challenge icon

Challenge

Easy

Let'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 a Character struct with two integer fields: health and damage. 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 pointer
    • attack — takes two Character pointers (attacker and target), reduces the target's health by the attacker's damage
    • is_alive — takes a const Character pointer and returns 1 if health is greater than 0, otherwise returns 0
    • print_status — takes a const Character pointer and prints the character's health in the format Health: X
    • free_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 either Hero wins! or Enemy wins! depending on who survived. Don't forget to free both characters.

Your program will receive four inputs:

  1. Hero's starting health
  2. Hero's damage per attack
  3. Enemy's starting health
  4. 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