Game Character System
Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 109 of 110.
Challenge
EasyLet'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 abstractCharacterclass that serves as the foundation for all character types. Each character has aname(String), private_health(int),attackPower(int), anddefense(int). Include a getter for health and these methods:takeDamage(int damage)- reduces health bydamage - defense(minimum 1 damage taken, health can't go below 0). Printsname takes X damage! Health: Ybool isAlive()- returns true if health is greater than 0- An abstract method
String attack()that subclasses implement for their unique attack style
toString()to returnname [Class] - HP: health, ATK: attackPower, DEF: defensewhere Class comes fromruntimeType.abilities.dart: Create two mixins that provide special abilities:CanHeal- provides aheal(int amount)method. Since mixins can't directly access private fields, this mixin should define an abstractvoid restoreHealth(int amount)that the character class implements. Theheal()method callsrestoreHealth(amount)and printsname heals for amount HP!CanCastSpells- providesint mana(defaults to 100) and acastSpell(String spellName, int manaCost)method that reduces mana if sufficient, printsname casts spellName!, and returns true. If insufficient mana, printsNot enough mana!and returns false.
warrior.dart: Create aWarriorclass extendingCharacter. Warriors are tanks with high defense. Initialize with 150 health, 20 attack power, and 15 defense. Implementattack()to returnname swings a mighty sword!mage.dart: Create aMageclass extendingCharacterand using theCanCastSpellsandCanHealmixins. Mages are fragile but powerful. Initialize with 80 health, 35 attack power, and 5 defense. Implementattack()to returnname hurls a fireball!. ImplementrestoreHealth(int amount)to increase health (capped at 80).archer.dart: Create anArcherclass extendingCharacter. Archers are balanced fighters. Initialize with 100 health, 25 attack power, and 10 defense. Implementattack()to returnname fires a precise arrow!main.dart: Demonstrate your character system by creating one of each character type: a Warrior namedThorin, a Mage namedGandalf, and an Archer namedLegolas. Print each character usingtoString(). Then simulate a battle sequence:- Print Thorin's attack message
- Gandalf takes 30 damage
- Gandalf casts
Healing Light(costs 25 mana) - Gandalf heals for 20 HP
- Print Gandalf's attack message
- Thorin takes 40 damage
- Print Legolas's attack message
- 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: trueTry 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
1Fundamentals of OOP
External FilesLibraries & ImportsIntroduction to OOPClasses vs ObjectsThe this KeywordMethodsInstance VariablesConstructor BasicsRecap - Simple Calculator4Null Safety
Intro to Null SafetyNullable vs Non-NullableThe ? and ! OperatorsLate Keyword & Null SafetyNull-Aware OperatorsNull Safety in ClassesRecap - User Profile System7Abstract Classes & Interfaces
Abstract ClassesAbstract MethodsInterfaces in DartImplicit InterfacesImplementing vs ExtendingMultiple InterfacesRecap - Shape Calculator10Collections & Generics
List, Set, Map OverviewType-Safe CollectionsGeneric ClassesGeneric MethodsGeneric ConstraintsIterable & IteratorRecap - Generic Storage13Advanced OOP Concepts
Composition vs InheritanceExtension MethodsCallable ClassesSealed Classes (Dart 3)Records (Dart 3)Patterns & Matching (3.0)Enums with Methods16Project: Library Management
Project OverviewBook and User Classes2Constructors in Dart
Default ConstructorNamed ConstructorsInitializer ListsConstant ConstructorsFactory ConstructorsRedirecting ConstructorsRecap - Shape Builder5Encapsulation
Public vs Private MembersThe _ Prefix ConventionLibrary-Level PrivacyGetters & Setters DepthInformation HidingRecap - Student Records8Mixins
Introduction to MixinsCreating MixinsUsing Multiple Mixinson Keyword in MixinsMixin vs InheritanceMixin vs InterfaceRecap - Animal System11Special Methods
toString() OverridehashCode & == OverrideComparable Interfacecall() MethodnoSuchMethod OverrideRecap - Custom Collection14Design Patterns Part 1
Intro to Design PatternsSingleton PatternFactory PatternObserver PatternStrategy Pattern3Class Properties
Instance vs Static MembersFinal & Const FieldsLate VariablesStatic Methods & FieldsGetters and SettersRecap - Bank Account Manager6Inheritance
Basic InheritanceThe super KeywordMethod OverridingThe @override AnnotationThe final Class KeywordConstructors & InheritanceRecap - Employee Hierarchy9Polymorphism
Polymorphism BasicsPolymorphism via InterfacesRuntime Type CheckingThe is & as OperatorsCovariant KeywordRecap - Payment Processor