Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
implementing conway game of life
function seed() {
    return Array.prototype.slice.call(arguments);
}

function same([x, y], [j, k]) {
    return x === j && y === k;
}

// The game state to search for `cell` is passed as the `this` value of the function. 
function contains(cell) {
    return this.some(c => same(c, cell));
}

const sum = ([x, y], [j, k]) => [x + j, y + k];

const getNeighborsOf = ([x, y]) => {
    const neighborDeltas = [
        [-1, 1],
        [0, 1],
        [1, 1],
        [-1, 0], /* */ , [1, 0],
        [-1, -1],
        [0, -1],
        [1, -1]
    ];
    return neighborDeltas.map((d) => sum(d, [x, y]));
}

const livingNeighbors = getLivingNeighbors(cell, state);

return (
    livingNeighbors.length === 3 ||
    contains.call(state, cell) && livingNeighbors.length === 2);
}

const corners = (state = []) => {
    const xs = state.map(([x, _]) => x);
    const ys = state.map(([_, y]) => y);
    return {
        topRight: [Math.max(...xs), Math.max(...ys)],
        bottomLeft: [Math.min(...xs), Math.min(...ys)]
    };
};


const calculateNext = (state) => {
    const { bottomLeft, topRight } = corners(state);
    let result = [];
    for (let y = topRight[1] + 1; y >= bottomLeft[1] - 1; y--) {
        for (let x = bottomLeft[0] - 1; x <= topRight[0] + 1; x++) {
            result = result.concat(isAlive([x, y], state) ? [
                [x, y]
            ] : []);
        }
    }
    return result;
};

const printCell = (cell, state) => {
    return contains.call(state, cell) ? 'x' : '_';
};

const printCells = (state) => {
    const { bottomLeft, topRight } = corners(state);
    for (let y = topRight[1]; y >= bottomLeft[1]; y--) {
        let row = [];
        for (let x = bottomLeft[0]; x <= topRight[0]; x++) {
            row.push(printCell([x, y], state));
        }
        console.log(row.join(''));
    }
};

exports.seed = seed;
exports.same = same;
exports.contains = contains;
exports.sum = sum;
exports.getNeighborsOf = getNeighborsOf;
exports.getLivingNeighbors = getLivingNeighbors;
exports.isAlive = isAlive;
exports.corners = corners;
exports.calculateNext = calculateNext;
exports.printCell = printCell;
exports.printCells = printCells;

I am getting an error.

In this project you will build a simple two dimensional simulation of living cells, known as Conway’s Game of Life. In the process you will learn all about JavaScript functions, including what you can do with them and their many features.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda