Skip to content

Instantly share code, notes, and snippets.

@Dyrits
Forked from codecademydev/Archmage.cs
Last active May 10, 2023 08:08
Show Gist options
  • Save Dyrits/00317b28fd10aa5d232f5d8985c320e5 to your computer and use it in GitHub Desktop.
Save Dyrits/00317b28fd10aa5d232f5d8985c320e5 to your computer and use it in GitHub Desktop.
Supernatural Inheritance

Supernatural Inheritance

At the Codecademy Guild of the Supernatural, there are three ranks of magicians. They rank from least to most powerful: Pupils, Mages, and Archmages. In this project, you’ll be developing a system in C# to track the weather magicians in the guild and their spells.

  • Pupils have a title and can create one weak wind storm.
  • Mages have a title and can create a weak wind storm and a weak rain storm.
  • Archmages have a title and can create a weak wind storm, a strong rain storm, and a strong lightning storm.

Notice anything shared in these ranks? If we created a class for each type of magician, we would have a lot of duplicated code. For example, every magician has a title and wind storm. To avoid duplication, we’ll use inheritance.

  • Pupil is the base class
  • Mage inherits from Pupil
  • Archmage inherits from Mage

Archmage inherits from Mage, which inherits from Pupil

We’ll also need a Storm class, which stores the essence, strength, and the title of the magician who created it.

Let’s get started!

// Archmage.cs
using System;
namespace MagicalInheritance
{
class Archmage: Mage
{
public Archmage(string title) : base(title) {}
public override Storm CastRainStorm()
{
return new Storm("rain", true, Title);
}
public Storm CastLightningStorm()
{
return new Storm("lightning", true, Title);
}
}
}
// Mage.cs
using System;
namespace MagicalInheritance
{
class Mage: Pupil
{
public Mage(string title) : base(title) {}
public virtual Storm CastRainStorm()
{
return new Storm("rain", false, Title);
}
}
}
using System;
namespace MagicalInheritance
{
class Program
{
static void Main(string[] args)
{
Storm ZulRajasStorm = new Storm("wind", false, "Zul'rajas");
Console.WriteLine(ZulRajasStorm.Announce());
Pupil MezilKree = new Pupil("Mezil-Kree");
Storm MezilKreeStorm = MezilKree.CastWindStorm();
Console.WriteLine(MezilKreeStorm.Announce());
Mage GulDan = new Mage("Gul’dan");
Storm GulDanRainStorm = GulDan.CastRainStorm();
Console.WriteLine(GulDanRainStorm.Announce());
Archmage NielasAran = new Archmage("Nielas Aran");
Storm NielasAranRainStorm = GulDan.CastRainStorm();
Console.WriteLine(NielasAranRainStorm.Announce());
Storm NielasAranLightningStorm = NielasAran.CastLightningStorm();
Console.WriteLine(NielasAranLightningStorm.Announce());
}
}
}
// Pupil.cs
using System;
namespace MagicalInheritance
{
class Pupil {
public string Title { get; private set; }
public Pupil(string title)
{
Title = title;
}
public Storm CastWindStorm()
{
return new Storm("wind", false, Title);
}
}
}
// Storm.cs
using System;
namespace MagicalInheritance
{
class Storm
{
string Essence { get; set; }
bool IsStrong { get; set; }
string Caster { get; set; }
public Storm(string essence, bool isStrong, string caster)
{
Essence = essence;
IsStrong = isStrong;
Caster = caster;
}
public string Announce()
{
return $"{Caster} cast a {(IsStrong ? "strong" : "weak")} {Essence} storm!";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment