
Dudas varias Batalla Naval Pascal
Publicado por Laira (2 intervenciones) el 26/12/2014 20:37:16
Hola
Estoy intentando terminar el juego de Batalla naval en tablero de 8x8, 2 jugadores.
Creo que algo tengo mal porque sigo y sigo jugando y nunca termina el juego...Además tengo que acabar el juego con la posibilidad de guardar el avance y seguir jugando mas tarde.
El código que llevo hasta ahora es:
Alguien me puede echar una mano? Sobre todo con el guardado/carga de partida porque no tengo ni idea. Gracias
Estoy intentando terminar el juego de Batalla naval en tablero de 8x8, 2 jugadores.
Creo que algo tengo mal porque sigo y sigo jugando y nunca termina el juego...Además tengo que acabar el juego con la posibilidad de guardar el avance y seguir jugando mas tarde.
El código que llevo hasta ahora es:
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
program Hundir_la_flota;
uses crt;
type
tbarco = array [1..8, 1..8] of string;
tcontrol = array [1..8, 1..8] of string;
ttamano = array [1..8, 1..8] of string;
var
i,j: integer;
tablero_T1, tablero_T2: tbarco;
tablero_D1, tablero_D2: tcontrol;
tablero_B1, tablero_B2: ttamano;
conta1,conta2: integer;
fin, fin1, fin2: real;
direccion:string;
tb:tbarco;
tt,tp,tbom:tcontrol;
d1,d2 : tcontrol;
t1,t2 : tbarco;
b1,b2 : ttamano;
procedure Inicia_Tablero_Control (var td:tcontrol); {Se carga el tablero
donde colocar los barcos con todas las posiciones vacias.}
var
i,j:integer;
begin
for i:=1 to 8 do
for j:=1 to 8 do
td[i,j] := 'X';
end;
procedure Inicia_Tablero_Barcos (var tb: tbarco); {Se carga el tablero
donde colocar los barcos con todas las posiciones en Agua}
var
i,j:integer;
begin
for i:= 1 to 8 do
for j:= 1 to 8 do
tb[i,j]:= 'A';
end;
procedure Inicia_Tablero_Tamano (var tt:ttamano); {Se carga el tablero con las dimensiones de cada barco}
var
i,j: integer;
begin
for i:= 1 to 8 do
for j:= 1 to 8 do
tt[i,j]:= '';
end;
procedure Introducir_Barcos (var tb: tbarco; tp: tcontrol); {Se carga el tablero
donde el jugador inserta los barcos}var
i,j:integer;
direccion: string;
begin
repeat
writeln('Introduce la coordenada FILA del barco 1: ');
readln(i);
writeln('Introduce la coordenada COLUMNA del barco 1: ');
readln(j);
if (i>8) and (j>8) then
writeln('Coordenadas fuera de rango, utilice valores de 1 a 8.');
until i<=8;
begin
tp[i,j]:='B';
tb[i,j]:='UNO';
end;
writeln('¿En que direccion quiere colocar el barco 2: arriba/abajo/derecha/izquierda?:');
readln(direccion);
repeat
writeln('Introduce la coordenada FILA del barco 2:');
read(i);
writeln('Introduce la coordenada COLUMNA del barco 2: ');
readln(j);
if (i>8) and (j>8) then
writeln('Coordenadas fuera de rango, utilice valores de 1 a 8. ');
until i<=8;
if direccion = 'izquierda' then
for j:= j downto 2 do
begin
tp[i,j]:= 'B';
tb[i,j]:='DOS';
end
else
if direccion = 'derecha' then
for j:= j to 2 do
begin
tp[i,j]:= 'B';
tb[i,j]:='DOS';
end
else
if direccion = 'arriba' then
for i := i downto 2 do
begin
tp[i,j]:= 'B';
tb[i,j]:='DOS';
end
else
if direccion = 'abajo' then
for i := i to 2 do
begin
tp[i,j]:= 'B';
tb[i,j]:='DOS';
end;
writeln('¿En que direccion quiere colocar el barco 3: arriba/abajo/derecha/izquierda?:');
readln(direccion);
repeat
writeln('Introduce la coordenada FILA del barco 3:');
read(i);
writeln('Introduce la coordenada COLUMNA del barco 3: ');
readln(j);
if (i>8) and (j>8) then
writeln('Coordenadas fuera de rango, utilice valores de 1 a 8. ');
until i<=8;
if direccion = 'izquierda' then
for j:= j downto 3 do
begin
tp[i,j]:= 'B';
tb[i,j]:='TRES';
end
else
if direccion = 'derecha' then
for j:= j to 3 do
begin
tp[i,j]:= 'B';
tb[i,j]:='TRES';
end
else
if direccion = 'arriba' then
for i := i downto 3 do
begin
tp[i,j]:= 'B';
tb[i,j]:='TRES';
end
else
if direccion = 'abajo' then
for i := i to 3 do
begin
tp[i,j]:= 'B';
tb[i,j]:='TRES';
end;
writeln('¿En que direccion quiere colocar el barco 4: arriba/abajo/derecha/izquierda?:');
readln(direccion);
repeat
writeln('Introduce la coordenada FILA del barco 4:');
read(i);
writeln('Introduce la coordenada COLUMNA del barco 4: ');
readln(j);
if (i>8) and (j>8) then
writeln('Coordenadas fuera de rango, utilice valores de 1 a 8. ');
until i<=8;
if direccion = 'izquierda' then
for j:= j downto 4 do
begin
tp[i,j]:= 'B';
tb[i,j]:='CUATRO';
end
else
if direccion = 'derecha' then
for j:= j to 4 do
begin
tp[i,j]:= 'B';
tb[i,j]:='CUATRO';
end
else
if direccion = 'arriba' then
for i := i downto 4 do
begin
tp[i,j]:= 'B';
tb[i,j]:='CUATRO';
end
else
if direccion = 'abajo' then
for i := i to 4 do
begin
tp[i,j]:= 'B';
tb[i,j]:='CUATRO';
end;
end;
procedure MuestraBombardeados (tbom:tcontrol);
var
i,j:integer;
begin
for i:= 1 to 8 do
begin
for j:= 1 to 8 do
begin
write(tbom[i,j],' ');
end;
writeln;
end;
end;
procedure Disparos (var t1,t2: tbarco; d1,d2: tcontrol; b1,b2: ttamano); {Se inicia el proceso de juego}
var
i,j: integer;
conta1,conta2 :integer;
fin, fin1, fin2 : real;
begin
conta1 :=0;
conta2 :=0;
begin
while fin <>1 do
begin
fin1 :=4;
fin2 :=4;
repeat
begin
writeln;
writeln('Es el turno del jugador 1');
writeln;
writeln('El estado del tablero de disparos es: ');
begin
MuestraBombardeados(d1)
end;
repeat
writeln;
writeln('Introduzca la coordenada FILA de barco a atacar: ');
readln(i);
if i>8 then
begin
writeln;
writeln('La coordenada indicada esta fuera de rango, intentelo de nuevo con valores entre 1 y 8.');
end;
until i<=8;
repeat
writeln;
writeln('Introduzca la coordenada COLUMNA de barco a atacar: ');
readln(j);
if j>8 then
begin
writeln;
writeln('La coordenada indicada esta fuera de rango, intentelo de nuevo con valores entre 1 y 8.');
end;
until j<=8;
if t2[i,j]='B' then
begin writeln;
writeln('TOCADO, ha alcanzado un barco de dimensión ',b2[i,j]);
d1[i,j]:='B';
conta1 := conta1 + 1;
end
else
begin
writeln;
writeln('AGUA');
writeln;
writeln('Ha perdido el turno.');
d1[i,j]:='A';
end;
if t2[i,j]<> 'B' then
begin
fin1 := 2;
end;
if conta1 >=10 then
begin
fin1 := 2;
writeln;
writeln('FELICIDADES!!, ha GANADO el jugador 1');
end;
end;
until fin1 = 2;
if conta1 >= 10 then
begin
fin := 1
end;
if conta1 <10 then
begin
repeat
begin
writeln;
writeln('Es el turno del jugador 2');
writeln;
writeln('El estado del tablero de disparos es: ');
begin
MuestraBombardeados(d1)
end;
end;
repeat
writeln;
writeln('Introduzca la coordenada FILA de barco a atacar: ');
readln(i);
if i>8 then
begin
writeln;
writeln('La coordenada indicada esta fuera de rango, intentelo de nuevo con valores entre 1 y 8.');
end;
until i<=8;
repeat
writeln;
writeln('Introduzca la coordenada COLUMNA de barco a atacar: ');
readln(j);
if j>8 then
begin
writeln;
writeln('La coordenada indicada esta fuera de rango, intentelo de nuevo con valores entre 1 y 8.');
end;
until j<=8;
if t1[i,j]='B' then
begin writeln;
writeln('TOCADO, ha alcanzado un barco de dimensión ',t2[i,j]);
d2[i,j]:='B';
conta2 := conta2 +1;
end
else
begin
writeln;
writeln('AGUA');
writeln;
writeln('Ha perdido el turno.');
d2[i,j]:='A';
end;
if t1[i,j]<>'B' then
begin
fin2:=2;
end;
if conta2>=10 then
begin
fin2 := 2;
writeln;
writeln('FELICIDADES!!, ha GANADO el jugador 1');
writeln;
end;
end;
until fin2 = 2;
end;
if conta2>=10 then
begin
fin := 1;
end;
end;
end;
end;
begin (*del programa principal*);
writeln ('Bienvenido al juego Hundir la Flota');
Inicia_Tablero_Barcos(tablero_T1);
Inicia_Tablero_Barcos(tablero_T2);
Inicia_Tablero_Control(tablero_D1);
Inicia_Tablero_Control(tablero_D1);
Inicia_Tablero_Tamano(tablero_B1);
Inicia_Tablero_Tamano(tablero_B2);
writeln;
writeln('JUGADOR 1 introduzca sus barcos');
writeln;
introducir_barcos(tablero_T1,tablero_D1);
writeln;
writeln('JUGADOR 2 introduzca sus barcos');
writeln;
introducir_barcos(tablero_T2,tablero_D2);
Disparos(tablero_T1,tablero_T2,tablero_D1,tablero_D2,tablero_B1,tablero_B2);
end.
Alguien me puede echar una mano? Sobre todo con el guardado/carga de partida porque no tengo ni idea. Gracias
Valora esta pregunta


0