Fix the problems the tests found

This commit is contained in:
Oliver Akins 2022-06-28 23:59:56 -06:00
parent af5e74d4af
commit be50ff62d9
No known key found for this signature in database
GPG key ID: 3C2014AF9457AF99

View file

@ -1,27 +1,36 @@
import { Board, GamePiece } from "../types/GameBoard";
import { FuelCard } from "../types/FuelCard"; import { FuelCard } from "../types/FuelCard";
import { Board } from "../types/GameBoard";
/** @internal */ /** @internal */
export function tractorBeam(board: Board, origin: number, magnitude: number) { export function tractorBeam(board: Board, origin: number, magnitude: number) {
let delta = 0; let delta = 0;
// let movedShips: GamePiece[] = [];
while (true) { while (true) {
delta++; delta++;
let behind = origin - delta; let behind = origin - delta;
let ahead = origin + delta; let ahead = origin + delta;
let negativeNewPosition = behind;
let positiveNewPosition = ahead;
let behindShip = board[behind];
let aheadShip = board[ahead];
/* /*
Check for a ship behind the origin, if there is a ship there then pull Check for a ship behind the origin, if there is a ship there then pull
it towards the origin (forwards). If it were to collide with another it towards the origin (forwards). If it were to collide with another
ship, then drift until it no longer collides. ship, then drift until it no longer collides.
*/ */
if (behind >= 0 && board[behind] != null) { if (
let ship = board[behind]; behind >= 0
&& behindShip != null
) {
let localMagnitude = magnitude; let localMagnitude = magnitude;
while (board[behind + localMagnitude] != null) { while (board[behind + localMagnitude] != null) {
localMagnitude++; localMagnitude++;
}; };
board[behind + localMagnitude] = ship negativeNewPosition = behind + localMagnitude;
board[behind] = null; board[behind] = null;
}; };
@ -30,16 +39,27 @@ export function tractorBeam(board: Board, origin: number, magnitude: number) {
pull it towards the origin (backwards). If it were to collide with pull it towards the origin (backwards). If it were to collide with
another ship, then drift until it no longer collides. another ship, then drift until it no longer collides.
*/ */
if (ahead < board.length && board[ahead] != null) { if (
let ship = board[ahead]; ahead < board.length
&& aheadShip
) {
let localMagnitude = magnitude; let localMagnitude = magnitude;
while (board[ahead - localMagnitude] != null) { while (board[ahead - localMagnitude] != null) {
localMagnitude++; localMagnitude++;
}; };
board[ahead - localMagnitude] = ship positiveNewPosition = ahead - localMagnitude;
board[ahead] = null; board[ahead] = null;
}; };
// Simultaneous movement
if (aheadShip) {
board[positiveNewPosition] = aheadShip;
};
if (behindShip) {
board[negativeNewPosition] = behindShip;
};
if (behind < 0 && ahead >= board.length) { if (behind < 0 && ahead >= board.length) {
break; break;
}; };