Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was reading about the rules for royal succession of the British monarchy and thought the easiest way I could understand it is with code (or pseudo-code). I came up with this (in a C# like pseudo-code):

EventHandler CurrentMonarch_Died {
    CurrentMonarch = SuccessorRootedAt(Person.Electress_Sophia_Of_Hanover);
}

enum Gender { Male, Female };  // Male must come before Female

Person SuccessorRootedAt(Person person) {
    if (person.IsAlive && IsQualified(person.Religion, person.Citizenship))
        return person;

    sortedChildren = person.Children.OrderBy(p => p.Gender).ThenBy(p => p.DateOfBirth);

    foreach (child in sortedChildren) {
        successor = SuccessorRootedAt(child);
        if (successor != null)
            return successor;
    }

    return null;
}

Is it correct?

share|improve this question
2  
As an aside, you may find Comprehensive Rules for Game of Thrones Lines of Succession on Sci-Fi.SE interesting. It has some pseudo-code in it too. –  MichaelT Jul 31 '13 at 20:32
1  
I don't have enough rep to leave this as a comment, so it will have to be an answer. The current rules of succession no longer have male children taking precedence over females (see here). –  user97824 Jul 31 '13 at 20:42
2  
They changed the law before the birth of the new baby, so firstborn now takes precedence, regardless of gender. –  razethestray Jul 31 '13 at 20:47
1  
@razethestray: This change is not yet effective, because not all of the Commonwealth realms have implemented it. –  chirlu Jul 31 '13 at 21:01
2  
@SimonCB765: See above; Wikipedia on the Perth Agreement has the full story. –  chirlu Jul 31 '13 at 21:08

1 Answer 1

Close, but if you analyse history, it's more like:

Person SuccessorRootedAt(Person person) {
  if(!rand%100) //Reality simulator
  {
    if (person.IsAlive && IsQualified(person.Religion, person.Citizenship))
      return person;

    sortedChildren = person.Children.OrderBy(p => p.Gender).ThenOrderBy(p => p.DateOfBirth);

    foreach (child in sortedChildren) {
      successor = SuccessorRootedAt(child);
      if (successor != null)
        return successor;
  }
  else
  {
    return rand();
  }
}

And yes, it occasionally causes a whole mess of trouble.

share|improve this answer
1  
So does rand() represent invaders, rebellion, forged claims, political maneuvering, incompetent heirs, or yes? –  Paul Marshall Jul 31 '13 at 18:49
2  
@PaulMarshall: I think incompentent heirs would not be included by rand() because IsQualified() does not take Person.Competence. –  FrustratedWithFormsDesigner Jul 31 '13 at 19:06
    
@Philip: If I recall correctly from CGPGrey, return rand(); should be replaced with return SuccessorRootedAt(person.getKinglyParent()). This is a bit of an oversimplification. I think it would probably be better to say that if no successor is found, re-run the successor algorithm that was used for choosing the previous king (in this iteration, the previous king will be ineligible). Of course, this kind of rule is more applicable in the last few hundred years; before that it was more about army size. And yes, this is applied recursively. CGPGrey's explanation is probably more accurate. –  Brian Jul 31 '13 at 20:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.