Game of guessing a number in Python

1

I'm trying to make a game where you try to guess a three-digit number.

The maximum of attempts is 5 and on the board the attempts that have been made along with their state must be shown. For example, if the code to guess (produced randomly) is [4,8,9] and the intent is [4,9,6] you must indicate which digits belong, that is: ["S","P","N"] .

  • S : Correct position.
  • P Incorrect position.
  • N Not found.

The code I have (based on one of functional structure), has some classes that I'm not sure where to place, and it generates errors like the following:

  

takes exactly 2 arguments (1 given)

Code:

import random
import sys


class CodeMaker:

    def __init__(self):
        self.codigo=[random.randint(0, 9) for i in range(3)]

class CodeBreaker:
    def __init__(self):

        le_correcta_intento = False
        while not le_correcta_intento:
            self.intento= input("Adivina la cifra de cuatro digitos: ")
            self.intento=[int(i) for i in str(self.intento)]
            if len(self.intento) != 3:
                print("Debe ser de cuatro digitos, intente de nuevo.")

            else:
                le_correcta_intento = True


class Tablero:

    def __init__(self,intentos):
        self.intentos=intentos
        self.tablero = Tablero.tablero(self)

    def tablero(self):
        self.intentos=[]
        u=CodeBreaker()
        print "Score"
        print('{0}'.format('*'*55))
        print "Jugada numero:", turnos, "/10"

        self.intentos.append(u.intentos)
        print self.intentos
        for i, x in zip(intentos, estado_juego):
            print("{0}\t{1}\t{2}\t{3}\t{4}".format(i[0],i[1],i[2],i[3], x))
        turnos=turnos+1


class f:

    def __init__(self):
        self.chequear = f.chequear(self)

    def chequear(self):
        return all([x == y for x, y in zip(CodeBreaker.intento, CodeMaker.codigo)])
        evaluar()

    def evaluar():

        estado_juego=[]
        nuevo_estado = []
        for posicion, num in enumerate(intento):
            if codigo[posicion] == num:
                nuevo_estado.append("S")
            elif numero in codigo_secreto:
                nuevo_estado.append("P")
            else:
                nuevo_estado.append("N")
        estado_juego.append(''.join([i for i in nuevo_estado]))
        return estado_juego

class juego(CodeMaker,CodeBreaker,Tablero):
    def __init__(self):

        self.ju=juego.jugar(self)

    def Jugar_de_nuevo(self):
            opciones=raw_input('¿Desea jugar de nuevo? (si o no)')
            if opciones == 'si':
                juego.__init__(self)
            else:
                sys.exit(0)

    def jugar(self):
        global turnos
        turnos=0
        intentos=[]
        p=CodeMaker()
        while not turnos == 5:
            y=Tablero()
            turnos=turnos+1
                if y.intento != p.codigo :
                print "Perdiste"
            else:
                print "Ganaste, el codigo era ", p.codigo
                juego.Jugar_de_nuevo(self)

            print y.intento
            print p.codigo
            print turnos
        if turnos == 5:
            print("No adivinaste el código, éste era: {0}! ".format(''.join([str(i) for i in p.codigo])))
            juego.Jugar_de_nuevo(self)

if __name__ == '__main__':

    codigo=[]
    intento=[]
    game=juego()
    game.jugar()
    
asked by Adam 06.05.2017 в 22:03
source

1 answer

0

The point error is in the method jugar of the class juego in the line

        y=Tablero()

But the initialization of the Tablero object is done:

 def __init__(self, intentos):
    self.intentos=intentos
    self.tablero = Tablero.tablero(self)

So you are waiting for a second argument intentos ( self is automatically passed) that you are not passing it.

    
answered by 09.05.2017 в 16:07