Beispiel #1
0
    def response_from_server(self, method: int):
        """
        Ask the server to solve a sudoku with a specific algorithm

        Parameters
        ----------
        method : int
            The algorithm to execute to solve the sudoku
        """
        self.flag = False
        self.response = ""
        if self.socket != None:

            message = "SOLVE " + str(method) + " " + self.sudoku_matrix
            try:
                self.socket.send(message.encode("utf-8"))
                response = self.socket.recv(1024).decode("utf-8")
                if response != "":
                    response_split = response.split(" ")
                    if response_split[1] != "-1":
                        server = response_split[0]
                        position = int(response_split[1])
                        number = response_split[2]

                        print("Before built matrix")
                        Matrix.print_matrix(Matrix.build_matrix(self.sudoku_matrix))

                        matriz_list_temp = Matrix.to_list(self.sudoku_matrix)
                        matriz_list_temp[position] = number
                        self.sudoku_matrix = Matrix.to_str(matriz_list_temp)

                        print(response)
                        self.response = response
                        print("After built matrix")
                        Matrix.print_matrix(Matrix.build_matrix(self.sudoku_matrix))
                    else:
                        self.response = response
            except socket.timeout as ex:
                print("Error: Connection timeout. A response cannot be found for method: " + str(method) + " =>", ex)

        else:
            print("Error: A valid connection doesn't created, a response can't be waited")

        self.flag = True
        self.thread = None