with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Synchronous_Task_Control; use Ada.Text_IO, Ada.Integer_Text_IO, Ada.Synchronous_Task_Control; package body Tron_IO is procedure Movecursor (Column : T_COORDINATA_X; Row : T_COORDINATA_Y) is -- sposta il cursore in una posizione dello schermo begin -- comandi del VT100 per muovere il cursore Put(ASCII.ESC); Put("["); Put(Row, width => 1); Put(';'); Put(Column, width => 1); Put('f'); end Movecursor; procedure ClearScreen is begin -- comandi del VT100 per pulire lo schermo Put(ASCII.ESC); Put_line("[2J"); end ClearScreen; procedure Scontro (Mat_Gioco : in out T_Matrice; X : in out T_COORDINATA_X; Y : in out T_COORDINATA_Y; Gio : T_GIOCATORE ) is begin -- se il giocatore si sposta in una posizione gia' accupata (mat_gioco(y,x) /= 0) -- allora quel giocatore si e' scontrato if gio = 1 and mat_gioco(y,x) /= 0 then raise scontro1; elsif gio = 2 and mat_gioco(y,x) /= 0 then raise scontro2; elsif gio = 3 and mat_gioco(y,x) /= 0 then raise scontro_computer; else -- altrimenti attualizzo la matrice mettendo il numero del giocatore -- nella posizione in cui si e' spostato mat_gioco(y,x) := gio; end if; end Scontro; procedure Avanti (Gio : T_Giocatore; X : in out T_COORDINATA_X; Y : in out T_COORDINATA_Y; Dir : in T_Direzione; Symbol : in CHARACTER) is begin case dir is when Up => -- se sto muovendomi verso l'alto y:= y-1; -- devo decrementare l'indice di riga y if y = 0 then -- esco a Nord y := line; -- rientro a Sud end if; Movecursor(x,y); Put( symbol ); -- visualizzo la mossa del giocatore Scontro (mat, x, y, gio); -- controllo se e' avvenuto lo scontro when Down => -- se sto muovendomi verso il basso y:= y+1; -- devo incrementare l'indice di riga y if y = line+1 then -- esco a Sud y := 1; -- rientro a Nord end if; Movecursor(x,y); Put( symbol ); Scontro (mat, x, y, gio); when Right => -- se sto muovendomi verso destra x := x+1; -- devo incrementare l'indice di colonna x if x = col+1 then -- esco a Est x := 1; -- rientro a Ovest end if; Movecursor(x,y); Put( symbol ); Scontro (mat, x, y, gio); when Left => -- se sto muovendomi verso sinistra x := x-1; -- devo decrementare l'indice di colonna x if x = 0 then -- esco a Ovest x := col; -- rientro a Est end if; Movecursor(x,y); Put( symbol ); Scontro (mat, x, y, gio); end case; end Avanti; procedure Destra (Dir : in out T_Direzione) is begin -- cambio il valore di dir se voglio spostarmi verso destra case dir is when Up => Dir := Right; when Down => Dir := Left; when Right => Dir := Down; when Left => Dir := Up; end case; end Destra; procedure Sinistra (Dir : in out T_Direzione) is begin -- cambio il valore di dir se voglio spostarmi verso sinistra case dir is when Up => Dir := Left; when Down => Dir := Right; when Right => Dir := Up; when Left => Dir := Down; end case; end Sinistra; -- schermata per la scelta del livello di difficolta' procedure Schermata_2(time : in out DURATION; no_continue : in out BOOLEAN) is ch4 : CHARACTER; -- variabile che prende la scelta dell'utente begin clearscreen; movecursor(1,1); Put("********************************************************************************"); Put("********************************************************************************"); Put("** **"); Put("** **"); Put("** Scegli livello **"); Put("** **"); Put("** di difficolta' **"); Put("** **"); Put("** **"); Put("** **"); Put("** **"); Put("** **"); Put("** 1 - Semplice **"); Put("** **"); Put("** 2 - Normale **"); Put("** **"); Put("** 3 - Difficile **"); Put("** **"); Put("** ESC - Schermata **"); Put("** precedente **"); Put("** **"); Put("** **"); Put("********************************************************************************"); Put("********************************************************************************"); loop Get_immediate(ch4); -- prendo la scelta case ch4 is -- discuto la scelta when '1' => time := 0.2; -- questo valore andra' a finire in Time1 del programma principale exit; when '2' => time := 0.1; -- questo valore andra' a finire in Time1 del programma principale exit; when '3' => time := 0.05; -- questo valore andra' a finire in Time1 del programma principale exit; when ASCII.ESC => no_continue := true; -- mi permette di tornare alla schermata precedente exit; when others => -- catturo la pressione di altri tasti null; end case; end loop; end Schermata_2; --schermata dove si sceglie il tipo di gioco procedure Schermata_principale(ch : in out CHARACTER; s : in out Suspension_Object; time : in out DURATION; x1,x2 : in T_COORDINATA_X; y1,y2 : in T_COORDINATA_Y) is ch2,ch3 : CHARACTER; -- variabile che prendono la scelta dell'utente file_di_istruzioni : FILE_TYPE; -- nome logico del file delle istruzioni no_continue : BOOLEAN; --variabile che passo alla schermata 2 begin mat := (others => (others => 0)); -- inizializzo la matrice per iniziare poi il gioco <> -- etichetta dove viente indirizzato il goto clearscreen; movecursor(1,1); no_continue := false; Put("********************************************************************************"); Put("********************************************************************************"); Put("** **"); Put("** ------- ------ --- - - **"); Put("** | | | -- \ / - \ | \ | | **"); Put("** -- -- | | \ | / / \ \ | \ | | **"); Put("** | | | | | | | | | | | \ \ | | **"); Put("** | | | | / | | | | | | |\ \| | **"); Put("** | | | -- / | | | | | | \ | | **"); Put("** | | | --- \ | | | | | | \ | **"); Put("** | | | | \ | \ \ / / | | | | **"); Put("** | | | | | | \ - / | | | | **"); Put("** - - - --- - - **"); Put("** **"); Put("** 1 - Sfida il Computer **"); Put("** **"); Put("** 2 - Sfida a 2 giocatori **"); Put("** **"); Put("** 3 - Istruzioni **"); Put("** **"); Put("** ESC - Termina **"); Put("** **"); Put("********************************************************************************"); Put("********************************************************************************"); loop Get_immediate(ch); -- prendo la scelta dell'utente case ch is when '1' => --Faccio partire il programma per la sfida con il computer schermata_2(time,no_continue); -- scelta del livello di difficolta' if no_continue then -- se l'utente ha premuto ESC allora no_continue = true goto inizio; -- e torno all'inizio end if; -- preparo la schermata per iniziare a giocare ClearScreen; movecursor(x1,y1); Put('#'); Mat(y1,x1) := 1; movecursor(x2,y2); Put('*'); Mat(y2,x2) := 3; Set_True(s); exit; when '2' => --Faccio partire il programma per la sfida a 2 giocatori schermata_2(time,no_continue); if no_continue then goto inizio; end if; ClearScreen; movecursor(x1,y1); Put('#'); Mat(y1,x1) := 1; movecursor(x2,y2); Put('*'); Mat(y2,x2) := 2; Set_True(s); exit; when '3' => --Apro la pagina delle istruzioni ClearScreen; Movecursor(1,1); open(File => file_di_istruzioni, Mode => In_File, Name => "istruzioni.txt", Form => ""); while not end_of_file(file_di_istruzioni) loop while not end_of_line(file_di_istruzioni) loop Get(file_di_istruzioni,ch3); if ch3 = '<' then -- per visualizzare il contenuto del file in due parti Get_immediate(ch3); -- prendo la scelta dell'utente if ch3 = ASCII.ESC then -- se ESC close(File => file_di_istruzioni); -- chiudo il file goto inizio; -- e torno alla schermata iniziale end if; ClearScreen; movecursor(1,1); exit; end if; Put(ch3); delay 0.003; -- stampo i il file un carattere per volta end loop; Skip_Line(file_di_istruzioni); New_line; end loop; close(File => file_di_istruzioni); loop Get_immediate(ch2); if ch2 = ASCII.ESC then goto inizio; end if; end loop; when ASCII.ESC => -- Esco completamente dal gioco clearscreen; Set_True(s); exit; when others => null; end case; end loop; end Schermata_principale; end Tron_IO;