SnIPPETsX
SnIPPETsX

SnIPPETsX › 1-omino (gerardo_abaunza_technical)

1-omino (gerardo_abaunza_technical)

JavaScript · updated Nov 11, 2026 · 73 lines

main.js

// 1-omino
//
//] [
//
// 2-omino
//
// [] []
// 
// 3-omino
//
// [] [] []
//
// []
// [] []
//
// 4-omino (non exhaustive)
//
// [] []
// [] []
//
// [] [] []
//    []

class nimo {
  grid = [];

  constructor(n) {
    for (let i = 0; i < n; i++) {
      const row = [];
      for (let j = 0; j < n; j++) {
        row.push(0);
      }
      grid.push(row);
    }
    // [[1, 0, 0],
    //  [1, 1, 0],
    // [0, 0, 0]]
    makeShape();
  }

  makeShape() {
    // Start at top left and turn into 1
    // Pick random adjacent neighbor and turn into 1 (valid index)
    // Repeat n times
    grid[0][0] = 1;
    let numbersTurned = 1;
    const n = grid.length;
    let row = 0;
    let col = 0;

    while (numbersTurned < n) {
      const directions = [[-1, 0], [0, 1], [1, 0], [0, -1]];
      let newIndex = getRandomIntInclusive(0, 3);
      // Check its within bounds
      if !(newIndex >= 0 && newIndex < n && grid[row + directions[newIndex][0]][col + directions[newIndex][1]] == 0) {
        newIndex = getRandomIntInclusive(0, 3);
      }
      
      row += directions[newIndex][0];
      col += directions[newIndex][1];
      grid[row][col] = 1;
      numbersTurned++;
    }

    return true;
  }
}

function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

Run this JavaScript snippet

Open this snippet on SnIPPETsX to run the JavaScript code in your browser, edit it together with others in real time, or fork it into your own copy. No install or account is needed.