elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: AIO elhacker.NET 2021 Compilación herramientas análisis y desinfección malware


  Mostrar Mensajes
Páginas: [1]
1  Programación / Programación General / Consulta Pascal en: 22 Noviembre 2018, 16:11 pm
Buenos días!! disculpen que lo publique acá pero no se donde publicarlo no encontré ninguna sección de pascal... Estoy haciendo un batalla naval para la facultad (estoy en primer año) y tengo que hacer que los barcos se carguen random en la matriz, sin que colapsen entre si y sin que sean contiguos(No importa si son contiguos en diagonal) logre hacer prácticamente todo pero tengo un problema y es que aveces me carga perfectamente la matriz con los barcos sin que se choquen entre si, pero aveces también me tira un runtime 201 como si se saliera de rango en algún momento pero no logro ver/entender cuando pasa eso y como debería corregirlo... Muchas gracias!! y perdón por publicarlo acá necesito ayuda urgente.
Código:
program BatallaNaval;
const
        MinF=1;
        MaxF=15;
        MinC=1;
        MaxC=15;
        CantBarcos=5;
        MaxJugadas=10;
type
        Matriz=Array[MinF..MaxF,MinC..MaxC] of char;
Function BarcoValidoHorizontal(Tablero:matriz;c,f,t:integer):boolean;
var
b:integer;
begin
    b:=1;
    BarcoValidoHorizontal:=False;
    If c+t<MaxC then begin
        If ((Tablero[f,c]<>'B') and (Tablero[f,c-1]<>'B')) then begin
            If Tablero[f,c+1]<>'B' then begin
                While b<>t do begin
                    if (((Tablero[f,c+1]<>'B') and (Tablero[f-1,c]<>'B')) and (Tablero[f+1,c]<>'B')) then begin
                        c:=c+1;
                        b:=b+1;
                    end
                    else
                        BarcoValidoHorizontal:=False;
                end;
                If b=t then
                    BarcoValidoHorizontal:=True;
            end;
        end
        else
            BarcoValidoHorizontal:=False;
    end
    else
        BarcoValidoHorizontal:=false;
end;
Function BarcoValidoVertical(Tablero:matriz;c,f,t:integer):boolean;
var
b:integer;
begin
    b:=1;
    BarcoValidoVertical:=False;
    If f+t<Maxf then begin
        If ((Tablero[f,c]<>'B') and (Tablero[f-1,c]<>'B')) then begin
            If Tablero[f+1,c]<>'B' then begin
                While b<>t do begin
                    if (((Tablero[f+1,c]<>'B') and (Tablero[f,c-1]<>'B')) and (Tablero[f,c+1]<>'B')) then begin
                        f:=f+1;
                        b:=b+1;
                    end
                    else
                        BarcoValidoVertical:=False;
                end;
                If b=t then
                    BarcoValidoVertical:=True;
            end;
        end
        else
            BarcoValidoVertical:=False;
    end
    else
        BarcoValidoVertical:=false;
end;
procedure GenerarTablero(var Tablero:Matriz);
var
       c,f,i,j,b,t,p,k:integer;
begin
        For i:=1 to MaxC do
                For j:=1 to MaxF do
                        Tablero[i,j]:='*';
        k:=0;
        While k<CantBarcos do begin
                b:=0;
                randomize;
                c:=Random(14)+1;
                f:=Random(14)+1;
                t:=Random(4)+2;
                p:=Random(2);
                If p=1 then begin
                    If BarcoValidoHorizontal(Tablero,c,f,t)=True then begin
                        While (b<=t) do begin
                               Tablero[f,c]:='B';
                                c:=c+1;
                                b:=b+1;
                        end;
                    end
                    else
                        k:=k-1;
                end
                else
                    If BarcoValidoVertical(Tablero,c,f,t)=true then begin
                        While (b<=t) do begin
                                Tablero[f,c]:='B';
                                f:=f+1;
                                b:=b+1;
                        end;
                    end
                    else
                        k:=k-1;
                k:=k+1;
        end;
end;
procedure Mostrar(Tablero:matriz);
var
        c,f:integer;
begin
        For c:=1 to maxC do begin
                For f:=1 to MaxF do
                        write(Tablero[c,f]);
                        writeln;
        end;
end;
var
Tablero:matriz;
begin
GenerarTablero(Tablero);
Mostrar(Tablero);
end.
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines