Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- with Ada.Real_Time; use Ada.Real_Time;
- with Ada.Text_IO; use Ada.Text_IO;
- with generarAleatorio; use generarAleatorio;
- with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
- with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
- procedure carreraCaballosMod is
- finCarrera : Boolean := False;
- T : Time; -- Controlar el tiempo
- Periodo : Time_Span := Milliseconds(10); -- Tiempo de cada ciclo
- distancia : Integer := 0;
- caras : Integer; -- Caras del dado
- L : Integer; -- Distancia del ciruito
- N : Integer := 4; --Número de caballos
- Max : constant Integer := 4; -- Máximo número de caballos permitido
- type Ciclo is mod 6;
- Turno : Ciclo := 0;
- ganador : Character := ' ';
- -- Array para guardar las posiciones
- type tipoPosiciones is array(Integer range 1..Max) of Integer;
- -- Array para los caballos
- type tipoCaballos is array(Positive range 1..Max) of Character;
- posiciones : tipoPosiciones := (others=>0);
- caballos : tipoCaballos := ('A', 'B', 'C', 'D');
- -- Caballo 1
- task C1 is
- entry C1;
- end;
- task body C1 is
- begin
- distancia := 0;
- loop
- accept C1 do
- distancia := Generate_Number(1, caras);
- posiciones(1) := posiciones(1) + distancia;
- end C1;
- exit when finCarrera;
- end loop;
- end C1;
- -- Caballo 2
- task C2 is
- entry C2;
- end;
- task body C2 is
- begin
- distancia := 0;
- loop
- accept C2 do
- distancia := Generate_Number(1, caras);
- posiciones(2) := posiciones(2) + distancia;
- end C2;
- exit when finCarrera;
- end loop;
- end C2;
- -- Caballo 3
- task C3 is
- entry C3;
- end;
- task body C3 is
- begin
- distancia := 0;
- loop
- accept C3 do
- distancia := Generate_Number(1, caras);
- posiciones(3) := posiciones(3) + distancia;
- end C3;
- exit when finCarrera;
- end loop;
- end C3;
- -- Caballo 4
- task C4 is
- entry C4;
- end;
- task body C4 is
- begin
- distancia := 0;
- loop
- accept C4 do
- distancia := Generate_Number(1, caras);
- posiciones(4) := posiciones(4) + distancia;
- end C4;
- exit when finCarrera;
- end loop;
- end C4;
- -- Al final de cada ronda el comentarista actualiza la posicion de los caballos
- task comentarista is
- entry comentarista;
- end;
- task body comentarista is
- begin
- loop
- accept comentarista do
- Put_Line(" ");
- for i in 1..N loop
- Put(caballos(i)'Image &" "); -- Imprimimos el nombre de los caballos
- end loop;
- Put_Line(" ");
- for i in 1..N loop
- Put(posiciones(i)'Image &" "); -- Imprimimos la distancia recorrida por cada caballo
- if (posiciones(i) >= L) then -- Comprueba si un caballo ha llegado a la meta
- finCarrera := true;
- ganador := caballos(i);
- end if;
- end loop;
- end comentarista;
- exit when finCarrera;
- end loop;
- end comentarista;
- begin
- -- Introducimos los datos de la carrera
- Put_Line("Introduza la longitud de la pista: ");
- Get(L);
- Put_Line("Introduza el numero de caras del dado: ");
- Get(caras);
- T := Clock;
- for i in 1..20 loop
- while (finCarrera = False) loop
- T := T + Periodo;
- Delay until (T);
- case Turno is
- -- Se ejecutan las tareas en funcion de la planificacion temporal
- when 0 => C1.C1; C2.C2; C3.C3; C4.C4;
- when 1 => C1.C1; C2.C2;
- when 2 => C1.C1; C3.C3;
- when 3 => C1.C1; C2.C2; C4.C4;
- when 4 => C1.C1; C3.C3;
- when 5 => C1.C1; comentarista.comentarista;
- end case;
- Turno := Turno + 1;
- end loop;
- end loop;
- Put_Line(" ");
- Put_Line("Ha terminado la carrera");
- Put_Line("El ganador es "&ganador'Img);
- end carreraCaballosMod;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement