Skip to content

Instantly share code, notes, and snippets.

@libkazz
Last active May 14, 2020 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save libkazz/525549ef933241a34521d3ff2136ea0f to your computer and use it in GitHub Desktop.
Save libkazz/525549ef933241a34521d3ff2136ea0f to your computer and use it in GitHub Desktop.
22種類のものが隣同士にならないように、縦におなじものがこないように縦6×横100にランダムに並べる
const randomArray = (w, h, v) => {
const a = Array.from(Array(v), (_,k) => k)
let t = []
for(let y=0; y<h; y++){
t.push([]);
for(let x=0; x<w; x++){
let g = t.map(r => r[x]).concat([t[y][x-1]]);
let f = a.filter(e => !g.includes(e));
t[y][x] = f[Math.floor(Math.random()*f.length)];
}
}
return t
}
// render to html
const colorMap = (v) => {
let r = 0x0, g = 0x0, b = 0x0;
let a = [];
let max;
for(let i=0;;i++) {
if(Math.pow(i,3) >= v) {
max = i;
break;
}
}
let _plus = 0xff / max;
for(let i=1; i<=max ; i++ ) {
r = _plus * i,
g = 0x0;
g = 0x0;
for(let j=1; j<=max ; j++ ) {
g = _plus * j;
b = 0x0;
for(let k=1; k<=max ; k++) {
b = _plus * k;
a.push([Math.round(r) , Math.round(g) , Math.round(b)]);
if(a.length >= v) return a;
}
if(a.length >= v) return a;
}
if(a.length >= v) return a;
}
}
const colors = colorMap(22),
body = document.getElementsByTagName('body')[0],
div = document.createElement('div'),
table = document.createElement('table');
randomArray(100, 6, 22).forEach(r => {
let tr = document.createElement('tr')
r.forEach(c => {
let td = document.createElement('td');
td.textContent = c;
color = colors[c]
td.setAttribute('style', `text-align: center; background-color: rgb(${color.join()})`);
tr.appendChild(td);
})
table.appendChild(tr)
});
div.appendChild(table)
body.insertBefore(div, body.firstChild)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment