Right so by analogy, it's possible that medieval castles were built a) for a reason that we don't understand or b) for a reason we do understand, but where the justification for why castles fulfil that requirement is dubious.
You made the following statements:
1. Pyramids were built so kings could live forever
2. The Egyptians saw that this did not work, but continued to build pyramids.
The first statement is not true. The pyramids were built for the after-life. This invalidates the premise for your second statement, as the Egyptians could not verify that it worked in the after-life (or that there even is an after-life).
Now that you see my reply in a different context, your castle analogy is also not relevant.