示例#1
0
    def __init__(self,
                 sudoku_matrix=[[
                     BoardSquare.default_num
                     for x in range(0, DEFAULT_SIDE_LEN)
                 ] for y in range(0, DEFAULT_SIDE_LEN)]):
        side_len = len(sudoku_matrix)
        # Matrix of BoardSquare objects, which will be populated by
        # the input sudoku_matrix.
        self.board = [[BoardSquare(y, x) for x in range(0, side_len)]
                      for y in range(0, side_len)]

        # Maintain a hashmap from the index of a 3x3 section (counting
        # top-to-bottom and left-to-right) to the squares contained in
        # that section. This will be used later to help check board validity
        self.__section_map = {}
        for x in range(0, side_len):
            self.__section_map[x] = []

        # Initialize the sudoku board from input
        for row in range(side_len):
            for col in range(side_len):
                square = self.board[row][col]
                square.num = sudoku_matrix[row][col] if sudoku_matrix[row][
                    col] else BoardSquare.default_num
                # Figure out which section this square is in, and add
                # this square to the list of squares in that section
                self.__section_map[self.__section_for_square(square)].append(
                    square)
示例#2
0
    def load_board(self, board_file_name):
        """
        This function takes a file name and loads the map, creating BoardSquare objects in a grid.

        :param board_file_name: the board file name
        :return: sets the self.board object within the class
        """

        import json
        try:
            with open(board_file_name) as board_file:
                board_json = json.loads(board_file.read())
                self.num_pieces = self.STARTING_PIECES
                self.board = []
                for x, row in enumerate(board_json):
                    self.board.append([])
                    for y, square in enumerate(row):
                        self.board[x].append(
                            BoardSquare(x,
                                        y,
                                        entrance=square['entrance'],
                                        _exit=square['exit'],
                                        rosette=square['rosette'],
                                        forbidden=square['forbidden']))

                for i in range(len(self.board)):
                    for j in range(len(self.board[i])):
                        if board_json[i][j]['next_white']:
                            x, y = board_json[i][j]['next_white']
                            self.board[i][j].next_white = self.board[x][y]
                        if board_json[i][j]['next_black']:
                            x, y = board_json[i][j]['next_black']
                            self.board[i][j].next_black = self.board[x][y]
        except OSError:
            print('The file was unable to be opened. ')
示例#3
0
 def init_board(self):
     """Initialize the checkered board pattern, add the pieces to board_dict"""
     for i in range(8):
         if i % 2 == 0:
             self.board_dict[i] = {}
             for j in range(8):
                 if j % 2 == 0:
                     self.board_dict[i][j] = BoardSquare(i, j, 'black')
                 else:
                     self.board_dict[i][j] = BoardSquare(i, j, 'white')
         else:
             self.board_dict[i] = {}
             for j in range(8):
                 if j % 2 == 1:
                     self.board_dict[i][j] = BoardSquare(i, j, 'black')
                 else:
                     self.board_dict[i][j] = BoardSquare(i, j, 'white')
    def save_map(self):
        file_name = asksaveasfilename(title='Select File to save as map',
                                      filetypes=(('Royal Ur Files', '*.ur'), ))
        if not file_name:
            return
        with open(file_name, 'w') as json_file:
            dict_grid = []
            for i in range(len(self.the_grid)):
                dict_grid.append([])
                for j in range(len(self.the_grid[i])):
                    bs = BoardSquare(i, j, self.the_grid[i][j].entrance.get(),
                                     self.the_grid[i][j].exit.get(),
                                     self.the_grid[i][j].rosette.get(),
                                     self.the_grid[i][j].forbidden.get())

                    bs.next_white = self.the_grid[i][j].next_white
                    bs.next_black = self.the_grid[i][j].next_black

                    dict_grid[i].append(bs.jsonify())

            json_file.write(json.dumps(dict_grid))
示例#5
0
    from board_square import BoardSquare
    import tkinter as tk

    # This is needed to use the image attributes used to create piece objects
    test_window = tk.Tk()
    test_game = Game()

    board_dict = {}

    # Create a board_dict for testing purposes
    for i in range(8):
        if i % 2 == 0:
            board_dict[i] = {}
            for j in range(8):
                if j % 2 == 0:
                    board_dict[i][j] = BoardSquare(i, j, 'black')
                else:
                    board_dict[i][j] = BoardSquare(i, j, 'white')
        else:
            board_dict[i] = {}
            for j in range(8):
                if j % 2 == 1:
                    board_dict[i][j] = BoardSquare(i, j, 'black')
                else:
                    board_dict[i][j] = BoardSquare(i, j, 'white')

    # Test is_in_board method
    assert test_game.is_in_board(0, 0) == True
    assert test_game.is_in_board(3, 3) == True
    assert test_game.is_in_board(8, 8) == False