-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjuedo_vida.html
221 lines (204 loc) · 7.05 KB
/
juedo_vida.html
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<html><head><base href="https://marcelowong.info/juego-de-la-vida/" />
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Juego de la Vida</title>
<style>
body {
font-family: 'Courier New', monospace;
display: flex;
flex-direction: column;
align-items: center;
background-color: #2c3e50;
color: #ecf0f1;
margin: 0;
padding: 20px;
}
h1 {
color: #e74c3c;
text-shadow: 2px 2px #34495e;
}
#grid-container {
position: relative;
width: 600px;
height: 600px;
margin-bottom: 20px;
overflow: hidden;
}
#background {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="600" height="600" viewBox="0 0 600 600"><rect width="600" height="600" fill="%2327ae60"/><g fill="%231abc9c"><rect x="0" y="580" width="600" height="20"/><rect x="0" y="560" width="600" height="20" opacity="0.7"/><rect x="0" y="540" width="600" height="20" opacity="0.5"/></g><g fill="%232ecc71"><circle cx="50" cy="50" r="30"/><circle cx="150" cy="100" r="40"/><circle cx="300" cy="80" r="50"/><circle cx="450" cy="60" r="35"/><circle cx="550" cy="120" r="45"/></g><g fill="%2395a5a6"><rect x="100" y="500" width="40" height="40" rx="5"/><rect x="300" y="520" width="50" height="30" rx="5"/><rect x="500" y="510" width="35" height="35" rx="5"/></g></svg>');
background-size: cover;
z-index: -1;
}
#grid {
display: grid;
grid-template-columns: repeat(30, 20px);
grid-gap: 0;
background-color: transparent;
}
.cell {
width: 20px;
height: 20px;
transition: all 0.3s;
display: flex;
justify-content: center;
align-items: center;
}
.cell::after {
content: "";
display: block;
width: 16px;
height: 16px;
opacity: 0;
transition: opacity 0.3s;
}
.cell.alive::after {
opacity: 1;
}
.cell.alive.man::after {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><rect x="4" y="2" width="8" height="8" fill="%232980b9"/><rect x="6" y="10" width="4" height="4" fill="%232980b9"/><circle cx="6" cy="5" r="1" fill="%23ecf0f1"/><circle cx="10" cy="5" r="1" fill="%23ecf0f1"/></svg>');
}
.cell.alive.woman::after {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><rect x="4" y="2" width="8" height="8" fill="%23e74c3c"/><polygon points="6,10 10,10 8,14" fill="%23e74c3c"/><circle cx="6" cy="5" r="1" fill="%23ecf0f1"/><circle cx="10" cy="5" r="1" fill="%23ecf0f1"/></svg>');
}
button {
margin: 5px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #e74c3c;
color: #ecf0f1;
border: none;
border-radius: 5px;
transition: background-color 0.3s;
font-family: 'Courier New', monospace;
}
button:hover {
background-color: #c0392b;
}
</style>
</head>
<body>
<h1>Juego de la Vida</h1>
<div id="grid-container">
<div id="background"></div>
<div id="grid"></div>
</div>
<div>
<button id="start">Iniciar</button>
<button id="stop">Detener</button>
<button id="clear">Limpiar</button>
<button id="random">Aleatorio</button>
</div>
<script>
const gridSize = 30;
const grid = document.getElementById('grid');
let cells = [];
let isRunning = false;
let intervalId = null;
function createGrid() {
for (let i = 0; i < gridSize * gridSize; i++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.addEventListener('click', () => toggleCell(i));
grid.appendChild(cell);
cells.push({ alive: false, gender: Math.random() < 0.5 ? 'man' : 'woman' });
}
}
function toggleCell(index) {
cells[index].alive = !cells[index].alive;
updateGrid();
}
function updateGrid() {
const cellElements = grid.getElementsByClassName('cell');
for (let i = 0; i < cells.length; i++) {
cellElements[i].classList.toggle('alive', cells[i].alive);
cellElements[i].classList.toggle('man', cells[i].alive && cells[i].gender === 'man');
cellElements[i].classList.toggle('woman', cells[i].alive && cells[i].gender === 'woman');
}
}
function countNeighbors(index) {
const row = Math.floor(index / gridSize);
const col = index % gridSize;
let count = 0;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (i === 0 && j === 0) continue;
const newRow = (row + i + gridSize) % gridSize;
const newCol = (col + j + gridSize) % gridSize;
const neighborIndex = newRow * gridSize + newCol;
if (cells[neighborIndex].alive) count++;
}
}
return count;
}
function nextGeneration() {
const newCells = JSON.parse(JSON.stringify(cells));
for (let i = 0; i < cells.length; i++) {
const neighbors = countNeighbors(i);
if (cells[i].alive) {
newCells[i].alive = neighbors === 2 || neighbors === 3;
} else {
newCells[i].alive = neighbors === 3;
if (newCells[i].alive) {
const maleNeighbors = countMaleNeighbors(i);
const femaleNeighbors = neighbors - maleNeighbors;
newCells[i].gender = maleNeighbors > femaleNeighbors ? 'man' : 'woman';
}
}
}
cells = newCells;
updateGrid();
}
function countMaleNeighbors(index) {
const row = Math.floor(index / gridSize);
const col = index % gridSize;
let count = 0;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (i === 0 && j === 0) continue;
const newRow = (row + i + gridSize) % gridSize;
const newCol = (col + j + gridSize) % gridSize;
const neighborIndex = newRow * gridSize + newCol;
if (cells[neighborIndex].alive && cells[neighborIndex].gender === 'man') count++;
}
}
return count;
}
function startSimulation() {
if (!isRunning) {
isRunning = true;
intervalId = setInterval(nextGeneration, 300);
}
}
function stopSimulation() {
if (isRunning) {
isRunning = false;
clearInterval(intervalId);
}
}
function clearGrid() {
stopSimulation();
cells = cells.map(() => ({ alive: false, gender: Math.random() < 0.5 ? 'man' : 'woman' }));
updateGrid();
}
function randomizeGrid() {
stopSimulation();
cells = cells.map(() => ({
alive: Math.random() < 0.3,
gender: Math.random() < 0.5 ? 'man' : 'woman'
}));
updateGrid();
}
document.getElementById('start').addEventListener('click', startSimulation);
document.getElementById('stop').addEventListener('click', stopSimulation);
document.getElementById('clear').addEventListener('click', clearGrid);
document.getElementById('random').addEventListener('click', randomizeGrid);
createGrid();
</script>
</body>
</html>