Pascal/Turbo Pascal - Batalla naval

 
Vista:
sin imagen de perfil

Batalla naval

Publicado por Juan Manuel (2 intervenciones) el 19/11/2018 21:25:04
Buenas!! necesito ayuda, tengo que hacer un batalla naval pero que me cargue los barcos en el tablero de forma aleatoria, y no tengo idea de como manejar el randomize. Alguien me podria ayudar?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Batalla naval

Publicado por Juan Manuel (2 intervenciones) el 22/11/2018 04:13:58
Buenas!! logre hacer esto hasta el momento pero me ocurre un problema y es que cada tanto al ejecutarlo me tira un runtime 201 o directamente no hace nada, pero aveces si se ejecuta bien y me carga los barcos perfectamente... Que puede ser y como lo corrijo?? muchas gracias!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar