life · Fácil

▶ Aprender life (Fácil) paso a paso · explicación de life

Otras versiones: Optim · 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ódigoSubjectPor qué
#ifndef LIFE_HExpected files : *.c *.hGuarda: que el .h no se incluya dos veces.
# define LIFE_H
# include <stdlib.h>atoiatoi.
# include <unistd.h>readread.
# include <stdio.h>putcharputchar.
#endifFin de la guarda.
life.c
CódigoSubjectPor qué
#include "life.h"El .c usa los includes del .h.
int main(int argc, char **argv)./life width height iterationsTodo cabe en el main.
{
if (argc != 4)./life width height iterationsPrograma + 3 argumentos.
return (1);Si no, fuera.
int w = atoi(argv[1]);Width and height are the dimensions of the boardAncho.
int h = atoi(argv[2]);Alto.
int iter = atoi(argv[3]);iterations is the number ofCuántas generaciones.
if (w < 0 || h < 0 || iter < 0)Negativos no tienen sentido.
return (1);Si no, fuera.
// el tablero de ahora y el siguiente, con un borde de células muertas
char board[h + 2][w + 2];Each cell outside of the array will be considered dead.El tablero de ahora. +2: un borde de muertas alrededor.
char next[h + 2][w + 2];El de la generación siguiente. No hace falta ponerlo a 0: se escribe entero antes de leerlo.
for (int i = 0; i < h + 2; i++)Todo el tablero, borde incluido…
for (int j = 0; j < w + 2; j++)
board[i][j] = 0;Todo muerto, borde incluido.
// 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 commandsCará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[y][x] = 1;La casilla donde estoy, viva.
}
// 2. simular: todo a next y luego copiar
for (int it = 0; it < iter; it++)Once end of file is reached, your program must simulate a game of life on thisCada 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++)
if (di || dj)Si di y dj son 0 a la vez es la propia celda: la salto. Así no hay que restarse después.
n += board[i + di][j + dj];Viva vale 1, muerta 0.
if (board[i][j])Si está viva…
next[i][j] = (n == 2 || n == 3);…sigue con 2 o 3 vecinas.
else
next[i][j] = (n == 3);Muerta: nace con 3 justas.
}
}
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.
board[i][j] = next[i][j];Copio la generación nueva al tablero de ahora.
}
// 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[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);
}