src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

direction.js (622B)


      1 /* eslint-disable no-bitwise */
      2 
      3 export const up = 1 << 1;
      4 export const right = 1 << 2;
      5 export const down = 1 << 3;
      6 export const left = 1 << 4;
      7 export const clockwise = [up, right, down, left];
      8 
      9 // random returns a shuffled array of the four directions.
     10 export const random = () => [up, right, down, left].sort(
     11   () => Math.random() - 0.5,
     12 );
     13 
     14 // opposite returns the direction opposite to the passed direction.
     15 export const opposite = (d) => {
     16   if (d === up) return down;
     17   if (d === right) return left;
     18   if (d === down) return up;
     19   if (d === left) return right;
     20 
     21   throw new Error(`unrecognized direction ${d}`);
     22 };