Skip to content

Commit

Permalink
Evading unit can be killed
Browse files Browse the repository at this point in the history
  • Loading branch information
xpmatteo committed Nov 9, 2023
1 parent 61a304d commit c88562c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
40 changes: 21 additions & 19 deletions src/ai/__snapshots__/mcts_player.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,38 @@ exports[`2 on 2 1`] = `
`;

exports[`close combat from light to heavy 1`] = `
"-21/30: undefined -> Roman play one card -> 1
-21/30: PlayCard(Order Light Troops) -> Roman order 1 light units -> 1
-21/29: End phase -> Roman movement -> 1
-20/28: MacroCommand(Move [2,3] to [3,2]) -> Roman battle -> 1
"-23/30: undefined -> Roman play one card -> 1
-23/30: PlayCard(Order Light Troops) -> Roman order 1 light units -> 1
-23/29: End phase -> Roman movement -> 1
-22/28: MacroCommand(Move [2,3] to [3,2]) -> Roman battle -> 1
CHANCE/27: Close Combat from [3,2] to [2,2] -> Roman battle -> 5
-3/3: Close Combat from [3,2] to [2,2] -> Carthaginian retreat -> 2
-2/3: Close Combat from [3,2] to [2,2] -> Carthaginian retreat -> 2
0/0: Retreat [2,2] to [3,1] -> Roman advance after combat -> 0
3/3: Retreat [2,2] to [2,1] -> Roman advance after combat -> 2
2/3: Retreat [2,2] to [2,1] -> Roman advance after combat -> 2
1/1: Momentum advance [3,2] to [2,2] -> Roman battle -> 0
1/1: Skip Momentum Advance -> Roman battle -> 1
-5/6: Close Combat from [3,2] to [2,2] -> Roman attacker retreat -> 3
-2/2: Retreat [3,2] to [3,4] -> Roman battle -> 1
2/2: End phase -> Carthaginian play one card -> 1
-7/7: Close Combat from [3,2] to [2,2] -> Roman attacker retreat -> 3
-3/3: Retreat [3,2] to [3,4] -> Roman battle -> 1
3/3: End phase -> Carthaginian play one card -> 1
-2/2: Retreat [3,2] to [1,4] -> Roman battle -> 1
2/2: End phase -> Carthaginian play one card -> 1
-1/2: Retreat [3,2] to [2,4] -> Roman battle -> 1
0/1: End phase -> Carthaginian play one card -> 0
-7/7: Close Combat from [3,2] to [2,2] -> Roman battle -> 1
7/7: End phase -> Carthaginian play one card -> 1
6/6: PlayCard(Order Heavy Troops) -> Carthaginian order 1 heavy units -> 1
-9/9: Close Combat from [3,2] to [2,2] -> Roman attacker retreat -> 3
-3/3: Retreat [3,2] to [1,4] -> Roman battle -> 1
2/2: End phase -> Carthaginian play one card -> 1
-2/2: Retreat [3,2] to [2,4] -> Roman battle -> 1
1/1: End phase -> Carthaginian play one card -> 0
-10/10: Close Combat from [3,2] to [2,2] -> Roman attacker retreat -> 3
-4/4: Retreat [3,2] to [1,4] -> Roman battle -> 1
3/3: End phase -> Carthaginian play one card -> 1
-3/3: Retreat [3,2] to [3,4] -> Roman battle -> 1
3/3: End phase -> Carthaginian play one card -> 1
-3/3: Retreat [3,2] to [2,4] -> Roman battle -> 1
3/3: End phase -> Carthaginian play one card -> 1
0/1: Close Combat from [3,2] to [2,2] -> Carthaginian retreat -> 2
0/1: Retreat [2,2] to [3,1] -> Roman advance after combat -> 0
2/3: Close Combat from [3,2] to [2,2] -> Carthaginian retreat -> 2
-2/3: Retreat [2,2] to [3,1] -> Roman advance after combat -> 2
-1/1: Momentum advance [3,2] to [2,2] -> Roman battle -> 0
0/1: Skip Momentum Advance -> Roman battle -> 1
0/0: Retreat [2,2] to [2,1] -> Roman advance after combat -> 0
-3/3: Close Combat from [3,2] to [2,2] -> Roman battle -> 1
3/3: End phase -> Carthaginian play one card -> 1
2/2: PlayCard(Order Heavy Troops) -> Carthaginian order 1 heavy units -> 1
"
`;

Expand Down
14 changes: 10 additions & 4 deletions src/model/commands/EvadeCommand.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Hex } from "../../lib/hexlib.js";
import { DamageEvent, DefenderEvasionEvent } from "../events.js";
import { DamageEvent, DefenderEvasionEvent, UnitKilledEvent } from "../events.js";
import { Command } from "./commands.js";

export class EvadeCommand extends Command {
Expand All @@ -21,10 +21,16 @@ export class EvadeCommand extends Command {
const diceResults = game.roll(attackingUnit.diceCount);
const damage = evadingUnit.calculateDamage(diceResults, false);
game.damageUnit(evadingUnit, damage);
game.moveUnit(this.toHex, this.fromHex);
/** @type {GameEvent[]} */
const events = [new DefenderEvasionEvent(this.toHex, this.fromHex),
new DamageEvent(attackingUnit, evadingUnit, this.fromHex, damage, diceResults)];
if (game.isUnitDead(evadingUnit)) {
events.push(new UnitKilledEvent(this.fromHex, evadingUnit));
} else {
game.moveUnit(this.toHex, this.fromHex);
}
game.endPhase();
return [new DefenderEvasionEvent(this.toHex, this.fromHex),
new DamageEvent(attackingUnit, evadingUnit, this.fromHex, damage, diceResults)];
return events;
}

toString() {
Expand Down
22 changes: 22 additions & 0 deletions src/model/commands/EvadeCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,26 @@ describe('evade command', () => {
test('it rolls for damage', () => {
expect(game.unitStrength(evadingUnit)).toBe(3);
});

});

test('It can kill the evading unit', () => {
const game = makeGame(new NullScenario(), diceReturning([RESULT_LIGHT, RESULT_LIGHT, RESULT_LIGHT, RESULT_LIGHT, RESULT_SWORDS]));
const evadingUnit = new RomanLightInfantry();
const attackingUnit = new CarthaginianHeavyInfantry();
game.placeUnit(hexOf(1, 4), evadingUnit);
game.placeUnit(hexOf(1, 5), attackingUnit);
game.unshiftPhase(new BattlePhase());
game.unshiftPhase(new FirstDefenderEvasionPhase(Side.ROMAN, [hexOf(0, 6)], hexOf(1, 4), hexOf(1, 5)));
const evadeCommand = new EvadeCommand(hexOf(0, 6), hexOf(1, 4), hexOf(1, 5));

const events = evadeCommand.play(game);

expect(game.unitAt(hexOf(0, 6))).toBeUndefined();
expect(events.map(e => e.toString())).toEqual([
"Defender evades to [0,6]",
"Carthaginian heavy infantry damages Roman light infantry at [1,4] for 4 damage with light,light,light,light,swords",
"Roman light infantry killed at [1,4]",
]);
expect(game.graveyard.unitsOf(Side.ROMAN)).toContain(evadingUnit);
});

0 comments on commit c88562c

Please sign in to comment.