You'd just code it so your health has to have reached 0, or you had to deck out. Simply leaving the game shouldn't work. And it would probably be best to put a limit on it. Like only once. If you fail the second time, you'll just have to play against a random opponent as usual.
I don't think there is really any advantage with this. You just lost, so it's still possible you could lose again. And this would normally be used against harder oppnents, since you don't often lose to the easy ones.