src/selectors/gameState.js
function performMove(fields, direction) {
  const oldSpaceshipPosition = findSpaceshipPosition(fields);
  const dx = { left: -1, ahead: 0, right: 1 }[direction];
  const newSpaceshipPosition = [oldSpaceshipPosition[0] - 1, oldSpaceshipPosition[1] + dx];
  const newFields = fields.map((row, i) => row.map((field, j) => {
    const [background, oldObjects] = field;
    let newObjects = oldObjects;
    if (i === oldSpaceshipPosition[0] && j === oldSpaceshipPosition[1]) {
      if (outsideWorld(fields, newSpaceshipPosition)) {
        let border = null;
        if (i === 0) {
          border = 'top';
        } else if (j === 0) {
          border = 'left';
        } else {
          border = 'right';
        }
        newObjects = [`spaceship-out-${border}`];
      } else {
        newObjects = removeSpaceship(oldObjects);
      }
    }
    if (i === newSpaceshipPosition[0] && j === newSpaceshipPosition[1]) {
      if (onRock(fields, newSpaceshipPosition)) {
        newObjects = [...newObjects, 'spaceship-broken'];
      } else {
        newObjects = [...newObjects, 'S'];
      }
    }
    return [background, newObjects];
  }));
  return newFields;
}
function performMove(fields, direction) {
  const oldSpaceshipPosition = findSpaceshipPosition(fields);
  const dx = { left: -1, ahead: 0, right: 1 }[direction];
  const newSpaceshipPosition = [oldSpaceshipPosition[0] - 1, oldSpaceshipPosition[1] + dx];
  const newFields = fields.map((row, i) => row.map((field, j) => {
    const [background, oldObjects] = field;
    let newObjects = oldObjects;
    if (i === oldSpaceshipPosition[0] && j === oldSpaceshipPosition[1]) {
      if (outsideWorld(fields, newSpaceshipPosition)) {
        let border = null;
        if (i === 0) {
          border = 'top';
        } else if (j === 0) {
          border = 'left';
        } else {
          border = 'right';
        }
        newObjects = [`spaceship-out-${border}`];
      } else {
        newObjects = removeSpaceship(oldObjects);
      }
    }
    if (i === newSpaceshipPosition[0] && j === newSpaceshipPosition[1]) {
      if (onRock(fields, newSpaceshipPosition)) {
        newObjects = [...newObjects, 'spaceship-broken'];
      } else {
        newObjects = [...newObjects, 'S'];
      }
    }
    return [background, newObjects];
  }));
  return newFields;
}
function performMove ( fields , direction ) { const oldSpaceshipPosition = findSpaceshipPosition ( fields ) ; const dx = { left : - 1 , ahead : 0 , right : 1 } [ direction ] ; const newSpaceshipPosition = [ oldSpaceshipPosition [ 0 ] - 1 , oldSpaceshipPosition [ 1 ] + dx ] ; const newFields = fields . map ( ( row , i ) => row . map ( ( field , j ) => { const [ background , oldObjects ] = field ; let newObjects = oldObjects ; if ( i === oldSpaceshipPosition [ 0 ] && j === oldSpaceshipPosition [ 1 ] ) { if ( outsideWorld ( fields , newSpaceshipPosition ) ) { let border = null ; if ( i === 0 ) { border = 'top' ; } else if ( j === 0 ) { border = 'left' ; } else { border = 'right' ; } newObjects = [ ` ${ border } ` ] ; } else { newObjects = removeSpaceship ( oldObjects ) ; } } if ( i === newSpaceshipPosition [ 0 ] && j === newSpaceshipPosition [ 1 ] ) { if ( onRock ( fields , newSpaceshipPosition ) ) { newObjects = [ ... newObjects , 'spaceship-broken' ] ; } else { newObjects = [ ... newObjects , 'S' ] ; } } return [ background , newObjects ] ; } ) ) ; return newFields ; }
Return new 2D fields after move of the spaceship represented as object 'S'.
Dicection is one of 'left', 'ahead', 'right'.
Return new 2D fields after move of the spaceship represented as object S . Dicection is one of left ahead right .