Fix the problems the tests found
This commit is contained in:
parent
af5e74d4af
commit
be50ff62d9
1 changed files with 27 additions and 7 deletions
|
|
@ -1,27 +1,36 @@
|
|||
import { Board, GamePiece } from "../types/GameBoard";
|
||||
import { FuelCard } from "../types/FuelCard";
|
||||
import { Board } from "../types/GameBoard";
|
||||
|
||||
/** @internal */
|
||||
export function tractorBeam(board: Board, origin: number, magnitude: number) {
|
||||
let delta = 0;
|
||||
// let movedShips: GamePiece[] = [];
|
||||
|
||||
while (true) {
|
||||
delta++;
|
||||
|
||||
let behind = 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
|
||||
it towards the origin (forwards). If it were to collide with another
|
||||
ship, then drift until it no longer collides.
|
||||
*/
|
||||
if (behind >= 0 && board[behind] != null) {
|
||||
let ship = board[behind];
|
||||
if (
|
||||
behind >= 0
|
||||
&& behindShip != null
|
||||
) {
|
||||
let localMagnitude = magnitude;
|
||||
while (board[behind + localMagnitude] != null) {
|
||||
localMagnitude++;
|
||||
};
|
||||
board[behind + localMagnitude] = ship
|
||||
negativeNewPosition = behind + localMagnitude;
|
||||
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
|
||||
another ship, then drift until it no longer collides.
|
||||
*/
|
||||
if (ahead < board.length && board[ahead] != null) {
|
||||
let ship = board[ahead];
|
||||
if (
|
||||
ahead < board.length
|
||||
&& aheadShip
|
||||
) {
|
||||
let localMagnitude = magnitude;
|
||||
while (board[ahead - localMagnitude] != null) {
|
||||
localMagnitude++;
|
||||
};
|
||||
board[ahead - localMagnitude] = ship
|
||||
positiveNewPosition = ahead - localMagnitude;
|
||||
board[ahead] = null;
|
||||
};
|
||||
|
||||
// Simultaneous movement
|
||||
if (aheadShip) {
|
||||
board[positiveNewPosition] = aheadShip;
|
||||
};
|
||||
if (behindShip) {
|
||||
board[negativeNewPosition] = behindShip;
|
||||
};
|
||||
|
||||
|
||||
if (behind < 0 && ahead >= board.length) {
|
||||
break;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue