-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtablero.py
316 lines (268 loc) · 10.6 KB
/
tablero.py
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
import pygame
import random as r
from config import BLACK, TAMAÑO_CUADROS, ESPACIO, FILAS, COLUMNAS, ANCHO, ALTO, BLUE, SIZE
# Agregar una estructura de datos para rastrear las casillas clickeadas
casillas_clickeadas = set()
barcos_ubicados = set()
barcos_enemigo = set()
click_enemigo = set()
# board = [[0 for _ in range(SIZE)] for _ in range(SIZE)]
# casillas_seleccionables = set()
# Reglas del juego de Batalla Naval
reglas_del_juego = [
"Tipos de barcos:",
"1. Lanchas (1 casilla)",
"2. Submarinos (2 casillas)",
"3. Buques (3 casillas)",
"",
"Reglas del Juego:",
"1. Coloca tus barcos en el tablero, estos no pueden",
" ir en diagonal. El orden para posicionar es Lancha,",
" Submarino y Buque",
"2. Intenta adivinar la ubicación de los barcos",
" del oponente haciendo clic en las casillas.",
"3. Gana el jugador que hunde todos los barcos",
" del oponente primero."
]
def is_in_barcos(casilla):
for c in barcos_ubicados:
if(c == casilla):
return True
return False
def is_in_list(casilla, casillas):
for c in casillas:
if(c == casilla):
return True
return False
def is_alround(casilla, casillas):
print(casilla, casillas)
if not casillas:
return False
if len(casillas) == 1:
c = casillas[0]
if (
(casilla == (c[0] - 1, c[1])) or
(casilla == (c[0] + 1, c[1])) or
(casilla == (c[0], c[1] - 1)) or
(casilla == (c[0], c[1] + 1)) or
seleccion_diagonal(casilla, casillas)
):
return True
x_values = [c[0] for c in casillas]
y_values = [c[1] for c in casillas]
min_x, min_y, max_x, max_y = min(x_values), min(y_values), max(x_values), max(y_values)
is_vertical = max_x - min_x == 0
if is_vertical:
if casilla[0] == min_x - 1 or casilla[0] == max_x + 1:
return True
else:
if casilla[1] == min_y - 1 or casilla[1] == max_y + 1:
return True
return False
def seleccion_diagonal(casilla, casillas):
for c in casillas:
if(c == (casilla[0] -1, casilla[1] - 1)):
return True
elif(c == (casilla[0] + 1, casilla[1] + 1)):
return True
elif(c == (casilla[0] - 1, casilla[1] +1 )):
return True
elif(c == (casilla[0] +1 , casilla[1] - 1 )):
return True
return False
def inicializar_screen():
pygame.init()
screen = pygame.display.set_mode((ANCHO, ALTO))
pygame.display.set_caption("La Batalla Naval")
return screen
def draw_board_with_ships(surface, title, x_offset, y_offset):
element_x = 50
element_y = 50 # Ajusta la posición vertical del tablero
font = pygame.font.Font(None, 36)
text = font.render(title, True, BLACK)
text_rect = text.get_rect()
text_rect.centerx = (
element_x
+ (COLUMNAS * TAMAÑO_CUADROS + (COLUMNAS - 1) * ESPACIO) // 2
+ x_offset
)
text_rect.centery = element_y // 2 + y_offset # Centra el título verticalmente
surface.blit(text, text_rect)
for fila in range(FILAS):
for col in range(COLUMNAS):
x = element_x + col * (TAMAÑO_CUADROS + ESPACIO) + x_offset
y = element_y + fila * (TAMAÑO_CUADROS + ESPACIO) + y_offset
pygame.draw.rect(
surface,
BLACK,
(x, y, TAMAÑO_CUADROS, TAMAÑO_CUADROS),
1,
)
# Dibujar cuadrado azul
if (fila, col) in barcos_ubicados:
pygame.draw.rect(surface, BLUE, (x, y, TAMAÑO_CUADROS, TAMAÑO_CUADROS))
# Reglas del juego
reglas_rect = pygame.Rect(
element_x + (COLUMNAS * TAMAÑO_CUADROS + (COLUMNAS - 1) * ESPACIO) + 20 + x_offset,
element_y + y_offset,
500,
500 # Ajusta el tamaño del cuadro de reglas
)
pygame.draw.rect(surface, BLACK, reglas_rect, 2)
# Dibuja las reglas en el cuadro de texto
font = pygame.font.Font(None, 24)
for i, regla in enumerate(reglas_del_juego):
regla_text = font.render(regla, True, BLACK)
regla_rect = regla_text.get_rect()
regla_rect.topleft = (
reglas_rect.left + 10,
reglas_rect.top + 10 + i * 30 # Ajusta el espaciado vertical entre las reglas
)
surface.blit(regla_text, regla_rect)
def randomShips():
# barcos_ubicados = set()
ship_sizes = [3, 2, 1]
for ship_size in ship_sizes:
while True:
orientation = r.choice(["horizontal", "vertical"])
if orientation == "horizontal":
x = r.randint(1, 10 - ship_size) # Aseguramos que el barco quepa en el tablero
y = r.randint(1, 10)
new_ship = {(x + i, y) for i in range(ship_size)}
else:
x = r.randint(1, 10)
y = r.randint(1, 10 - ship_size)
new_ship = {(x, y + i) for i in range(ship_size)}
# Verificamos que los barcos no se toquen
if all(coord not in barcos_ubicados for coord in new_ship):
barcos_ubicados.update(new_ship)
break
# return barcos_ubicados
def randomBoardSets():
barcos_ubicados.clear()
ships = {
"p": [],
"b": [],
"s":[]
}
for tamano in [3, 2 ,1]:
# print("tamano", tamano)
while True:
x = r.randint(0, SIZE - 1)
y = r.randint(0, SIZE - 1)
while(x + tamano >= SIZE):
x = r.randint(0, SIZE - 1)
while(y + tamano >= SIZE ):
y = r.randint(0, SIZE - 1)
direccion = r.choice(['horizontal', 'vertical'])
if direccion == 'horizontal':
if (x, y) in barcos_ubicados:
continue
ocupado = False
for i in range(tamano):
if (x + i, y) in barcos_ubicados:
ocupado = True
break
if ocupado:
continue
for i in range(tamano):
if(i == 0):
# print("entro i, tamano:", tamano)
if(tamano == 3):
ships["s"] = [x, y + i, 1]
elif(tamano == 2):
ships["b"] = [x, y + i, 1]
else:
ships["p"] = [x, y+i, 1]
# positions_set.add((x + i, y))
barcos_ubicados.add((x, y+i))
else:
if (x, y) in barcos_ubicados:
continue
ocupado = False
for i in range(tamano):
if (x, y + i) in barcos_ubicados:
ocupado = True
break
if ocupado:
continue
for i in range(tamano):
if(i == 0):
if(tamano == 3):
ships["s"] = [x + i, y, 0]
elif(tamano == 2):
ships["b"] =[x + i, y, 0]
else:
ships["p"]= [x+ i, y , 0]
# positions_set.add((x, y + i))
barcos_ubicados.add((x+ i, y ))
break
return ships
def draw_board_with_title(surface, title, x_offset, y_offset):
element_x = 50
element_y = 50 # Ajusta la posición vertical del tablero
font = pygame.font.Font(None, 36)
text = font.render(title, True, BLACK)
text_rect = text.get_rect()
text_rect.centerx = (
element_x
+ (COLUMNAS * TAMAÑO_CUADROS + (COLUMNAS - 1) * ESPACIO) // 2
+ x_offset
)
text_rect.centery = element_y // 2 + y_offset # Centra el título verticalmente
surface.blit(text, text_rect)
for fila in range(FILAS):
for col in range(COLUMNAS):
x = element_x + col * (TAMAÑO_CUADROS + ESPACIO) + x_offset
y = element_y + fila * (TAMAÑO_CUADROS + ESPACIO) + y_offset
pygame.draw.rect(
surface,
BLACK,
(x, y, TAMAÑO_CUADROS, TAMAÑO_CUADROS),
1,
)
# Dibujar cuadrado azul
if (fila, col) in barcos_ubicados:
pygame.draw.rect(surface, BLUE, (x, y, TAMAÑO_CUADROS, TAMAÑO_CUADROS))
# Dibujar una X en la casilla si está en la lista de casillas clickeadas
if (fila, col) in click_enemigo:
pygame.draw.line(
surface, BLACK, (x, y), (x + TAMAÑO_CUADROS, y + TAMAÑO_CUADROS), 2
)
pygame.draw.line(
surface, BLACK, (x + TAMAÑO_CUADROS, y), (x, y + TAMAÑO_CUADROS), 2
)
def draw_board_with_title_enemy(surface, title, x_offset, y_offset):
element_x = 50
element_y = 50 # Ajusta la posición vertical del tablero
font = pygame.font.Font(None, 36)
text = font.render(title, True, BLACK)
text_rect = text.get_rect()
text_rect.centerx = (
element_x
+ (COLUMNAS * TAMAÑO_CUADROS + (COLUMNAS - 1) * ESPACIO) // 2
+ x_offset
)
text_rect.centery = element_y // 2 + y_offset # Centra el título verticalmente
surface.blit(text, text_rect)
for fila in range(FILAS):
for col in range(COLUMNAS):
x = element_x + col * (TAMAÑO_CUADROS + ESPACIO) + x_offset
y = element_y + fila * (TAMAÑO_CUADROS + ESPACIO) + y_offset
pygame.draw.rect(
surface,
BLACK,
(x, y, TAMAÑO_CUADROS, TAMAÑO_CUADROS),
1,
)
# Dibujar cuadrado azul
if (fila, col) in barcos_enemigo:
pygame.draw.rect(surface, BLUE, (x, y, TAMAÑO_CUADROS, TAMAÑO_CUADROS))
# Dibujar una X en la casilla si está en la lista de casillas clickeadas
if (fila, col) in casillas_clickeadas:
pygame.draw.line(
surface, BLACK, (x, y), (x + TAMAÑO_CUADROS, y + TAMAÑO_CUADROS), 2
)
pygame.draw.line(
surface, BLACK, (x + TAMAÑO_CUADROS, y), (x, y + TAMAÑO_CUADROS), 2
)