Merge pull request #1376 from magefree/revert-1375-mana_cleanup

Revert "Mana updates"
This commit is contained in:
LevelX2 2015-11-19 22:59:33 +01:00
commit 02c1b9f22f
2 changed files with 26 additions and 107 deletions

View file

@ -434,8 +434,6 @@ public class ManaTest {
@Test
public void shouldNotSubtractLessThan0() {
// given
expectedException.expect(ArithmeticException.class);
expectedException.expectMessage("You can not subtract below 0");
Mana thisMana = new Mana(2, 2, 2, 2, 2, 2, 2);
Mana thatMana = new Mana(10, 1, 1, 1, 10, 1, 1);
@ -443,57 +441,13 @@ public class ManaTest {
thisMana.subtract(thatMana);
// then
}
@Test
public void shouldNotAllowMinusSubtractionCost() {
// given
expectedException.expect(ArithmeticException.class);
expectedException.expectMessage("You can not subtract below 0");
Mana thisMana = new Mana(2, 2, 2, 2, 2, 2, 2);
Mana thatMana = new Mana(10, 1, 1, 1, 10, 1, 1);
// when
thisMana.subtractCost(thatMana);
// then
}
@Test
public void shouldUseExistingManaToPayColorless() {
// given
Mana available = new Mana();
available.setRed(7);
Mana cost = new Mana();
cost.setRed(4);
cost.setColorless(2);
// when
available.subtractCost(cost);
// then
assertEquals(1, available.getRed());
}
@Test
public void shouldThrowExceptionOnUnavailableColorless() {
// given
expectedException.expect(ArithmeticException.class);
expectedException.expectMessage("Not enough mana to pay colorless");
Mana available = new Mana();
available.setRed(4);
Mana cost = new Mana();
cost.setRed(4);
cost.setColorless(2);
// when
available.subtractCost(cost);
assertEquals(-8, thisMana.getRed());
assertEquals(1, thisMana.getGreen());
assertEquals(1, thisMana.getBlue());
assertEquals(1, thisMana.getWhite());
assertEquals(-8, thisMana.getBlack());
assertEquals(1, thisMana.getColorless());
assertEquals(1, thisMana.getAny());
}

View file

@ -63,8 +63,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
protected int any;
protected boolean flag = false;
//TODO THIS IS UNSAFE AND MUTABLE
//TODO THIS SHOULD BE REMOVED
//todo unsafe and mutable
public static final Mana RedMana = RedMana(1);
public static final Mana GreenMana = GreenMana(1);
public static final Mana BlueMana = BlueMana(1);
@ -269,52 +268,24 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
*
* @param mana mana values to subtract
*/
public void subtract(final Mana mana) throws ArithmeticException {
red = validateSubtraction(red, mana.red);
green = validateSubtraction(green, mana.green);
blue = validateSubtraction(blue, mana.blue);
white = validateSubtraction(white, mana.white);
black = validateSubtraction(black, mana.black);
colorless = validateSubtraction(colorless, mana.colorless);
any = validateSubtraction(any, mana.any);
public void subtract(final Mana mana) {
red -= mana.red;
green -= mana.green;
blue -= mana.blue;
white -= mana.white;
black -= mana.black;
colorless -= mana.colorless;
any -= mana.any;
}
/**
* Ensures subtraction will not result in a negative number.
*
* @param lhs left hand side operand
* @param rhs right hand side operand
* @return returns the non-negative subtraction result
* @throws ArithmeticException thrown when the result of the subtraction
* is less than 0.
*/
private int validateSubtraction(final int lhs, final int rhs) throws ArithmeticException {
int result = lhs - rhs;
if (result < 0) {
throw new ArithmeticException("You can not subtract below 0");
}
return result;
}
/**
* Subtracts the passed in mana values from this instance. Will not
* reduce this instances mana below 0. The difference between this and
* {@code subtract()} is that if we do not have the available colorlesss
* mana to pay, we take mana from our colored mana pools.
*
* @param mana mana values to subtract
* @throws ArithmeticException thrown if there is not enough available
* colored mana to make up the negative colorless cost
*/
public void subtractCost(final Mana mana) throws ArithmeticException {
red = validateSubtraction(red, mana.red);
green = validateSubtraction(green, mana.green);
blue = validateSubtraction(blue, mana.blue);
white = validateSubtraction(white, mana.white);
black = validateSubtraction(black, mana.black);
any = validateSubtraction(any, mana.any);
colorless -= mana.colorless; // can be minus, will use remaining mana to pay
public void subtractCost(Mana cost) {
red -= cost.red;
green -= cost.green;
blue -= cost.blue;
white -= cost.white;
black -= cost.black;
any -= cost.any;
colorless -= cost.colorless;
while (colorless < 0) {
int oldColorless = colorless;
@ -347,7 +318,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
colorless++;
}
if (oldColorless == colorless) {
throw new ArithmeticException("Not enough mana to pay colorless");
break; // to prevent endless loop -> should not be possible, but who knows
}
}
}
@ -507,13 +478,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
*/
public boolean enough(final Mana avail) {
Mana compare = avail.copy();
red -= avail.red;
green -= avail.green;
blue -= avail.blue;
white -= avail.white;
black -= avail.black;
colorless -= avail.colorless;
any -= avail.any;
compare.subtract(this);
if (compare.getRed() < 0) {
compare.setAny(compare.getAny() + compare.getRed());
if (compare.getAny() < 0) {