Skip to content

Commit

Permalink
Merge pull request #56 from cmavelis/hotfix-opposing-push
Browse files Browse the repository at this point in the history
Hotfix for opposing push cancelation
  • Loading branch information
cmavelis authored Nov 11, 2023
2 parents 86f3723 + b9b1268 commit 8307720
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/game/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { test, expect } from 'vitest';
import { arrayToCoordinates, coordinatesToArray } from '@/game/common';
import {
arrayToCoordinates,
coordinatesToArray,
isOppositeVector,
} from '@/game/common';

test('0,0 coords, 3,4 shape', () => {
expect(coordinatesToArray({ x: 0, y: 0 }, { x: 3, y: 4 })).toEqual(0);
Expand Down Expand Up @@ -41,3 +45,19 @@ test('4 index, 3,4 shape', () => {
test('11 index, 3,4 shape', () => {
expect(arrayToCoordinates(11, { x: 3, y: 4 })).toEqual({ x: 2, y: 3 });
});

test('opposite straight vectors', () => {
expect(isOppositeVector({ x: 1, y: 0 }, { x: -1, y: 0 })).toBe(true);
});

test('orthogonal straight vectors', () => {
expect(isOppositeVector({ x: 1, y: 0 }, { x: 0, y: 1 })).toBe(false);
});

test('opposite diagonal vectors', () => {
expect(isOppositeVector({ x: 1, y: 1 }, { x: -1, y: -1 })).toBe(true);
});

test('orthogonal diagonal vectors', () => {
expect(isOppositeVector({ x: 1, y: 1 }, { x: -1, y: 1 })).toBe(false);
});
7 changes: 7 additions & 0 deletions src/game/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ export const addDisplacement = (
return { x: start.x + displacement.x, y: start.y + displacement.y };
};

export const isOppositeVector = (
vector1: Coordinates,
vector2: Coordinates,
) => {
return vector1.x === -vector2.x && vector1.y === -vector2.y;
};

export function reportError(e: string) {
console.error(e);
}
Expand Down
17 changes: 15 additions & 2 deletions src/game/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export function orderResolver({ G }: { G: GObject }) {
if (didMovesCancel(ordersToResolve[0], ordersToResolve[1])) {
break;
}
// pushArray: moves that should be applied to each piece pushed
const pushArrayOne = applyPush(ordersToResolve[0]);
// @ts-ignore -- Haven't explicitly checked the type of [1], but order priorities are unique
const pushArrayTwo = applyPush(ordersToResolve[1]);
Expand Down Expand Up @@ -243,6 +244,20 @@ export function orderResolver({ G }: { G: GObject }) {
break;
}

// if pieces targeting each other's spots => cancel all pushes
if (moveOne && moveTwo) {
const pieceOne = getPiece(G, moveOne.id);
const pieceTwo = getPiece(G, moveTwo.id);
if (
pieceOne &&
pieceTwo &&
isEqual(moveOne.newPosition, pieceTwo.position) &&
isEqual(pieceOne.position, moveTwo.newPosition)
) {
break;
}
}

// exit loop if nothing left
if (!moveOne && !moveTwo) {
break;
Expand Down Expand Up @@ -451,8 +466,6 @@ export function orderResolver({ G }: { G: GObject }) {
isEqual(target2, movedPiece1.position);
const targetSameSquare = isEqual(target1, target2);

// todo: check for push chains that conflict

// noinspection RedundantIfStatementJS
if (targetEachOther || targetSameSquare) {
// the moves "bounce", cancel the orders
Expand Down

0 comments on commit 8307720

Please sign in to comment.