Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 41 additions & 34 deletions codebreaker.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
trueNumber = "1010";

class Codebreaker:

def adivinar(self, numero=None):
if trueNumber == '':
return 'Number is not defined'

if numero is None or len(numero) != 4 or 'e' not in list(numero):
return "error"

if numero == trueNumber:
return True

resultadoX = ''
resultado_ = ''
arrayNumber = []

for x in len(numero):
if(arrayNumber[numero[x]] == True):
return 'error'

arrayNumber[numero[x]] = True

numero = list(numero)

for index, x in numero:
if trueNumber[index] == numero[index]:
resultadoX+='X'

elif x in trueNumber:
resultado_='_'

return resultadoX+resultado_
TRUE_NUMBER = "1489"


class CodeBreaker:

def adivinar(self, numero=None):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combinas nombres de variables, funciones y constantes entre español e ingles, te recomiendo mantener constancia con un solo idioma.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tambien te recomiendo darle un nombre mas descriptivo a esta función.

if TRUE_NUMBER == '':
return 'Number is not defined'

if numero is None or len(numero) != 4:
return "Error: number must have 4 digits"

if numero == TRUE_NUMBER:
return True
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Te recomiendo mantener constancia en el tipo de valor que vas a retornar, ya que en este momento estás combinando entre strings y booleans


caracter = ''
array_number = [1]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puedes agregar un comentario a esta variable explicando que hace?


# Evaluar si un número se repite se repite dentro de la secuencia
for x in range((len(numero))):

if numero[x] in array_number:
return f"Error: you have already entered the number {numero[x]} in position {array_number.index(numero[x])}"

array_number.append(numero[x])

numero = list(numero)

for num in range(len(numero)):
# Realiza la búsqueda y compara uno a uno la posición del índice del 'TRUE_NUMBER'
# con el del número ingresado.

if TRUE_NUMBER[num] == numero[num]:
caracter += 'X'

elif numero[num] in TRUE_NUMBER:
caracter += '_'
else:
caracter += ' '

return caracter
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Podrias modificar la logica para que agrupe primero las (X), luego los (_) bajos y por ultimo los espacios?

24 changes: 13 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from codebreaker import Codebreaker
from codebreaker import CodeBreaker

intentos_totales = 10
codebreaker = Codebreaker()
INTENTOS_TOTALES = 10
codebreaker = CodeBreaker()

intento = 0

print('Jugar Codebreaker!')
print('Play Codebreaker!')

while intento != intentos_totales:
number = input('Numero:');
resolve = codebreaker.adivinar(number)
print(resolve)
if resolve == True:
print('You win!!')
break
while intento != INTENTOS_TOTALES:

number = input('Numero:').strip()
resolve = codebreaker.adivinar(number)
print(resolve)

if resolve == True:
print('You win!!')
break

intento += 1