AUTORES: BENITO MIANA, SERGIO GONZALO COLOMA, MARIO A. GRUPO: 07 PUESTO: 5 Hemos simulado el juego de la vida, para ello hemos utilizado, matrices, funciones (if, ramdon...), bucles (repeat,while, for), procedimientos (procedure)... La estructura esta detallada mas adelante: Para la opcion 4, hace falta que halla un fichero txt, con el nombre mundo en la carpeta BIN del programa Turbopascal, para que funcione Program juegodelavida; uses crt; {Declaramos los tipos de variables} type m = array[1..20 , 1..20] of integer; function cargar_mundo(nombreFich: string): string; var fichero:text; linea:string; mundo:String[200]; begin assign(fichero,nombreFich); Reset(fichero); mundo:=''; while not eof(fichero) do begin Readln(fichero,linea); mundo:=mundo+linea+'|'; end; cargar_mundo:=mundo; end; {Realizamos procedimientos, para optimizar el programa} {Este procedimiento sirve para declarar el tama¤o del tablero} procedure dimen (var d:integer); begin Repeat begin writeln(''); writeln('Dime el tama¤o del lado del tablero:'); {Cada vez que se solicite al usuario un valor, utilizaremos el ioresult, para la robustez del programa} {$I-} repeat readln(d); {$I+} until ioresult=0; writeln(''); end; if(4<=d)and(20>=d)then writeln('Es correcto.') else begin writeln(''); writeln('El tama¤o tiene que estar entre 4 y 20.'); writeln(''); end; until (d>=4)and(d<=20); end; {Este procedimiento sirve para calcular la probabilidad de celulas vivas} procedure probabilidad (var proba:real); begin Repeat writeln(''); writeln('Dime la probabilidad en tanto porciento de que este viva:'); {$I-} repeat readln(proba); {$I+} until ioresult=0; writeln(''); if(proba>=0)and(proba<=1)then writeln('Es correcto.') else begin writeln(''); writeln('La probabilidad tiene que estar entre 0 y 1.'); writeln(''); end; until (proba>0)and(proba<1); end; {Este procedimiento sirve para saber los turnos a realizar por el programa} procedure turno (var turn:integer); begin Repeat begin writeln(''); writeln ('Di cuantos turnos quieres jugar:'); {$I-} repeat readln(turn); {$I+} until ioresult=0; writeln(''); if (turn>=1) then writeln(''); writeln('Es correcto.'); writeln(''); end; until (turn>=1); end; {Aqui declaramos las reglas del juego que quiere utilizar el usuario} procedure regla (var q,w,e:integer); begin writeln(''); writeln('Dime con que reglas quieres jugar:'); writeln(''); Repeat writeln(''); write ('Dame el valor de x: ') ; {$I-} repeat readln(q); {$I+} until ioresult=0; writeln(''); if (q<=8)and(q>=0) then writeln('Es correcto.'); until (q<=8)and(q>=0) ; writeln; Repeat writeln(''); write ('Dame el valor de y: '); {$I-} repeat readln(w); {$I+} until ioresult=0; writeln(''); if (w>=0)and(w<=7) then writeln('Es correcto'); until (w>=0)and(w<=7); writeln; Repeat writeln(''); write ('Dame el valor de z: '); {$I-} repeat readln(e); {$I+} until ioresult=0; writeln(''); if (e>w)and(e<8)then writeln ('Es correcto') else writeln('Tiene que ser un valor mayor que y menor que 8'); until (e>w)and(e<8); end; {Declaramos las variables del programa principal} var opcion,i,l,j,turnos,x,y,z,reglas,cont,k,t,a,p: integer; matriz, matriz1,matriz2:m; pro,prob:real; mundo:String; {Comienza el programa principal} begin {Utilizamos la funci¢n clear para limpiar la pantalla} clrscr; {Mediante la funci¢n textcolor, mejoramos el interfaz} writeln(''); textcolor(green); writeln(' ____________________________ '); writeln(' | |'); writeln(' | EL JUEGO DE LA VIDA. |'); writeln(' |____________________________|'); repeat begin textcolor(11); writeln(''); writeln(''); writeln(''); writeln(' ******MENU****** '); writeln(''); writeln(''); writeln('1.- GENERAR MUNDO ALEATORIO'); writeln(''); writeln('2.- EJECUCION PASO A PASO'); writeln(''); writeln('3.- EJECUCION SOLO RESULTADO'); writeln(''); writeln('4.- CARGAR ESTADO INICIAL'); writeln(''); writeln('5.- EXIT'); writeln(''); textcolor(white); writeln(''); writeln(''); write('ELIGE UNA OPCION:'); {$I-} repeat readln(opcion); {$I+} until ioresult=0; writeln(''); {Con funciones if, realizamos las opciones del menu} {generamos mundo aleatorio} if(opcion=1) then begin dimen (i); probabilidad (pro); {Utilizamos la funcion randomize para que aleatoriamente genere celulas vivas o muertas} randomize; for l:=1 to i do begin writeln(''); for j:=1 to i do begin prob:=random(10)+1; prob:=prob / 10; if(prob >= pro)or(j=1)or(j=i)or(l=1)or(l=i) then begin textcolor(7); matriz[l][j]:=0; end else begin textcolor(12); matriz[l][j]:=1; end; write(matriz[l][j]); end; end; readln; clrscr; end; {Generamos mundo cargando un fichero} if (opcion=4) then begin mundo:=cargar_mundo('mundo.txt'); p:=1; l:=1; {Con este bucle recorremos el string} while (p<=length(mundo)) do begin j:=1; {Con este bucle asignamos los valores del vector a una matriz } while mundo[p]<>'|' do begin matriz[l][j]:=ord(mundo[p])-ord('0'); j:=j+1; p:=p+1; end; l:=l+1; p:=p+1; end; i:=l-1; {Asi dibujamos la matriz} For l:=1 to i do begin writeln(''); for j:=1 to i do begin if(matriz[l][j]=0)then textcolor(7) else textcolor(12); write(matriz[l][j]); end; end; readln; end; {Modo de ejecucion por turnos} if (opcion=2) then begin turno (turnos); begin writeln (''); writeln(''); writeln ('Si quieres jugar con los valores predeterminados, pulsa 1, sino pulsa 2.'); {$I-} repeat readln(reglas); {$I+} until ioresult=0; writeln(''); if (reglas=1) then begin x:=3; y:=2; z:=3; end else regla (x,y,z); end; writeln(''); writeln('Comienza el primer turno'); For a:=1 to turnos do begin if a=1 then else begin textcolor(white); writeln('siguiente turno'); end; {Mediante bucles for recorremos la matriz} For l:=2 to (i-1) do For j:=2 to (i-1) do begin cont:=0; For k:=(l-1) to (l+1) do For t:=(j-1) to (j+1) do begin if (matriz[k][t]=1) then cont:=cont+1; if (matriz[l][j]=1) then begin cont:=cont-1; if (cont=x) then matriz1[l][j]:=1 else matriz1[l][j]:=0; end else if (cont>=y)and(cont<=z) then matriz1[l][j]:=1 else matriz1[l][j]:=0; end; end; For l:=1 to i do begin writeln(''); for j:=1 to i do begin if(matriz1[l][j]=0)then textcolor(7) else textcolor(12); write(matriz1[l][j]); end; end; For l:=1 to i do For j:=1 to i do matriz[l][j]:=matriz1[l][j]; writeln(''); readln; end; clrscr; end; {Modo de ejecuci¢n solo resultado} if (opcion=3) then begin turno (turnos); begin writeln (''); writeln(''); writeln ('Si quieres jugar con los valores predeterminados, pulsa 1, sino pulsa 2.'); {$I-} repeat readln(reglas); {$I+} until ioresult=0; writeln(''); if (reglas=1) then begin x:=3; y:=2; z:=3; end else regla (x,y,z); end; writeln(''); writeln('El resultado final es:'); For a:=1 to turnos do begin For l:=2 to (i-1) do For j:=2 to (i-1) do begin cont:=0; For k:=(l-1) to (l+1) do For t:=(j-1) to (j+1) do begin if (matriz[k][t]=1) then cont:=cont+1; if (matriz[l][j]=1) then begin cont:=cont-1; if (cont=x) then matriz1[l][j]:=1 else matriz1[l][j]:=0; end else if (cont>=y)and(cont<=z) then matriz1[l][j]:=1 else matriz1[l][j]:=0; end; end; For l:=1 to i do For j:=1 to i do matriz[l][j]:=matriz1[l][j]; end; for l:=1 to i do begin writeln(''); for j:=1 to i do begin if(matriz1[l][j]=0)then textcolor(7) else textcolor(12); write(matriz1[l][j]); end; end; readln; end; clrscr; end; {Salida del programa} until (opcion=5); end. ---------------------------- (añadido tras someter) ---------------------------- 20:02:13 10/12/09 -> Segun los datos introducidos los ficheros son: G07P05D2 -> Los autores del trabajo son: BENITO MIANA, SERGIO GONZALO COLOMA, MARIO.A Nombres originales de los archivos entregados -> Juego de la vida.PAS, Juego de la vida.txt