Menu
Coddy logo textTech

Game Character System

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 109 of 110.

challenge icon

Challenge

Easy

Let's build a Game Character System that brings together all the OOP concepts you've mastered! You'll create different character classes with unique abilities, stats, and combat mechanics - modeling how characters might work in a real game.

Your system will be organized across six files:

  • character.dart: Create an abstract Character class that serves as the foundation for all character types. Each character has a name (String), private _health (int), attackPower (int), and defense (int). Include a getter for health and these methods:
    • takeDamage(int damage) - reduces health by damage - defense (minimum 1 damage taken, health can't go below 0). Prints name takes X damage! Health: Y
    • bool isAlive() - returns true if health is greater than 0
    • An abstract method String attack() that subclasses implement for their unique attack style
    Override toString() to return name [Class] - HP: health, ATK: attackPower, DEF: defense where Class comes from runtimeType.
  • abilities.dart: Create two mixins that provide special abilities:
    • CanHeal - provides a heal(int amount) method. Since mixins can't directly access private fields, this mixin should define an abstract void restoreHealth(int amount) that the character class implements. The heal() method calls restoreHealth(amount) and prints name heals for amount HP!
    • CanCastSpells - provides int mana (defaults to 100) and a castSpell(String spellName, int manaCost) method that reduces mana if sufficient, prints name casts spellName!, and returns true. If insufficient mana, prints Not enough mana! and returns false.
  • warrior.dart: Create a Warrior class extending Character. Warriors are tanks with high defense. Initialize with 150 health, 20 attack power, and 15 defense. Implement attack() to return name swings a mighty sword!
  • mage.dart: Create a Mage class extending Character and using the CanCastSpells and CanHeal mixins. Mages are fragile but powerful. Initialize with 80 health, 35 attack power, and 5 defense. Implement attack() to return name hurls a fireball!. Implement restoreHealth(int amount) to increase health (capped at 80).
  • archer.dart: Create an Archer class extending Character. Archers are balanced fighters. Initialize with 100 health, 25 attack power, and 10 defense. Implement attack() to return name fires a precise arrow!
  • main.dart: Demonstrate your character system by creating one of each character type: a Warrior named Thorin, a Mage named Gandalf, and an Archer named Legolas. Print each character using toString(). Then simulate a battle sequence:
    1. Print Thorin's attack message
    2. Gandalf takes 30 damage
    3. Gandalf casts Healing Light (costs 25 mana)
    4. Gandalf heals for 20 HP
    5. Print Gandalf's attack message
    6. Thorin takes 40 damage
    7. Print Legolas's attack message
    8. Print whether each character is alive in format: name alive: true/false

Expected output:

Thorin [Warrior] - HP: 150, ATK: 20, DEF: 15
Gandalf [Mage] - HP: 80, ATK: 35, DEF: 5
Legolas [Archer] - HP: 100, ATK: 25, DEF: 10
Thorin swings a mighty sword!
Gandalf takes 25 damage! Health: 55
Gandalf casts Healing Light!
Gandalf heals for 20 HP!
Gandalf hurls a fireball!
Thorin takes 25 damage! Health: 125
Legolas fires a precise arrow!
Thorin alive: true
Gandalf alive: true
Legolas alive: true

Try it yourself

import 'warrior.dart';
import 'mage.dart';
import 'archer.dart';

void main() {
  // TODO: Create a Warrior named "Thorin"

  // TODO: Create a Mage named "Gandalf"

  // TODO: Create an Archer named "Legolas"

  // TODO: Print each character using toString()

  // TODO: Simulate battle sequence:
  // 1. Print Thorin's attack message
  // 2. Gandalf takes 30 damage
  // 3. Gandalf casts "Healing Light" (costs 25 mana)
  // 4. Gandalf heals for 20 HP
  // 5. Print Gandalf's attack message
  // 6. Thorin takes 40 damage
  // 7. Print Legolas's attack message
  // 8. Print whether each character is alive: "name alive: true/false"
}

All lessons in Object Oriented Programming