life · Funciones
▶ Aprender life (Funciones) paso a paso · explicación de life
Otras versiones: Optim · Fácil
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.c
| Código | Subject | Por qué |
|---|---|---|
#include <stdio.h> | putchar | putchar. |
#include <stdlib.h> | atoi | atoi. |
#include <unistd.h> | read | read. |
| ||
void draw(int h, int w, char b[h + 2][w + 2]) | The initial configuration of the board will be drawn by a sequence of commands | Dibuja en el tablero que le paso. h y w van antes para poder usarlos en el tamaño de b. |
{ | ||
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… |
b[y][x] = 1; | La casilla donde estoy, viva. | |
} | ||
} | ||
| ||
int count(int h, int w, char b[h + 2][w + 2], int i, int j) | Cuántas vecinas vivas tiene (i, j). | |
{ | ||
int n = 0; | Vecinas vivas. | |
for (int di = -1; di <= 1; di++) | El cuadrado 3x3 alrededor… | |
for (int dj = -1; dj <= 1; dj++) | ||
n += b[i + di][j + dj]; | Sumo el 3x3 entero, yo incluida. | |
return n - b[i][j]; | Me resto a mí misma. | |
} | ||
| ||
void step(int h, int w, char cur[h + 2][w + 2], char next[h + 2][w + 2]) | Once end of file is reached, your program must simulate a game of life on this | Una generación: leo de cur, escribo en next. |
{ | ||
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 = count(h, w, cur, i, j); | Vecinas vivas. | |
next[i][j] = (n == 3 || (n == 2 && cur[i][j])); | La regla: 3 → viva; 2 → como estaba; si no → muerta. | |
} | ||
} | ||
| ||
void print(int h, int w, char b[h + 2][w + 2]) | print the result in stdout | Imprime las casillas de dentro. |
{ | ||
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(b[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. | |
} | ||
} | ||
| ||
int main(int ac, char **av) | ./life width height iterations | El main solo coordina. |
{ | ||
if (ac != 4) | ./life width height iterations | Programa + 3 argumentos. |
return 1; | Si no, fuera. | |
int w = atoi(av[1]); | Width and height are the dimensions of the board | Ancho. |
int h = atoi(av[2]); | Alto. | |
int iter = atoi(av[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. | |
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. | |
draw(h, w, board[0]); | Dibujo en el tablero 0. | |
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. |
{ | ||
step(h, w, board[cur], board[!cur]); | Del actual al otro. | |
cur = !cur; | El siguiente pasa a ser el actual. Sin copiar nada. | |
} | ||
print(h, w, board[cur]); | Imprimo el actual. | |
return 0; | ||
} |