life · Optim
▶ Aprender life (Optim) paso a paso · explicación de life
Otras versiones: Fácil · Funciones
Código línea a línea
Tres columnas: el código, el trozo del subject o del main que obliga a escribir esa línea, y el por qué.
life.h
| Código | Subject | Por qué |
|---|---|---|
#ifndef LIFE_H | Expected files : *.c *.h | Guarda: que el .h no se incluya dos veces. |
# define LIFE_H | ||
| ||
# include <stdlib.h> | atoi | atoi. |
# include <unistd.h> | read | read. |
# include <stdio.h> | putchar | putchar. |
| ||
#endif | Fin de la guarda. |
life.c
| Código | Subject | Por qué |
|---|---|---|
#include "life.h" | El .c usa los includes del .h. | |
| ||
int main(int argc, char **argv) | ./life width height iterations | Todo cabe en el main. |
{ | ||
if (argc != 4) | ./life width height iterations | Programa + 3 argumentos. |
return (1); | Si no, fuera. | |
int w = atoi(argv[1]); | Width and height are the dimensions of the board | Ancho. |
int h = atoi(argv[2]); | Alto. | |
int iter = atoi(argv[3]); | iterations is the number of | Cuántas generaciones. |
if (w < 0 || h < 0 || iter < 0) | Negativos no tienen sentido. | |
return (1); | Si no, fuera. | |
| ||
// dos tableros (actual y siguiente) con un borde de células muertas | ||
char board[2][h + 2][w + 2]; | Each cell outside of the array will be considered dead. | Dos tableros: el actual y el siguiente. +2: un borde de muertas alrededor, así los vecinos nunca se salen. En la pila (VLA): nada que liberar. |
for (int i = 0; i < h + 2; i++) | Todo el tablero, borde incluido… | |
for (int j = 0; j < w + 2; j++) | ||
board[0][i][j] = board[1][i][j] = 0; | …a muertas, en los dos. | |
| ||
// 1. dibujar: el lápiz empieza arriba a la izquierda (1,1) | ||
int x = 1, y = 1, pen = 0; | Imagine a pen starting in the top left corner of the board. Default is: pen not drawing. | El lápiz en (1,1): la esquina de arriba a la izquierda (el 0 es borde). Sin dibujar. |
char c; | El comando leído. | |
while (read(0, &c, 1) == 1) | The initial configuration of the board will be drawn by a sequence of commands | Carácter a carácter de stdin hasta el final. |
{ | ||
if (c == 'w' && y > 1) | 'w', 'a', 's', 'd': move the pen up, left, down, and right. | Arriba, si no estoy en la primera fila. |
y--; | ||
else if (c == 's' && y < h) | Your pen is not moving outside of the board, and will stay still if a command asks for it. | Abajo, si no estoy en la última. Si me saldría, no me muevo. |
y++; | ||
else if (c == 'a' && x > 1) | Izquierda. | |
x--; | ||
else if (c == 'd' && x < w) | Derecha. | |
x++; | ||
else if (c == 'x') | 'x': lift or lower the pen in order to start or stop drawing. | Subir o bajar el lápiz. |
pen = !pen; | 0 ↔ 1. | |
if (pen) | Your pen does nothing in case of invalid command. | Tras cada comando (válido o no), si dibujo… |
board[0][y][x] = 1; | …la casilla donde estoy, viva. | |
} | ||
| ||
// 2. simular | ||
int cur = 0; | Qué tablero es el actual: 0 o 1. | |
for (int it = 0; it < iter; it++) | Once end of file is reached, your program must simulate a game of life on this | Cada generación. |
{ | ||
for (int i = 1; i <= h; i++) | Solo las casillas de dentro: de 1 a h… | |
{ | ||
for (int j = 1; j <= w; j++) | …y de 1 a w. | |
{ | ||
int n = 0; | Vecinas vivas. | |
for (int di = -1; di <= 1; di++) | El cuadrado 3x3 alrededor… | |
for (int dj = -1; dj <= 1; dj++) | ||
n += board[cur][i + di][j + dj]; | …sumo: viva vale 1, muerta 0. | |
n -= board[cur][i][j]; | Me he contado a mí misma: me resto. | |
board[!cur][i][j] = (n == 3 || (n == 2 && board[cur][i][j])); | La regla: 3 vecinas → viva; 2 → sigue como estaba; si no → muerta. Se escribe en el otro tablero. | |
} | ||
} | ||
cur = !cur; | El siguiente pasa a ser el actual. Sin copiar nada. | |
} | ||
| ||
// 3. imprimir | ||
for (int i = 1; i <= h; i++) | Solo las casillas de dentro: de 1 a h… | |
{ | ||
for (int j = 1; j <= w; j++) | …y de 1 a w. | |
putchar(board[cur][i][j] ? 'O' : ' '); | each cell alive will be represented by the character 'O' and each dead cell will be represented by a space | Viva O, muerta espacio. |
putchar('\n'); | Fin de fila. | |
} | ||
return (0); | ||
} |