-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
121 lines (97 loc) · 3.48 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
let inputHeight, inputWidth;
function generarTablero() {
const gridContainer = document.getElementById('grid-container');
inputHeight = parseInt(document.getElementById('input-height').value);
inputWidth = parseInt(document.getElementById('input-width').value);
gridContainer.innerHTML = '';
if (!inputHeight || !inputWidth) {
alert('Por favor, introduce la altura y el ancho del tablero.');
return;
}
gridContainer.style.gridTemplateColumns = `repeat(${inputWidth}, 20px)`;
for (let i = 0; i < inputHeight; i++) {
for (let j = 0; j < inputWidth; j++) {
const cellDiv = document.createElement('div');
cellDiv.classList.add('cell');
cellDiv.addEventListener('click', toggleCellState);
gridContainer.appendChild(cellDiv);
}
}
}
function verPoblacion() {
const gridContainer = document.getElementById('grid-container');
const inputCode = document.getElementById('input-code').value;
const filas = inputCode.split('#');
const maxColLength = Math.max(...filas.map(fila => fila.length));
if (filas.length > inputHeight || maxColLength > inputWidth) {
alert('El código excede el tamaño del tablero.');
return;
}
let cellIndex = 0;
for (let i = 0; i < inputHeight; i++) {
let fila = filas[i] || '';
if (fila === '') {
fila = '0'.repeat(inputWidth);
}
for (let j = 0; j < inputWidth; j++) {
const celda = fila[j] || '0';
const cellDiv = gridContainer.children[cellIndex];
if (celda === '1') {
cellDiv.classList.add('alive');
} else {
cellDiv.classList.remove('alive');
}
cellIndex++;
}
}
}
function borrarPoblacion() {
const cells = document.getElementById('grid-container').children;
for (let cell of cells) {
cell.classList.remove('alive');
}
}
function toggleCellState() {
this.classList.toggle('alive');
}
function generarCodigoPoblacion() {
const gridContainer = document.getElementById('grid-container');
const cells = gridContainer.children;
let codigo = '';
let filasVacias = 0;
for (let i = 0; i < inputHeight; i++) {
let filaCodigo = '';
for (let j = 0; j < inputWidth; j++) {
const cellDiv = cells[i * inputWidth + j];
filaCodigo += cellDiv.classList.contains('alive') ? '1' : '0';
}
if (filaCodigo.trim() === '0'.repeat(inputWidth)) {
filasVacias++;
} else {
if (filasVacias > 0) {
codigo += '#'.repeat(filasVacias);
filasVacias = 0;
}
if (codigo.length > 0) {
codigo += '#';
}
codigo += filaCodigo;
}
}
if (filasVacias > 0) {
codigo += '#'.repeat(filasVacias);
}
if (codigo.startsWith('#')) {
codigo = codigo.substring(1);
}
if (codigo.endsWith('#')) {
codigo = codigo.substring(0, codigo.length - 1);
}
document.getElementById('generated-code').innerText = `p=${codigo}`;
}
function copiarCodigo() {
const codigo = document.getElementById('generated-code').innerText;
navigator.clipboard.writeText(codigo).then(() => {
alert('Código copiado al portapapeles');
});
}