Beispiel #1
0
class Interfaz:
    def __init__(self, tamaño=None):
        os.system("clear")
        print("\nBienvenido a Sudoku")

        if tamaño is None:
            self.tamaño = 0
            while (True):
                self.tamaño = int(
                    input("\nIngrese el tamaño deseado (4 o 9): "))
                if (self.tamaño != 4 and self.tamaño != 9):
                    os.system("clear")
                    print("\nError al ingresar tamaño, intente nuevamente")
                else:
                    return None
        elif (tamaño == 4 or tamaño == 9):
            self.tamaño = tamaño
        else:
            print("\nParametro de tamaño invalido")
            print("\nSaliendo...")

    def inicio(self):
        self.generar_tablero()
        self.generar_logica()
        self.jugar()

    def generar_tablero(self):
        self.api = API()  #creo objeto api
        self.tablero = self.api.crear_api(
            self.tamaño)  #genero tablero y asigno
        return self.tablero

    def generar_logica(self):
        self.logica = Sudoku(self.tablero, self.tamaño)  #genero objeto logico

    def obtener_valores(self):
        self.row = int(
            input("\nIngrese la fila que desea modificar (1-{}): ".format(
                self.tamaño)))
        self.column = int(
            input("\nIngrese la columna que desea modificar (1-{}): ".format(
                self.tamaño)))
        self.value = int(
            input("\nIngrese el valor a insertar en la posicion ({},{}): ".
                  format(self.row, self.column)))
        self.coordinates = self.row - 1, self.column - 1
        return self.coordinates, self.value

    def jugar(self):
        while (True):
            print(
                """\n MENU\n\n1-Imprimir Sudoku\n2-Modificar valor\n3-Salir""")
            decision = input("Que desea hacer ? ")
            os.system("clear")
            if (decision == '1'):
                print(self.logica.imprimir_tablero())
            elif (decision == '2'):
                os.system("clear")
                print(self.logica.imprimir_tablero())
                self.obtener_valores()
                #verifico que no se transgredan las reglas
                if (self.logica.modificar_tablero(self.coordinates,
                                                  self.value)[0]):
                    os.system("clear")
                    print("\nModificando...\n\n")
                    print(self.logica.imprimir_tablero())
                    #verifico victoria
                    if (self.logica.verificar_victoria()):
                        print("\nFELICITACIONES HAS GANADO !!!")
                        return False
                #si se transgreden las reglas
                else:
                    os.system("clear")
                    print("\n***ERROR*** -> ", end="")
                    print(
                        self.logica.modificar_tablero(self.coordinates,
                                                      self.value)[1])
                    print("\nVuelva a intentar...")
            #menu de salida
            elif (decision == '3'):
                (os.system("clear"))
                decision2 = input("\nSe perdera el progreso. Confirme (Y) ")
                if (decision2 == 'y' or decision2 == 'Y'):
                    print("\nSaliendo...")
                    return False
            else:
                print("\n*Error* - Opcion inexistente, vuelva a intentar")
Beispiel #2
0
class Test_API(unittest.TestCase):
    def setUp(self):
        self.tablaApi = API()

    def test_API_1(self):  #9x9
        mock_response = MagicMock()
        mock_response.json = MagicMock(
            return_value={
                "response":
                True,
                "size":
                "9",
                "squares": [{
                    "x": 0,
                    "y": 1,
                    "value": 6
                }, {
                    "x": 0,
                    "y": 7,
                    "value": 1
                }, {
                    "x": 0,
                    "y": 8,
                    "value": 2
                }, {
                    "x": 1,
                    "y": 1,
                    "value": 2
                }, {
                    "x": 1,
                    "y": 4,
                    "value": 3
                }, {
                    "x": 1,
                    "y": 5,
                    "value": 9
                }, {
                    "x": 1,
                    "y": 7,
                    "value": 5
                }, {
                    "x": 1,
                    "y": 8,
                    "value": 6
                }, {
                    "x": 2,
                    "y": 1,
                    "value": 7
                }, {
                    "x": 2,
                    "y": 2,
                    "value": 4
                }, {
                    "x": 2,
                    "y": 5,
                    "value": 6
                }, {
                    "x": 2,
                    "y": 6,
                    "value": 3
                }, {
                    "x": 3,
                    "y": 0,
                    "value": 4
                }, {
                    "x": 3,
                    "y": 3,
                    "value": 6
                }, {
                    "x": 3,
                    "y": 4,
                    "value": 2
                }, {
                    "x": 3,
                    "y": 5,
                    "value": 8
                }, {
                    "x": 3,
                    "y": 8,
                    "value": 9
                }, {
                    "x": 4,
                    "y": 0,
                    "value": 7
                }, {
                    "x": 4,
                    "y": 2,
                    "value": 6
                }, {
                    "x": 4,
                    "y": 4,
                    "value": 5
                }, {
                    "x": 4,
                    "y": 6,
                    "value": 2
                }, {
                    "x": 4,
                    "y": 8,
                    "value": 3
                }, {
                    "x": 5,
                    "y": 0,
                    "value": 9
                }, {
                    "x": 5,
                    "y": 3,
                    "value": 4
                }, {
                    "x": 5,
                    "y": 8,
                    "value": 5
                }, {
                    "x": 6,
                    "y": 0,
                    "value": 2
                }, {
                    "x": 6,
                    "y": 2,
                    "value": 5
                }, {
                    "x": 6,
                    "y": 3,
                    "value": 1
                }, {
                    "x": 6,
                    "y": 6,
                    "value": 9
                }, {
                    "x": 6,
                    "y": 7,
                    "value": 8
                }, {
                    "x": 7,
                    "y": 1,
                    "value": 9
                }, {
                    "x": 7,
                    "y": 3,
                    "value": 3
                }, {
                    "x": 7,
                    "y": 4,
                    "value": 8
                }, {
                    "x": 7,
                    "y": 6,
                    "value": 5
                }, {
                    "x": 7,
                    "y": 7,
                    "value": 2
                }, {
                    "x": 8,
                    "y": 1,
                    "value": 4
                }, {
                    "x": 8,
                    "y": 2,
                    "value": 1
                }, {
                    "x": 8,
                    "y": 4,
                    "value": 9
                }, {
                    "x": 8,
                    "y": 6,
                    "value": 6
                }, {
                    "x": 8,
                    "y": 7,
                    "value": 3
                }]
            })
        with patch("api.requests.get", return_value=mock_response):
            result = self.tablaApi.crear_api(9)

        self.assertEqual(result, [["x", "x", "x", 4, 7, 9, 2, "x", "x"],
                                  [6, 2, 7, "x", "x", "x", "x", 9, 4],
                                  ["x", "x", 4, "x", 6, "x", 5, "x", 1],
                                  ["x", "x", "x", 6, "x", 4, 1, 3, "x"],
                                  ["x", 3, "x", 2, 5, "x", "x", 8, 9],
                                  ["x", 9, 6, 8, "x", "x", "x", "x", "x"],
                                  ["x", "x", 3, "x", 2, "x", 9, 5, 6],
                                  [1, 5, "x", "x", "x", "x", 8, 2, 3],
                                  [2, 6, "x", 9, 3, 5, "x", "x", "x"]])

    def test_API_2(self):  #4x4
        mock_response = MagicMock()
        mock_response.json = MagicMock(
            return_value={
                "response":
                True,
                "size":
                "4",
                "squares": [{
                    "x": 0,
                    "y": 2,
                    "value": 4
                }, {
                    "x": 1,
                    "y": 1,
                    "value": 4
                }, {
                    "x": 1,
                    "y": 2,
                    "value": 2
                }, {
                    "x": 2,
                    "y": 0,
                    "value": 1
                }, {
                    "x": 2,
                    "y": 1,
                    "value": 2
                }, {
                    "x": 2,
                    "y": 2,
                    "value": 3
                }, {
                    "x": 2,
                    "y": 3,
                    "value": 4
                }, {
                    "x": 3,
                    "y": 2,
                    "value": 1
                }]
            })
        with patch("api.requests.get", return_value=mock_response):
            result = self.tablaApi.crear_api(4)

        self.assertEqual(result, [["x", "x", 1, "x"], ["x", 4, 2, "x"],
                                  [4, 2, 3, 1], ["x", "x", 4, "x"]])