-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
277 lines (204 loc) · 7.02 KB
/
Copy pathmain.py
File metadata and controls
277 lines (204 loc) · 7.02 KB
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
#EJERCICIO GENERALA
import random
def tirar_dados(cantidad_dados):
dados= []
for i in range(cantidad_dados):
dado=random.randint(1,6)
dados.append(dado)
return dados
def jugada(dados):
contadores = [0,0,0,0,0,0]
# contar cuantas veces aparece cada dado
for dado in dados:
contadores[dado-1] += 1
# GENERALA
for c in contadores:
if c == 5:
return "Generala"
# POKER
for c in contadores:
if c == 4:
return "Poker"
# FULL
hay_tres = False
hay_dos = False
for c in contadores:
if c == 3:
hay_tres = True
if c == 2:
hay_dos = True
if hay_tres and hay_dos:
return "Full"
# ESCALERA
dados_ordenados = sorted(dados)
if dados_ordenados == [1,2,3,4,5] or dados_ordenados == [2,3,4,5,6]:
return "Escalera"
return "Nada"
def puntaje_numero(dados, numero):
suma = 0
for dado in dados:
if dado == numero:
suma += dado
return suma
def reroll(dados, indices):
nuevos = dados[:] # copia
for i in indices:
nuevos[i] = random.randint(1, 6)
return nuevos
def pedir_indices_a_retirar():
while True:
s = input("Qué dados querés volver a tirar? (1-5 separados por espacio, Enter=ninguno): ").strip()
if s == "":
return []
partes = s.split()
# verificar que todos sean numeros
for p in partes:
if not p.isdigit():
print("Error: solo se permiten números.")
break
else:
numeros = []
for p in partes:
k = int(p)
if not (1 <= k <= 5):
print("Error: los números deben estar entre 1 y 5.")
break
if k in numeros:
print("Error: no puede haber números repetidos.")
break
numeros.append(k)
else:
# convertir a índices de python
indices = []
for k in numeros:
indices.append(k - 1)
return indices
def turno():
dados = tirar_dados(5)
print("Tirada 1:", dados)
resultado_primera_tirada = jugada(dados)
if resultado_primera_tirada == "Generala":
return dados, 1, True, resultado_primera_tirada
nro_tirada_final = 1
for nro_tirada in range(2, 4):
indices = pedir_indices_a_retirar()
if len(indices) == 0:
break
dados = reroll(dados, indices)
print(f"Tirada {nro_tirada}:", dados)
nro_tirada_final = nro_tirada
return dados, nro_tirada_final, False, resultado_primera_tirada
CATEGORIAS = ["E","F","P","G","1","2","3","4","5","6"]
def crear_planilla():
planilla = {}
for c in CATEGORIAS:
planilla[c] = None
return planilla
def categorias_disponibles(planilla):
disp = []
for c in planilla:
if planilla[c] is None:
disp.append(c)
return disp
def pedir_categoria(planilla):
disp = categorias_disponibles(planilla)
print("Categorías disponibles:", disp)
while True:
c = input("Elegí categoría: ").upper()
if c in disp:
return c
print("Categoría inválida o ya usada.")
def calcular_puntaje(dados, categoria, nro_tirada_final, resultado_primera_tirada):
if categoria in ["1","2","3","4","5","6"]:
return puntaje_numero(dados, int(categoria))
resultado = jugada(dados)
if categoria == "E" and resultado == "Escalera":
if nro_tirada_final == 1 and resultado_primera_tirada == "Escalera":
return 25
return 20
if categoria == "F" and resultado == "Full":
if nro_tirada_final == 1 and resultado_primera_tirada == "Full":
return 35
return 30
if categoria == "P" and resultado == "Poker":
if nro_tirada_final == 1 and resultado_primera_tirada == "Poker":
return 45
return 40
if categoria == "G" and resultado == "Generala":
return 50
return 0
def anotar_puntaje(planilla, categoria, puntos):
planilla[categoria] = puntos
def turno_jugador(planilla, nombre):
print("\nTurno de", nombre)
dados, nro_tirada_final, generala_real, resultado_primera_tirada = turno()
print("Dados finales:", dados)
print("Terminó en la tirada:", nro_tirada_final)
resultado = jugada(dados)
print("Jugada obtenida:", resultado)
if generala_real:
anotar_puntaje(planilla, "G", 80)
print("GENERALA REAL")
print(nombre, "gana automáticamente")
print("Planilla actual:", planilla)
return True
categoria = pedir_categoria(planilla)
puntos = calcular_puntaje(dados, categoria, nro_tirada_final, resultado_primera_tirada)
anotar_puntaje(planilla, categoria, puntos)
print("Se anotaron", puntos, "puntos en la categoría", categoria)
print("Planilla actual:", planilla)
return False
def planilla_completa(planilla): #RECORRE LA PLANILLA Y SE FIJA SI ESTA COMPLETA O NO
for categoria in planilla:
if planilla[categoria] is None:
return False
return True
def total_puntos(planilla):
total = 0
for categoria in planilla:
total += planilla[categoria]
return total
def guardar_csv(planilla_j1, planilla_j2):
with open("jugadas.csv", "w") as archivo:
archivo.write("jugada,j1,j2\n")
for c in CATEGORIAS:
p1 = planilla_j1[c]
p2 = planilla_j2[c]
if p1 is None:
p1 = 0
if p2 is None:
p2 = 0
linea = c + "," + str(p1) + "," + str(p2) + "\n"
archivo.write(linea)
def main():
planilla_j1 = crear_planilla()
planilla_j2 = crear_planilla()
guardar_csv(planilla_j1, planilla_j2)
while not (planilla_completa(planilla_j1) and planilla_completa(planilla_j2)):
if not planilla_completa(planilla_j1):
generala_real = turno_jugador(planilla_j1, "Jugador 1")
guardar_csv(planilla_j1, planilla_j2)
if generala_real:
print("\n--- FIN DEL JUEGO ---")
print("Ganó Jugador 1 por GENERALA REAL")
return
if not planilla_completa(planilla_j2):
generala_real = turno_jugador(planilla_j2, "Jugador 2")
guardar_csv(planilla_j1, planilla_j2)
if generala_real:
print("\n--- FIN DEL JUEGO ---")
print("Ganó Jugador 2 por GENERALA REAL")
return
total_j1 = total_puntos(planilla_j1)
total_j2 = total_puntos(planilla_j2)
print("\n--- FIN DEL JUEGO ---")
print("Puntaje total Jugador 1:", total_j1)
print("Puntaje total Jugador 2:", total_j2)
if total_j1 > total_j2:
print("Ganó Jugador 1")
elif total_j2 > total_j1:
print("Ganó Jugador 2")
else:
print("Empate")
if __name__ == "__main__":
main()