diff --git a/src/game/common.test.ts b/src/game/common.test.ts index 8c566c1..ab59b0a 100644 --- a/src/game/common.test.ts +++ b/src/game/common.test.ts @@ -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); @@ -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); +}); diff --git a/src/game/common.ts b/src/game/common.ts index 3c988c3..40f0305 100644 --- a/src/game/common.ts +++ b/src/game/common.ts @@ -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); } diff --git a/src/game/orders.ts b/src/game/orders.ts index c2604e1..23dc5e0 100644 --- a/src/game/orders.ts +++ b/src/game/orders.ts @@ -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]); @@ -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; @@ -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