PROGRAM juego; uses crt; {N: dimensi¢n m xima de la matriz} const N = 20; type {mundo:tipo de matriz donde guardaremos los datos} mundo= array[1..N, 1..N] of integer; {Crea el mundo utilizando la dimension que tenemos almacenada, teniendo en cuenta la probabilidad dada y la guarda en la matriz m} procedure crearmundo(dimension:integer; var m:mundo; var probabilidad:real); var i,j: integer; aleatorio: real; begin for i:=1 to dimension do begin for j:=1 to dimension do begin if i=1 then begin {0 por ser lateral} m[i][j] := 0; end else if j=1 then begin {0 por ser lateral} m[i][j] := 0; end else if j=dimension then begin {0 por ser lateral} m[i][j] := 0; end else if i=dimension then begin {0 por ser lateral} m[i][j] := 0; end else begin {obtenemos un numero aleatorio entre 0 y 1} aleatorio := random; {si el aleatorio en menor a la probabilidad introducida hay vida} if aleatorio<=probabilidad then begin m[i][j] := 1; end else begin {si el aleatorio es mayor a la probabilidad introducida no hay vida} m[i][j] := 0; end; end; end; end; ClrScr; writeln(''); writeln('Mundo creado'); writeln(''); write('Dimensi¢n: '); writeln(dimension); write('Probabilidad: '); writeln(probabilidad); writeln(''); end; {escribe en pantalla el mundo actual dada la matriz y la dimensi¢n de la misma} procedure vermundo(dimension: integer; m:mundo); var i,j: integer; c: char; begin for i:=1 to dimension do begin for j:=1 to dimension do begin if j=dimension then writeln(m[i][j]) else write(m[i][j]); end; end; writeln(''); writeln('Pulse una enter para continuar...'); writeln(''); {Repite hasta que el usuario pulsa enter} repeat c:=readkey; until c in [#13]; end; {realiza una transacci¢n segun las reglas x,y,z en la matriz m de dimensi¢n dimension} procedure transaccion(dimension: integer; x,y,z: integer; var m: mundo); var {n: se utilizar  para realizar una copia del mundo actual} n: mundo; contador: integer; i,j: integer; begin {copiamos en n el mundo actual} n:=m; {contamos las celulas vivas al rededor de una dada} for i:=2 to dimension-1 do begin for j:=2 to dimension-1 do begin contador := 0; if n[i-1][j] = 1 then contador:= contador + 1; if n[i+1][j] = 1 then contador:= contador + 1; if n[i][j-1] = 1 then contador:= contador + 1; if n[i][j+1] = 1 then contador:= contador + 1; if n[i-1][j-1] = 1 then contador:= contador + 1; if n[i-1][j+1] = 1 then contador:= contador + 1; if n[i+1][j-1] = 1 then contador:= contador + 1; if n[i+1][j+1] = 1 then contador:= contador + 1; {si la celula esta muerta y rodeada de x vivas la mantenemos viva} if (contador = x) AND (n[i][j]=0) then m[i][j]:=1 else begin if (contador >= y) AND (n[i][j]=1) then begin {si la celula esta viva y rodeada de entre y y z vivas nace} if contador <= z then m[i][j]:=1 {si no muere} else m[i][j]:=0; end else begin {la celula se marca como muerta} m[i][j]:=0; end; end; end; end; end; {generamos el mundo a partir de los datos que da el usuario} procedure generar_mundo(var dimension: integer; var probabilidad: real; var m: mundo); var c: string; error: integer; begin ClrScr; writeln(' '); writeln(' Datos necesarios para la generaci¢n del mundo: '); writeln(' '); write(' Introduce la dimensi¢n del tablero (entre 4 y 20) '); repeat readln(c); val(c,dimension,error); if dimension<4 then error:=1; if dimension>20 then error:=1; until error=0; writeln(' '); write(' Introduce la probabilidad (entre 0 y 1) '); repeat readln(c); val(c,probabilidad,error); if probabilidad<0 then error:=1; if probabilidad>1 then error:=1; until error=0; crearmundo(dimension,m,probabilidad); vermundo(dimension,m); end; {realiza la ejecuci¢n paso a paso} procedure paso_paso(dimension:integer; x,y,z: integer; var m: mundo; numero_turnos: integer); var k: integer; begin ClrScr; for k:=1 to numero_turnos do begin write('Ejecuci¢n '); write(k); write(' de '); write(numero_turnos); writeln(''); writeln(''); transaccion(dimension, x, y, z, m); vermundo(dimension,m); end; end; {muestra directamente los resultados de la ejecuci¢n} procedure resultados(dimension:integer; x,y,z: integer; var m: mundo; numero_turnos: integer); var k: integer; begin ClrScr; write('Realizadas '); write(numero_turnos); write(' ejecuciones. '); writeln(''); writeln(''); for k:=1 to numero_turnos do begin transaccion(dimension, x, y, z, m); end; vermundo(dimension,m); end; {opci¢n ejecutar del menu, se leen los datos necesarios} procedure ejecutar(dimension: integer; var m: mundo); var x,y,z: integer; numero_turnos: integer; modo: char; c:string; error: integer; begin ClrScr; writeln(' '); writeln(' Datos necesarios para realizar la ejecuci¢n: '); writeln(' '); write(' Introduce el numero de turnos: '); repeat readln(c); val(c,numero_turnos,error); until error=0; writeln(' '); write(' Introduce el valor de x: '); repeat readln(c); val(c,x,error); until error=0; writeln(' '); write(' Introduce el valor de y: '); repeat readln(c); val(c,y,error); until error=0; writeln(' '); write(' Introduce el valor de z: '); repeat readln(c); val(c,z,error); until error=0; writeln(''); writeln(' Introduce el modo:'); writeln(' 1 - Paso a paso'); writeln(' 2 - Solo resultados'); writeln(' '); repeat readln(modo); until modo in ['1','2']; modo:=upcase(modo); case modo of '1': paso_paso(dimension, x,y,z, m, numero_turnos); '2': resultados(dimension, x,y,z, m, numero_turnos); end; end; {leemos el archivo} procedure leer(var dimension:integer; var m: mundo); var cadena: string; c: integer; k,i,j:integer; begin {simula que lee una cadena} cadena := '000000|011110|000000|011110|000000|000000'; k:=1; i:=0; j:=1; while k <= length(cadena) do begin i:=i+1; if cadena[k]<>'|' then begin val(cadena[k], m[j][i], c); end else begin dimension := i-1; j:= j+1; i:= 0; end; k:= k+1; end; ClrScr; writeln('Mundo leido: '); writeln(''); vermundo(dimension,m); end; {muestra el menu} procedure menu; var O: char; dimension: integer; probabilidad: real; m: mundo; begin repeat begin ClrScr; writeln(''); writeln(' JueGo De La ViDa'); writeln(''); writeln(' A - Generar mundo'); writeln(' B - Ejecutar'); writeln(' C - Leer archivo'); writeln(' D - Salir'); writeln(''); write(' '); readln(O); O:=upcase(O); case O of 'A' : generar_mundo(dimension,probabilidad,m); 'B' : ejecutar(dimension, m); 'C' : leer(dimension,m); end; end; until (O='D') end; BEGIN menu; END. ---------------------------- (añadido tras someter) ---------------------------- 18:26:47 11/12/09 -> Segun los datos introducidos los ficheros son: G16P06D2 -> Los autores del trabajo son: LERIN CRISTOBAL, MIRIAM Nombres originales de los archivos entregados -> JV.PAS, jv.txt