Skip to content

Instantly share code, notes, and snippets.

@danielistyo
Last active July 19, 2021 16:38
Show Gist options
  • Save danielistyo/97d77a53781fd99577b5bd103b74aed3 to your computer and use it in GitHub Desktop.
Save danielistyo/97d77a53781fd99577b5bd103b74aed3 to your computer and use it in GitHub Desktop.

Revisions

  1. danielistyo revised this gist Jul 19, 2021. 1 changed file with 15 additions and 10 deletions.
    25 changes: 15 additions & 10 deletions tornado-generator.js
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ function generateTornado(w, h) {
    let x = 0,
    y = 0,
    direction = 'right',
    newTornado = Array.from(tornado, (t) => [...t]),
    newTornado = Array.from(tornado, t => [...t]),
    count = 1;

    do {
    @@ -27,17 +27,22 @@ function generateTornado(w, h) {
    tornado[x][y] = count;
    x++;
    }
    newTornado = Array.from(newTornado, (t) => {
    const arr = [];
    for (let k = 0; k < t.length - 1; k++) {
    arr.push(t[k]);
    }
    return arr;
    });

    direction = 'left';
    y--;
    x--;
    count++;
    if (!tornado[x][y]) {
    newTornado = Array.from(newTornado, t => {
    const arr = [];
    for (let k = 0; k < t.length - 1; k++) {
    arr.push(t[k]);
    }
    return arr;
    });
    } else {
    break;
    }
    } else if (direction === 'left') {
    for (let j = 0; j < newTornado[0].length; j++) {
    tornado[x][y] = count;
    @@ -58,7 +63,7 @@ function generateTornado(w, h) {
    x++;
    count++;
    if (!tornado[x][y]) {
    newTornado = Array.from(newTornado, (t) => {
    newTornado = Array.from(newTornado, t => {
    const arr = [];
    for (let k = 1; k < t.length; k++) {
    arr.push(t[k]);
    @@ -75,4 +80,4 @@ function generateTornado(w, h) {
    console.log(tornado[t].join(' '));
    }
    }
    generateTornado(7, 8);
    generateTornado(7, 10);
  2. danielistyo revised this gist Dec 11, 2020. No changes.
  3. danielistyo revised this gist Dec 11, 2020. No changes.
  4. danielistyo revised this gist Dec 11, 2020. No changes.
  5. danielistyo revised this gist Dec 11, 2020. No changes.
  6. danielistyo revised this gist Dec 11, 2020. No changes.
  7. danielistyo renamed this gist Dec 11, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. danielistyo created this gist Dec 11, 2020.
    78 changes: 78 additions & 0 deletions tornado-generator
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    function generateTornado(w, h) {
    // initiate tornado
    const tornado = new Array(h);
    for (let i = 0; i < tornado.length; i++) {
    tornado[i] = new Array(w);
    }

    let x = 0,
    y = 0,
    direction = 'right',
    newTornado = Array.from(tornado, (t) => [...t]),
    count = 1;

    do {
    if (direction === 'right') {
    for (let j = 0; j < newTornado[0].length; j++) {
    tornado[x][y] = count;
    y++;
    }
    newTornado.splice(0, 1);
    direction = 'down';
    x++;
    y--;
    count++;
    } else if (direction === 'down') {
    for (let j = 0; j < newTornado.length; j++) {
    tornado[x][y] = count;
    x++;
    }
    newTornado = Array.from(newTornado, (t) => {
    const arr = [];
    for (let k = 0; k < t.length - 1; k++) {
    arr.push(t[k]);
    }
    return arr;
    });
    direction = 'left';
    y--;
    x--;
    count++;
    } else if (direction === 'left') {
    for (let j = 0; j < newTornado[0].length; j++) {
    tornado[x][y] = count;
    y--;
    }
    newTornado.splice(newTornado.length - 1, 1);
    direction = 'up';
    x--;
    y++;
    count++;
    } else if (direction === 'up') {
    for (let j = 0; j < newTornado.length; j++) {
    tornado[x][y] = count;
    x--;
    }
    direction = 'right';
    y++;
    x++;
    count++;
    if (!tornado[x][y]) {
    newTornado = Array.from(newTornado, (t) => {
    const arr = [];
    for (let k = 1; k < t.length; k++) {
    arr.push(t[k]);
    }
    return arr;
    });
    } else {
    break;
    }
    }
    } while (newTornado.length);

    for (const t in tornado) {
    console.log(tornado[t].join(' '));
    }
    }
    generateTornado(7, 8);