Merge pull request #3027 from drmDev/bug/gameofchaos

fixes #3022 Bug/gameofchaos
This commit is contained in:
Derek M 2017-03-26 11:56:39 -04:00 committed by GitHub
commit 3d1aa304df

View file

@ -85,42 +85,35 @@ class GameOfChaosEffect extends OneShotEffect {
public boolean apply(Game game, Ability source) { public boolean apply(Game game, Ability source) {
Player you = game.getPlayer(source.getControllerId()); Player you = game.getPlayer(source.getControllerId());
Player targetOpponent = game.getPlayer(getTargetPointer().getFirst(game, source)); Player targetOpponent = game.getPlayer(getTargetPointer().getFirst(game, source));
if (you != null && targetOpponent != null) { if (you != null && targetOpponent != null) {
boolean continueFlipping = true; boolean continueFlipping = true;
boolean youWinFlip = you.flipCoin(game); // controller flips first boolean youWonFlip = you.flipCoin(game); // controller flips first
boolean controllerWonLast = false; boolean youWonLastFlip = false; // tracks if you won the flip last, negation of it means opponent won last
int lifeAmount = 1; // starts with 1 life int lifeAmount = 1; // starts stakes with 1 life
while (continueFlipping) { while (continueFlipping) {
if (youWonFlip) { // flipper of coin wins, flipper gain 1 and non-flipper loses 1
if (youWinFlip) { // flipper of coin wins, flipper gain 1 and non-flipper loses 1 handleLifeChangesFromFlip(game, you, targetOpponent, lifeAmount);
you.gainLife(lifeAmount, game); if (!cannotContinueFlipping(you, targetOpponent)) {
targetOpponent.loseLife(lifeAmount, game, false); continueFlipping = you.chooseUse(outcome, "You gained " + lifeAmount + " life! Flip again for double the life stakes?", source, game);
if (targetOpponent.canRespond() && you.canRespond()) { youWonLastFlip = true;
continueFlipping = you.chooseUse(outcome, "Flip again?", source, game);
controllerWonLast = true;
} }
} else { // non-flipper wins, flipper lose 1 and non-flipper gains 1 } else { // non-flipper wins, flipper lose 1 and non-flipper gains 1
you.loseLife(lifeAmount, game, false); handleLifeChangesFromFlip(game, targetOpponent, you, lifeAmount);
targetOpponent.gainLife(lifeAmount, game); if (!cannotContinueFlipping(you, targetOpponent)) {
if (targetOpponent.canRespond() && you.canRespond()) { continueFlipping = targetOpponent.chooseUse(outcome, "You gained " + lifeAmount + " life! Flip again for double the life stakes?", source, game);
continueFlipping = targetOpponent.chooseUse(outcome, "Flip again?", source, game); youWonLastFlip = false;
controllerWonLast = false;
} }
} }
if (!targetOpponent.canRespond() && !you.canRespond()) { if (cannotContinueFlipping(you, targetOpponent))
continueFlipping = false; continueFlipping = false;
}
if (continueFlipping) {
if (continueFlipping) {
lifeAmount *= 2; // double the life each time lifeAmount *= 2; // double the life each time
if (controllerWonLast) { youWonFlip = youWonLastFlip ? you.flipCoin(game) : !targetOpponent.flipCoin(game); // negate the opponent's results for proper evaluation of if you won in next iteration
youWinFlip = you.flipCoin(game);
} else {
youWinFlip = !targetOpponent.flipCoin(game); // negate the results for proper evaluation above
}
} }
} }
@ -128,4 +121,13 @@ class GameOfChaosEffect extends OneShotEffect {
} }
return false; return false;
} }
private void handleLifeChangesFromFlip(Game game, Player playerGainingLife, Player playerLosingLife, int lifeAmount) {
playerGainingLife.gainLife(lifeAmount, game);
playerLosingLife.loseLife(lifeAmount, game, false);
}
private boolean cannotContinueFlipping(Player you, Player opponent) {
return (!you.canRespond() || !opponent.canRespond() || (you.canLoseByZeroOrLessLife() && you.getLife() <= 0) || (opponent.canLoseByZeroOrLessLife() && opponent.getLife() <= 0));
}
} }