Commit 79ef6d90 authored by Autopawn's avatar Autopawn

Agregado código de automata celular.

parent cf13340a
build:
mkdir -p build
gcc -c automata.c
gcc -c draw.c
gcc -c main.c
gcc automata.o draw.o main.o -o automata
\ No newline at end of file
#include "automata.h"
void board_step(board *bo){
board old = *bo;
for(int y=0;y<SIZE_Y;y++){
for(int x=0;x<SIZE_X;x++){
int x_p_1 = (x+1)%SIZE_X;
int x_m_1 = (x-1+SIZE_X)%SIZE_X;
int y_p_1 = (y+1)%SIZE_Y;
int y_m_1 = (y-1+SIZE_Y)%SIZE_Y;
int cell_w = old.cells[y][x_p_1];
int cell_n = old.cells[y_m_1][x];
int cell_e = old.cells[y][x_m_1];
int cell_s = old.cells[y_p_1][x];
int sum = cell_w+cell_n+cell_e+cell_s;
if(bo->cells[y][x]==0){
if(sum==1 || sum==2) bo->cells[y][x]=1;
}else{
if(sum>=2) bo->cells[y][x]=0;
}
}
}
}
board board_init(){
board bo;
for(int y=0;y<SIZE_Y;y++){
for(int x=0;x<SIZE_X;x++){
bo.cells[y][x] = 0;
}
}
for(int k=0;k<5;k++){
int x = rand()%SIZE_X;
int y = rand()%SIZE_Y;
bo.cells[y][x] = 1;
}
return bo;
}
#ifndef AUTOMATA_H
#define AUTOMATA_H
#include <stdlib.h>
#include <stdio.h>
#define SIZE_Y 20
#define SIZE_X 30
typedef struct{
char cells[SIZE_Y][SIZE_X];
} board;
void board_step(board *bo);
board board_init();
#endif
#include "draw.h"
void board_print(const board *bo){
printf("--------\n");
for(int y=0;y<SIZE_Y;y++){
for(int x=0;x<SIZE_X;x++){
if(bo->cells[y][x]){
printf(" *");
}else{
printf(" .");
}
}
printf("\n");
}
}
#ifndef DRAW_H
#define DRAW_H
#include "automata.h"
void board_print(const board *bo);
#endif
#include <unistd.h>
#include <time.h>
#include "automata.h"
#include "draw.h"
int main(int argc, char const *argv[]){
srand(time(NULL));
board bo = board_init();
while(1){
board_print(&bo);
usleep(1000000);
board_step(&bo);
}
return 0;
}
......@@ -238,7 +238,7 @@ Por ejemplo:
* Comentarios de C:
```plain
(//.*\n)|(/\*.*\*/)
(//.*\n)|(/\*(.|\n)*\*/)
```
```plain
(//.*\n)|(/\*([^*]|\*[^/])*\*/)
......
......@@ -122,6 +122,7 @@ gcc -D DISTANCE_EUCLIDEAN ...
:arrow_right: They paste the contents of a file.
:arrow_right: They are used to import *headers* so that the code has access to its **definitions**.
---
```
#include <file>
......@@ -309,12 +310,12 @@ int delta(int x, int y);
# Just link already created object files
gcc -c main.c
gcc -c delta.c
gcc main.o delta.o -o final.out
gcc main.o delta.o -o final
```
```bash
# Perform the whole compilation process:
gcc main.c delta.c -o final.out
gcc main.c delta.c -o final
```
---
......@@ -328,7 +329,7 @@ gcc main.c delta.c -o final.out
TARGETS = main.c delta.c
build/main:
mkdir build
mkdir -p build
gcc $(TARGETS) -o build/main
```
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment