-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
151 lines (138 loc) · 4.87 KB
/
index.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
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Juego de la Vida - Conway</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
canvas {
border: 1px solid black;
margin-top: 20px;
}
.controls {
margin-top: 10px;
}
button {
margin: 5px;
}
</style>
</head>
<body>
<h1>Juego de la Vida de Conway</h1>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<div class="controls">
<button id="startBtn">Iniciar</button>
<button id="pauseBtn">Pausar</button>
<button id="resetBtn">Reiniciar</button>
<button id="clearBtn">Limpiar</button>
</div>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const resolution = 10;
const COLS = canvas.width / resolution;
const ROWS = canvas.height / resolution;
let grid = createGrid(ROWS, COLS);
let interval;
let running = false;
function createGrid(rows, cols) {
return new Array(rows).fill(null).map(() => new Array(cols).fill(0));
}
function nextGen(grid) {
const nextGrid = createGrid(ROWS, COLS);
for (let row = 0; row < grid.length; row++) {
for (let col = 0; col < grid[row].length; col++) {
const cell = grid[row][col];
const numNeighbors = countNeighbors(grid, row, col);
if (cell === 1 && (numNeighbors < 2 || numNeighbors > 3)) {
nextGrid[row][col] = 0;
} else if (cell === 0 && numNeighbors === 3) {
nextGrid[row][col] = 1;
} else {
nextGrid[row][col] = cell;
}
}
}
return nextGrid;
}
function countNeighbors(grid, x, y) {
let sum = 0;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
const row = (x + i + ROWS) % ROWS;
const col = (y + j + COLS) % COLS;
sum += grid[row][col];
}
}
sum -= grid[x][y];
return sum;
}
function drawGrid(grid) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let row = 0; row < grid.length; row++) {
for (let col = 0; col < grid[row].length; col++) {
const cell = grid[row][col];
ctx.beginPath();
ctx.rect(col * resolution, row * resolution, resolution, resolution);
ctx.fillStyle = cell ? 'black' : 'white';
ctx.fill();
ctx.stroke();
}
}
}
function initializePattern() {
grid[10][10] = 1;
grid[10][11] = 1;
grid[10][9] = 1;
grid[9][10] = 1;
grid[11][11] = 1;
}
canvas.addEventListener('click', (e) => {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const col = Math.floor(x / resolution);
const row = Math.floor(y / resolution);
grid[row][col] = grid[row][col] ? 0 : 1;
drawGrid(grid);
});
// Controles
document.getElementById('startBtn').addEventListener('click', () => {
if (!running) {
interval = setInterval(() => {
grid = nextGen(grid);
drawGrid(grid);
}, 100);
running = true;
}
});
document.getElementById('pauseBtn').addEventListener('click', () => {
clearInterval(interval);
running = false;
});
document.getElementById('resetBtn').addEventListener('click', () => {
clearInterval(interval);
running = false;
grid = createGrid(ROWS, COLS);
initializePattern();
drawGrid(grid);
});
document.getElementById('clearBtn').addEventListener('click', () => {
clearInterval(interval);
running = false;
grid = createGrid(ROWS, COLS);
drawGrid(grid);
});
// Inicializar el canvas
initializePattern();
drawGrid(grid);
</script>
</body>
</html>