Está en la página 1de 3

Alberto Martínez Aguilar

Práctica 7 – Juego de la Vida


En este programa del juego de la vida se guarda toda la información de cada celda en un
solo byte. 4 bits indican cuántas celdas vivas rodean a una y los 4 restantes indican si esta
está viva o no. Las funciones utilizadas que no estuvieran en la guía son contarVecinos(),
vecinosCelda() y supervivencia(). Además se han hecho pequeños cambios para que las
funciones fueran compatibles. El contenido de las funciones desarrolladas es:

void contarVecinos(uint16_t d, celda mundo[d][d]) { // para todo el mundo


uint16_t f, c;
uint8_t vecindad;
for(f=0; f<d; f++) {
for(c=0; c<d; c++) {
vecindad = vecinosCelda(d, f, c, mundo);
setVecindad(&mundo[f][c], vecindad);
}
}
}

uint8_t vecinosCelda(uint16_t d, uint16_t f, uint16_t c, celda mundo[d][d]) {


// individual
int8_t i, j, m, n;
uint8_t vecinos = 0;

for(i=-1; i<=1; i++) {


for(j=-1; j<=1; j++) {
if(i == j && j == 0) { continue; }

m = f + i;
n = c + j;

if ((f == 0) && (i == -1)){ m = d - 1; }


else if((f == (d - 1)) && (i == 1)) { m = 0; }

if ((c == 0) && (j == -1)){ n = d - 1; }


else if((c == (d - 1)) && (j == 1)) { n = 0; }

vecinos += getEstado(mundo[m][n]) & 1;


}
}
return vecinos;
}

uint16_t supervivencia(uint16_t d , celda mundo[d][d]) {


uint16_t f, c;
uint16_t cuentaBichos = 0;
for(f=0; f<d; f++) {
for(c=0; c<d; c++) {
if(getEstado(mundo[f][c]) == _VIVO) {
if((getVecindad(mundo[f][c]) < 2) || (getVecindad(mundo[f]
[c]) > 3)) {
setEstado(&mundo[f][c], _MUERTO);
} else {
cuentaBichos++;
}
} else {
if(getVecindad(mundo[f][c]) == 3) {
setEstado(&mundo[f][c], _VIVO);
cuentaBichos++;
}
}
}
}
return cuentaBichos;
}

El código del proyecto completo es:

funciones.h
#include <stdint.h>

#define _SIZE 100


#define _DENSIDAD 20
#define _TIEMPO 120000

#define _BICHO 219


#define _NOBICHO ' '
#define _VIVO 0xF
#define _MUERTO 0x0

typedef uint8_t celda;

void setEstado(celda *c, uint8_t estado);


void setVecindad(celda *c, uint8_t vecindad);
uint8_t getEstado(celda c);
uint8_t getVecindad(celda c);
void mostrarMundo(uint16_t d, celda mundo[d][d]);
uint16_t inicializarMundo(uint16_t d, celda mundo[d][d], uint16_t p);
void contarVecinos(uint16_t d, celda mundo[d][d]);
uint16_t supervivencia(uint16_t d, celda mundo[d][d]);
uint16_t inicializarMundo(uint16_t d, celda mundo[d][d] , uint16_t porcentaje);
uint8_t vecinosCelda(uint16_t d, uint16_t f, uint16_t c, celda mundo[d][d]);

funciones.c
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <conio.h>
#include "funciones.h"

void setEstado(celda *c, uint8_t estado) {


*c = estado ? (*c | 0xF0) : (*c & 0x0F);
}

void setVecindad(celda *c, uint8_t vecindad) {


*c = (0xF0 & *c) ^ (0x0F & vecindad);
}

uint8_t getEstado(celda c) {
return (c & 0xF0) >> 4;
}

uint8_t getVecindad(celda c) {
return (c & 0x0F);
}

void mostrarMundo(uint16_t d, celda mundo[d][d]){


uint16_t f, c;
char matrizMundo[(2 * d * d) + d + 1];
char siBicho[4], noBicho[4];

sprintf(siBicho, "%c", _BICHO);


sprintf(noBicho, "%c", _NOBICHO);
matrizMundo[0] = 0; // Queda inicializada la cadena de texto
for(f = 0 ; f < d ; f++){
for(c = 0 ; c < d ; c++){
strcat(matrizMundo, getEstado(mundo[f][c]) ? siBicho : noBicho);
}
strcat(matrizMundo, "\n");
}
printf("%s\n\n", matrizMundo);
return;
}

uint16_t inicializarMundo(uint16_t d, celda mundo[d][d], uint16_t p){


int16_t f, c;
int16_t contadorBichos;
srand(time(NULL));
if(p < 0 || p > 100) return 0;
for(contadorBichos = 0, f = 0 ; f < d ; f++, srand(rand())) {
for(c = 0 ; c < d ; c++) {
if(rand() % 101 < p) {
setEstado(&mundo[f][c], _VIVO);
contadorBichos++;
} else {
setEstado(&mundo[f][c], _MUERTO);
}
}
}
return contadorBichos;
}

void contarVecinos(uint16_t d, celda mundo[d][d]) { // para todo el mundo


uint16_t f, c;
uint8_t vecindad;
for(f=0; f<d; f++) {
for(c=0; c<d; c++) {
vecindad = vecinosCelda(d, f, c, mundo);
setVecindad(&mundo[f][c], vecindad);
}
}
}

uint8_t vecinosCelda(uint16_t d, uint16_t f, uint16_t c, celda mundo[d][d]) { // individual


int8_t i, j, m, n;
uint8_t vecinos = 0;

for(i=-1; i<=1; i++) {


for(j=-1; j<=1; j++) {
if(i == j && j == 0) { continue; }

m = f + i;
n = c + j;

if ((f == 0) && (i == -1)){ m = d - 1; }


else if((f == (d - 1)) && (i == 1)) { m = 0; }

if ((c == 0) && (j == -1)){ n = d - 1; }


else if((c == (d - 1)) && (j == 1)) { n = 0; }

vecinos += getEstado(mundo[m][n]) & 1;


}
}
return vecinos;
}

main.c
#include "funciones.h"
#include "funciones.c"

int main()
{
celda mundo[_SIZE][_SIZE];
uint16_t bichos;
bichos = inicializarMundo(_SIZE, mundo, _DENSIDAD);

while(bichos)
{
system("cls");
contarVecinos(_SIZE, mundo);
mostrarMundo(_SIZE, mundo);
bichos = supervivencia(_SIZE, mundo);
usleep(_TIEMPO);
// getch();
}

return 0;
}

También podría gustarte