class ConsolePresentation:
    def __init__(self, puzzle):
        self.sudoku_game = puzzle
        self.new_tool = Tools()
        self.matrix = self.new_tool.convert_str_to_matrix(self.sudoku_game, 9, 9)
        self.new_menus = Menus()
        self.new_configuration = Configuration()
        self.new_sudoku_play = SudokuPlay()
        self.path_of_file = os.getcwd() + "\\configurations\\"
        self.matrix_play = self.matrix
        self.row = 0
        self.column = 0
        self.value = 0
        self.algorithm = ""
        self.difficulty = ""
        self.solved_out_put = ""
        

    def read_xml(self, file_name):
        """
        It receives an xml file and return root, it has the xml file data
        """
        try:
            self.tree = xmltree.parse(self.path_of_file + file_name)
        except:
            self.tree = xmltree.parse(self.path_of_file+self.name_of_file)

        self.root = self.tree.getroot()

        return self.root

    def get_difficulty_levels(self, root):
        """
        This method we did because we start writing code instead write a unit TC before; so this is an example of what we should not do it.
        """
        list_tmp = ""
        matrix = []
        for levels in root.iter('level'):
            xml_read = levels.attrib
            id_xml = xml_read['id']
            name_xml = xml_read['name']
            min_xml = xml_read['min']
            max_xml = xml_read['max']
            list_tmp = id_xml, name_xml, min_xml, max_xml
            matrix.append(list_tmp)
        return matrix

    def start_end_menus(self, menu):
        os.system('cls')
        self.new_tool.display_board(self.matrix)

        if menu == "main":
            self.new_menus.print_main_menu()
        elif menu == "game":
            self.new_menus.print_sub_menu_game()
        elif menu == "play":
            self.new_menus.print_sub_menu_play()
        elif menu == "read":
            self.new_menus.print_sub_menu_read()
        elif menu == "save":
            self.new_menus.print_sub_menu_save()
        elif menu == "options":
            self.new_menus.print_sub_menu_options()
        elif menu == "algorithm":
            self.new_menus.print_sub_menu_algorithm()
        elif menu == "difficulty":
            self.new_menus.print_list_difficulty()
            self.new_menus.print_sub_menu_difficulty()
        elif menu == "solved_out_put":
            self.new_menus.print_sub_menu_solved_out_put()
        elif menu == "edit":
            self.new_menus.print_sub_menu_edit()

        if menu == 'play':
            return raw_input("make your play: ")
        else:
            return raw_input("choose: ")

    def count_zeros(self, matrix):
        rows = len(matrix)
        columns = len(matrix[0])
        cont_zeros = 0
        for row in range(rows):
            for col in range(columns):
                if matrix[row][col] == 0:
                    cont_zeros += 1
        return cont_zeros

    def verify_game_completed(self, game_matrix):
        if self.count_zeros(game_matrix) == 0:
            return True
        else:
            return False


    def print_main_menu(self, puzzle):
        read_key = self.start_end_menus("main")

        while read_key != "X":
            if read_key == "p":
                self.new_sudoku_play.play_the_game(puzzle)
            elif read_key == "o":
                self.print_sub_menu_options()
            elif read_key == "x":
                print ""
                print "!!! Game Over !!!, ",
                print "you press key %s..." %(read_key)
                break
            else:
                read_key = raw_input("choose another option.")

            read_key = self.start_end_menus("main")

    def print_sub_menu_game(self):
        read_key = self.start_end_menus("game")

        while read_key != "B":
            if read_key == "p":
                self.play_the_game()
            elif read_key == "g":
                print "you press key %s" %(read_key)
            elif read_key == "l":
                print "You press key %s" % read_key
            elif read_key == "r":
                self.print_sub_menu_read()
            elif read_key == "s":
                self.print_sub_menu_save()
            elif read_key == "t":
                print "you press key %s" %(read_key)
            elif read_key == "b":
                break
            else:
                read_key = raw_input("choose another option.")

            read_key = self.start_end_menus("game")

    def print_sub_menu_read(self):
        read_key = self.start_end_menus("read")

        while read_key != "B":
            if read_key == "t":
                print "you press key %s" %(read_key)
            elif read_key == "v":
                print "you press key %s" %(read_key)
            elif read_key == "c":
                print "you press key %s" %(read_key)
            elif read_key == "b":
                break
            else:
                read_key = raw_input("choose another option.")

            read_key = self.start_end_menus("read")

    def print_sub_menu_save(self):
        read_key = self.start_end_menus("save")

        while read_key != "B":
            if read_key == "t":
                print "You press key %s" % read_key
            elif read_key == "c":
                print "you press key %s" %(read_key)
            elif read_key == "h":
                print "You press key %s" % read_key
            elif read_key == "b":
                break
            else:
                read_key = raw_input("choose another option.")

            read_key = self.start_end_menus("save")

    def print_sub_menu_options(self):
        read_key = self.start_end_menus("options")

        while read_key != "B":
            if read_key == "a":
                self.print_sub_menu_algorithm()
            elif read_key == "d":
                self.print_sub_menu_difficulty()
            elif read_key == "v":
                self.print_sub_menu_solved_out_put()
            elif read_key == "b":
                break
            else:
                read_key = raw_input("choose another option.")

            read_key = self.start_end_menus("options")

    def print_sub_menu_algorithm(self):
        read_key = self.start_end_menus("algorithm")

        while read_key != "B":
            if read_key == "k":
                self.algorithm = "backtracking"
                self.new_configuration.modify_value_in_xml(self.algorithm,"algorithm")
                raw_input ("you chose " +self.algorithm + " Algorithm")
                break
            elif read_key == "n":
                self.algorithm = "peternorvig"
                self.new_configuration.modify_value_in_xml(self.algorithm,"algorithm")
                raw_input ("you chose " + "Peter Norvig" + " Algorithm")
                break
            elif read_key == "i":
                self.algorithm = "recursive"
                self.new_configuration.modify_value_in_xml(self.algorithm,"algorithm")
                raw_input ("you chose " +self.algorithm + " Algorithm")
                break
            elif read_key == "b":
                break
            else:
                read_key = raw_input("choose another option.")

            read_key = self.start_end_menus("algorithm")

    def print_sub_menu_difficulty(self):
        read_key = self.start_end_menus("difficulty")
        difficulty_matrix = self.new_menus.get_difficulty_matrix()

        while read_key != "B":
            if read_key == "e":
                self.print_sub_menu_edit()
            try:
                if read_key == "e":
                    self.print_sub_menu_edit()
                elif read_key == "0":
                    self.difficulty = difficulty_matrix[0][1]
                    self.new_configuration.modify_value_in_xml(self.difficulty,"difficulty")
                    raw_input ("You chose " + self.difficulty + " as difficulty level") 
                    break
                elif read_key == "1":
                    self.difficulty = difficulty_matrix[1][1]
                    self.new_configuration.modify_value_in_xml(self.difficulty,"difficulty")
                    raw_input ("You chose "+ self.difficulty + " as difficulty level") 
                    break
                elif read_key == "2":
                    self.difficulty = difficulty_matrix[2][1]
                    self.new_configuration.modify_value_in_xml(self.difficulty,"difficulty")
                    raw_input ("You chose "+ self.difficulty + " as difficulty level") 
                    break
                elif read_key == "3":
                    self.difficulty = difficulty_matrix[3][1]
                    self.new_configuration.modify_value_in_xml(self.difficulty,"difficulty")
                    raw_input ("You chose "+ self.difficulty + " as difficulty level") 
                    break
                elif read_key == "4":
                    self.difficulty = difficulty_matrix[4][1]
                    self.new_configuration.modify_value_in_xml(self.difficulty,"difficulty")
                    raw_input ("You chose "+ self.difficulty + " as difficulty level") 
                    break
                elif read_key == "5":
                    self.difficulty = difficulty_matrix[5][1]
                    self.new_configuration.modify_value_in_xml(self.difficulty,"difficulty")
                    raw_input ("You chose "+ self.difficulty + " as difficulty level") 
                    break
                elif read_key == "b":
                    break
                else:
                    read_key = raw_input("choose another option.")
            except:
                raw_input("Choose another option")

            read_key = self.start_end_menus("difficulty")
            
    def print_sub_menu_solved_out_put(self):
        read_key = self.start_end_menus("solved_out_put")
        difficulty_matrix = self.new_menus.get_difficulty_matrix()

        while read_key != "B":
            if read_key == "e":
                self.print_sub_menu_edit()
            try:
                if read_key == "t":
                    self.solved_out_put = "txt"
                    self.new_configuration.modify_value_in_xml(self.solved_out_put,"solver_output_type")
                    raw_input ("You chose " + self.solved_out_put + " as solver output type") 
                    break
                elif read_key == "c":
                    self.solved_out_put = "csv"
                    self.new_configuration.modify_value_in_xml(self.solved_out_put,"solver_output_type")
                    raw_input ("You chose "+ self.solved_out_put + " as solver output type") 
                    break
                elif read_key == "h":
                    self.solved_out_put = "html"
                    self.new_configuration.modify_value_in_xml(self.solved_out_put,"solver_output_type")
                    raw_input ("You chose "+ self.solved_out_put + " as solver output type ") 
                    break
                elif read_key == "b":
                    break
                else:
                    read_key = raw_input("choose another option.")
            except:
                raw_input("Choose another option")

            read_key = self.start_end_menus("difficulty")

    def print_sub_menu_edit(self):
        read_key = self.start_end_menus("edit")

        while read_key != "B":
            if read_key == "n":
                print "You press key %s" % read_key
            elif read_key == "c":
                print "you press key %s" %(read_key)
            elif read_key == "b":
                break
            else:
                read_key = raw_input("choose another option.")

            read_key = self.start_end_menus("edit")

    def run_app(self, puzzle):
        self.print_main_menu(puzzle)
Beispiel #2
0
class SudokuPlay:
    def __init__(self):
        self.new_tool = Tools()
        self.matrix_play = []
        self.row = 0
        self.column = 0
        self.value = 0
        self.new_menu = Menus()
        self.return_game = ""
        self.boards = integrator.integrator()

    def print_sub_menu_play(self):
        self.new_menu.print_sub_menu_play()

    def start_end_menus(self, matrix_play, menu):
        os.system("cls")
        self.new_tool.display_board(matrix_play)
        if menu == "play":
            self.print_sub_menu_play()
            return raw_input("Make your play:")
        elif menu == "read":
            self.new_menu.print_sub_menu_read()
            return raw_input("Choose your option:")
        elif menu == "save":
            self.new_menu.print_sub_menu_save()
            return raw_input("Choose your option:")

    def verify_read_key_str(self, read_key, pos):
        ABC = self.new_tool.get_ABC(9, 9)
        list_tmp = list(read_key)
        try:
            if ABC[0].index(list_tmp[pos]) >= 0:
                return True
            else:
                return False
        except:
            return False

    def get_parse_str(self, read_key, pos):
        list_tmp = list(read_key)
        return list_tmp[pos]

    def verify_read_key_int(self, read_key, pos):
        list_tmp = list(read_key)

        try:
            if len(list_tmp) >= 4:
                return False
            else:
                if int(list_tmp[2]) in range(0, 10):
                    self.value = int(list_tmp[2])
                    return True
                else:
                    return False
        except:
            return False

    def set_possiton(self, read_key):
        list_tmp = list(read_key)
        self.row = ord(list_tmp[0]) - 65
        self.column = ord(list_tmp[1]) - 65

    def verify_busy(self, matrix, rows, columns):

        if matrix[rows][columns] == 0:
            return False
        else:
            return True

    def change_value_in_matrix(self, matrix, row, column, value):
        matrix[row][column] = str(value)
        return matrix

    def play_the_game(self, puzzle):
        self.boards.load_sudoku()
        puzzle = self.boards.get_original_sudoku()
        self.matrix_play = self.new_tool.convert_str_to_matrix(puzzle, 9, 9)
        read_key = self.start_end_menus(self.matrix_play, "play")
        while read_key != "Q":
            parse_row = self.verify_read_key_str(read_key, 0)
            parse_column = self.verify_read_key_str(read_key, 1)
            parse_value = self.verify_read_key_int(read_key, 2)

            if parse_row and parse_column and parse_value:
                self.set_possiton(read_key)

                if not self.verify_busy(self.matrix_play, self.row, self.column):
                    self.matrix_play = self.change_value_in_matrix(self.matrix_play, self.row, self.column, self.value)
                    self.return_game = self.new_tool.convert_matrix_to_str(self.matrix_play)
                else:
                    raw_input("That point is busy, make another play")
            elif read_key == "g":
                self.boards.load_sudoku()
                puzzle = self.boards.get_original_sudoku()
                self.matrix_play = self.new_tool.convert_str_to_matrix(puzzle, 9, 9)
            elif read_key == "r":
                self.print_sub_menu_read()
            elif read_key == "h":
                currently_game = self.new_tool.convert_matrix_to_str(self.matrix_play)
                currently_game = self.boards.get_hint(currently_game)
                self.matrix_play = self.new_tool.convert_str_to_matrix(currently_game, 9, 9)
            elif read_key == "v":
                puzzle = self.boards.get_solved_puzzle()
                print puzzle
                self.matrix_play = self.new_tool.convert_str_to_matrix(puzzle, 9, 9)
            elif read_key == "t":
                puzzle = self.boards.get_original_sudoku()
                self.matrix_play = self.new_tool.convert_str_to_matrix(puzzle, 9, 9)
            elif read_key == "s":
                self.print_sub_menu_save()
            elif read_key == "q":
                if raw_input("do you really want to quit(y/n)? ") == "y":
                    break
            else:
                raw_input("Make another play. Review the upper case and value")

            read_key = self.start_end_menus(self.matrix_play, "play")

    def print_sub_menu_read(self):
        read_key = self.start_end_menus(self.matrix_play, "read")

        while read_key != "B":
            if read_key == "t":
                path_file = self.get_path_from_console("txt")
                puzzle = self.boards.read_txt_file(path_file)
                self.matrix_play = self.new_tool.convert_str_to_matrix(puzzle, 9, 9)
            elif read_key == "v":
                path_file = self.get_path_from_console("cvs")
                puzzle = self.boards.read_cvs_file(path_file)
                self.matrix_play = self.new_tool.convert_str_to_matrix(puzzle, 9, 9)
            elif read_key == "c":
                # print "you press key %s" %(read_key)
                self.load_from_console()
            elif read_key == "b":
                break
            else:
                read_key = raw_input("choose another option.")

            read_key = self.start_end_menus(self.matrix_play, "read")

    def print_sub_menu_save(self):
        read_key = self.start_end_menus(self.matrix_play, "save")
        currently_game = self.boards.get_original_sudoku()
        while read_key != "B":
            if read_key == "t":
                self.boards.save_txt(currently_game)

            elif read_key == "c":
                self.boards.save_cvs(currently_game)
            elif read_key == "h":
                self.boards.save_html(currently_game)
            elif read_key == "d":
                self.boards.save_default()
            elif read_key == "b":
                break
            else:
                read_key = raw_input("choose another option.")

            read_key = self.start_end_menus(self.matrix_play, "save")

    def verify_numbers(self, numbers_string):
        pass

    def load_from_console(self):
        puzzle = raw_input("Introduce the game in a single string:")
        if len(puzzle) == 81:
            self.matrix_play = self.new_tool.convert_str_to_matrix(puzzle, 9, 9)
        else:
            raw_input("wrong input, try it again")

    def get_path_from_console(self, type_input):
        path_file = raw_input("Introduce the path of the game(" + type_input + "):")
        return path_file