I believe Sour bill sold out Vanellope for Turbo.
Sour Bill is sour for a reason. When Ralph confronted him about what’s going on he denies this and says I’ll take it to my grave but eventually gives it up. Only HE and KING CANDY know of the memories, no one else is aware of this. He doesn’t stop Turbo from messing with the memories, instead he just watches and holds him.
If you pay attention to when King Candy enters in the code, you watch as he has a napkin with writing on it. You can’t get a cheat code into the system so easily, someone has to know it and considering that Sour Bill lives in the castle and personally attends to the royalty, he’s sure to know it.
Maybe one night he heads to tappers and runs into turbo to which they have a conversation about how he CANT stand Vanellope and wishes he had someone else. Honestly, Vanellope getting her code messed with probably really messed with her mind by causing her to become an outcast.
Anyways, we know Tapper will not give away any secrets considering when Ralph asked about a game with medals, he tells him no such game exists, but yet Hero’s Duty does.
I also take this theory a step forward by saying King Candy hatched the PERFECT plot. First he finds out that Sour Bill wants a new king (who doesn’t want to become king!?) to which he gets the code for, by getting the code he comes up with a plan to NOT ONLY get rid of his game but to take someone else’s game out as well since that would be the ONLY true way to make yourself disappear. If he were to leave just his game people would notice him in headquarters or wonder where the guy went but if he took someone else out then it would seem like a revenge plot so no one would think he survived.
Now that Turbo “no longer” exists he can now insert himself into the game without anyone questioning what’s going on and he must’ve done this right when the game first started because no one questioned him or noticed that Vanellope was a princess first, if anyone planned on visiting. Turbo couldn’t delete her code but realized that if she crossed the line all his success would fall apart hence why he wants to destroy her, by outlasting the girl and having the whole town against her it’ll open help in her favor. On top of this, candle head says, I JUST LISTENED TO TAFFIDA! To which she was probably acting upon King Candy’s orders about how glitches can’t race.
At the end of the game everyone is surprised EXCEPT for Sour Bill when they get their memories. He goes OH (as if all that treachery comes flooding back) now I remember and steps up to Vanellope and says all hail our RIGHTFUL ruler, Princess Vanellope. He’s not thrilled or cares about her at all, you can watch his face as she speaks too.
When she begins to speak she sounds like royalty but quickly changes her demeanor by saying “this is the real me!” Obviously them messing with her code really changed the princesses thinking, hence saying she wants to become president instead.
Anyways this is my theory! Let me know what you think. ❤️