Skip to content

Commit

Permalink
Merge pull request #78 from mwadle/issue77
Browse files Browse the repository at this point in the history
Add ability to combine two spearmen (issue #77)
  • Loading branch information
Sesu8642 authored Sep 9, 2024
2 parents 5b7e5c7 + e38bb4c commit 139fbc9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,29 +447,14 @@ public static void placeOwn(GameState gameState, HexTile tile) {
*/
public static void combineUnits(GameState gameState, HexTile tile) {
// place resulting unit as held object
// the unit that is not the peasant will be upgraded
Unit oldUnit;
if (((Unit) tile.getContent()).getUnitType() == UnitTypes.PEASANT) {
oldUnit = (Unit) gameState.getHeldObject();
} else {
oldUnit = (Unit) tile.getContent();
}
UnitTypes newUnitType = null;
switch (oldUnit.getUnitType()) {
case PEASANT:
newUnitType = UnitTypes.SPEARMAN;
break;
case SPEARMAN:
newUnitType = UnitTypes.KNIGHT;
break;
case KNIGHT:
newUnitType = UnitTypes.BARON;
break;
default:
break;
}
Unit heldUnit = (Unit) gameState.getHeldObject();
Unit tileUnit = (Unit) tile.getContent();

int newStrength = heldUnit.getStrength() + tileUnit.getStrength();
UnitTypes newUnitType = UnitTypes.ofStrength(newStrength);

Unit newUnit = new Unit(newUnitType);
newUnit.setCanAct(((Unit) tile.getContent()).isCanAct());
newUnit.setCanAct(tileUnit.isCanAct());
gameState.setHeldObject(newUnit);
placeObject(gameState, tile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,10 @@ public static boolean checkCombineUnits(GameState gameState, Player player, HexT
if (!ClassReflection.isAssignableFrom(Unit.class, tile.getContent().getClass())) {
return false;
}
if (((Unit) gameState.getHeldObject()).getUnitType() != UnitTypes.PEASANT
&& ((Unit) tile.getContent()).getUnitType() != UnitTypes.PEASANT) {
// not at least one peasant
return false;
}
if (((Unit) gameState.getHeldObject()).getUnitType() == UnitTypes.BARON
|| ((Unit) tile.getContent()).getUnitType() == UnitTypes.BARON) {
// cannot upgrade a baron
int heldUnitStrength = ((Unit) gameState.getHeldObject()).getStrength();
int tileUnitStrength = ((Unit) tile.getContent()).getStrength();
if (heldUnitStrength + tileUnitStrength > UnitTypes.strongest().strength()) {
// cannot create unit stronger than the strongest unit
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ private boolean acquireBaron(GameState gameState, Kingdom kingdom, PickedUpUnits
pickedUpUnits.removeUnit(UnitTypes.KNIGHT);
pickedUpUnits.addUnit(UnitTypes.BARON);
return true;
} else if (pickedUpUnits.ofType(UnitTypes.SPEARMAN) >= 2) {
// combine two spearmen
pickedUpUnits.removeUnit(UnitTypes.SPEARMAN);
pickedUpUnits.removeUnit(UnitTypes.SPEARMAN);
pickedUpUnits.addUnit(UnitTypes.BARON);
return true;
} else if (pickedUpUnits.ofType(UnitTypes.KNIGHT) >= 1
&& (GameStateHelper.getKingdomIncome(kingdom)
- getActualKingdomSalaries(gameState, kingdom, pickedUpUnits) - UnitTypes.BARON.salary()
Expand All @@ -385,6 +391,16 @@ private boolean acquireBaron(GameState gameState, Kingdom kingdom, PickedUpUnits
pickedUpUnits.removeUnit(UnitTypes.KNIGHT);
pickedUpUnits.addUnit(UnitTypes.BARON);
return true;
} else if (pickedUpUnits.ofType(UnitTypes.SPEARMAN) >= 1
&& (GameStateHelper.getKingdomIncome(kingdom)
- getActualKingdomSalaries(gameState, kingdom, pickedUpUnits) - UnitTypes.BARON.salary()
+ UnitTypes.SPEARMAN.salary() >= 0 || kingdom.getSavings() > UnitTypes.BARON.salary() * 3)
&& kingdom.getSavings() >= 2 * Unit.COST) {
// buy 2 peasants and combine with an existing spearman
kingdom.setSavings(kingdom.getSavings() - (2 * Unit.COST));
pickedUpUnits.removeUnit(UnitTypes.SPEARMAN);
pickedUpUnits.addUnit(UnitTypes.BARON);
return true;
} else if (canKingdomSustainNewUnit(gameState, kingdom, pickedUpUnits, UnitTypes.BARON)) {
// buy 4 peasants = 1 baron
buyUnitDirectly(kingdom, pickedUpUnits, UnitTypes.BARON);
Expand Down

0 comments on commit 139fbc9

Please sign in to comment.