---------------------------------------------------------------------- -- AUTOR: Eduardo Jim‚nez Chapresto -- PROYECTO: Ejercicio de clase -- FECHA: 12 de enero del 2000 -- Modificado por: -- OBJETIVO: Juego del soldado y el campo de minas. ---------------------------------------------------------------------- with ada.Text_IO, ada.integer_text_io, screen; -- screen: biblioteca con instrucciones sobre use ada.Text_IO, ada.integer_text_io, screen; -- el control de la pantalla. with ada.Numerics.Discrete_Random; -- para los n£meros aleatorios. procedure minas is LONG :constant integer := 20; -- Longitud del tablero. ALT :constant integer := 20; -- Altura del tablero. ALEATORIOS:constant integer := 20; LUGARENT:constant integer := 15; -- Los lugares de entrada y salida LUGARSAL:constant integer := 5; -- est n predefinidos. NUMBOMBAS:constant integer := 10; type tpCasilla is (Vacio, Mina, Camino, Jugador); type tpTablero is array(1..LONG,1..ALT) of tpCasilla; subtype tpAleatorios is integer range 1..ALEATORIOS; type tpPosicion is record x :integer range 1..LONG; y :integer range 1..ALT; end record; type tpEstado is (vivo, muerto, fin, salir); package pRandom is new Ada.Numerics.Discrete_Random(tpAleatorios); use pRandom; tablero :tpTablero; pos :tpPosicion; estado :tpEstado:=vivo; c :character; procedure dibujaTablero(tablero :in tpTablero) is begin Move_Cursor(1,30); put("Ú"); for i in 1..LONG loop put("Ä"); end loop; put("¿"); for i in 1..ALT loop Move_Cursor(i+1, 30); if i /= LUGARENT then put("³"); else put(" "); end if; for j in 1..LONG loop if tablero(j,i) = Vacio or tablero(j,i) = Mina then put(" "); elsif tablero(j,i) = Camino then put("."); elsif tablero(j,i) = Jugador then put(CHARACTER'VAL(1)); end if; end loop; if i /= LUGARSAL then put("³"); end if; end loop; Move_Cursor(ALT + 2, 30); put("À"); for i in 1..LONG loop put("Ä"); end loop; put("Ù"); end dibujaTablero; procedure InicializaTablero(tablero :out tpTablero) is g :pRandom.Generator; kx,ky :integer; begin pRandom.Reset(g); -- inicializa g a partir del tiempo del reloj. for i in 1..ALT loop for j in 1..LONG loop tablero(i,j):=Vacio; -- vaciamos el tablero primero end loop; end loop; for i in 1..NUMBOMBAS loop kx:=pRandom.Random(g); ky:=pRandom.Random(g); if (kx/=1) or (ky/=LUGARENT) or (kx/=LONG) or (ky/=LUGARSAL) then tablero(kx,ky):=Mina; -- Ahora colocamos las minas, excepto end if; -- en el caso de que est‚n en la salida end loop; -- o en la entrada tablero(1, LUGARENT) := Jugador; -- colocamos al jugador en la entrada end InicializaTablero; function teclaPulsada return character is esta :boolean := FALSE; c :character; begin while not esta loop get_immediate(c, esta); end loop; return c; end teclaPulsada; function posValida(x,y :in integer) return boolean is begin if (x=0) or (y=0) or (x=LONG + 1) or (y=ALT + 1) then return FALSE; else return TRUE; end if; end posValida; procedure mueveJugador(pos:in out tpPosicion; c:in character) is -- Este procedimiento mueve al Jugador s¢lo si se ha presionado una tecla -- correcta (es decir, si se presiona una tecla que no significa nada o si -- se sale del tablero el jugador no se mueve) y coloca un punto en el lugar -- donde estaba antes el jugador. begin if c='2' and posValida(pos.x, pos.y + 1) then Move_Cursor(pos.y+1, pos.x+30); put("."); pos.y:=pos.y + 1; Move_Cursor(pos.y+1, pos.x+30); put(CHARACTER'VAL(1)); elsif c='4' and posValida(pos.x - 1, pos.y) then Move_Cursor(pos.y+1, pos.x+30); put("."); pos.x:=pos.x - 1; Move_Cursor(pos.y+1, pos.x+30); put(CHARACTER'VAL(1)); elsif c='6' and posValida(pos.x + 1, pos.y) then Move_Cursor(pos.y+1, pos.x+30); put("."); pos.x:=pos.x + 1; Move_Cursor(pos.y+1, pos.x+30); put(CHARACTER'VAL(1)); elsif c='8' and posValida(pos.x, pos.y - 1) then Move_Cursor(pos.y+1, pos.x+30); put("."); pos.y:=pos.y - 1; Move_Cursor(pos.y+1, pos.x+30); put(CHARACTER'VAL(1)); end if; end mueveJugador; function mina(tablero:in tpTablero; pos:in tpPosicion) return boolean is begin if tablero(pos.x, pos.y)=mina then return TRUE; else return FALSE; end if; end mina; procedure muerte(pos:in tpPosicion) is c :character; begin Move_Cursor(23, 1); Put(" Lo siento, has pisado una mina en la posici¢n "); put(pos.x, 0); put(", "); put(pos.y, 0); new_line; put(" Presione una tecla para continuar "); c := teclaPulsada; end muerte; procedure victoria is c :character; begin Move_Cursor(23, 1); Put(" Enhorabuena, has conseguido llegar a la salida"); new_line; put(" Presione una tecla para continuar "); c := teclaPulsada; end victoria; begin Clear_Screen; -- Borra la pantalla, del screen.adb InicializaTablero(tablero); dibujaTablero(tablero); pos.x:=1; pos.y:=LUGARENT; while estado=vivo loop c := teclaPulsada; mueveJugador(pos, c); if mina(tablero, pos) then estado:=muerto; Move_Cursor(pos.y, pos.x); put("X"); end if; if pos.x=LONG and pos.y=LUGARSAL then estado := fin; end if; if c='q' or c='Q' then estado:=salir; end if; end loop; case estado is when muerto => muerte(pos); when fin => victoria; when others => null; end case; end minas;