예제 #1
0
    def setUp(self):
        self.test_dead_board = Board(n=5)

        self.where_seed111_is_active = ((1, 0), (1, 1), (2, 2), (3, 2), (3, 3))
        self.test_active_board = Board(n=5)
        for entry in self.where_seed111_is_active:
            self.test_active_board.board[entry[0]][entry[1]].state = True
예제 #2
0
    def create_new_game(self, size, mines):
        if hasattr(self, 'board'):
            self.board.unload_board()
            self.removeItem(self.board.layout)

        self.game_over = False
        self.timer_counter = 0
        self.current_mines = mines
        self.timer.stop()

        self.board = Board(self.ctx, self, size, mines)
        self.addLayout(self.board.layout)
        self.update_top_layout()
예제 #3
0
class TestComponents(unittest.TestCase):
    def setUp(self):
        self.test_dead_board = Board(n=5)

        self.where_seed111_is_active = ((1, 0), (1, 1), (2, 2), (3, 2), (3, 3))
        self.test_active_board = Board(n=5)
        for entry in self.where_seed111_is_active:
            self.test_active_board.board[entry[0]][entry[1]].state = True

    def tearDown(self):
        pass

    def test_random_population(self):
        random.seed(111)
        self.test_dead_board.random_population()

        for i, row in enumerate(self.test_dead_board.board):
            for j, cell in enumerate(row):
                state_cell_seed111 = (i, j) in self.where_seed111_is_active
                self.assertEqual(cell.state, state_cell_seed111)

    def test_str_board(self):
        str_seed111 = "## ## ## ## ## \n-- -- ## ## ## \n## ## -- ## ## \n## ## -- -- ## \n## ## ## ## ## "
        self.assertEqual(self.test_active_board.str_board(), str_seed111)

    def test_neighbour_matrix(self):
        neighbour_matrix = [[2, 2, 1, 0, 0], [1, 2, 2, 1, 0], [2, 4, 3, 3, 1],
                            [0, 2, 2, 2, 1], [0, 1, 2, 2, 1]]

        self.assertEqual(self.test_active_board.neighbour_matrix(),
                         neighbour_matrix)

    def test_next_gen(self):
        self.test_active_board.next_gen()

        where_next_is_active = ((1, 1), (2, 2), (2, 3), (3, 2), (3, 3))

        for i, row in enumerate(self.test_active_board.board):
            for j, cell in enumerate(row):
                state_cell_next = (i, j) in where_next_is_active
                self.assertEqual(cell.state, state_cell_next)
예제 #4
0
class BoardTestCase(unittest.TestCase):
    def setUp(self):
        self.board = Board(9)
        self.puzzle = '0 9 4 0 0 0 1 3 0; 0 0 0 0 0 0 0 0 0; 0 0 0 0 7 6 0 0 2; 0 8 0 0 1 0 0 0 0; 0 3 2 0 0 0 0 0 0; 0 0 0 2 0 0 0 6 0; 0 0 0 0 5 0 4 0 0; 0 0 0 0 0 8 0 0 7; 0 0 6 3 0 4 0 0 8' 
        self.board.set_puzzle(self.puzzle)
        
    def test_set_puzzle(self):
        self.board.set_puzzle(self.puzzle)
        assert self.board.puzzle == self.puzzle
        assert self.board.empty_locations == 60
    
    def test_set_matrix(self):
        self.board.set_matrix(self.puzzle)
    
    def test_get_number(self):
        test_location = Location(0,1)
        assert self.board.get_number(test_location) == 9
        
    def test_set_number(self):
        test_location = Location(0,4)
        assert self.board.empty_locations == 60, "self.board.empty_locations == %s" % self.board.empty_locations
        self.board.set_number(test_location, 5)
        assert self.board.m.item(test_location) == 5
        assert self.board.empty_locations == 59
        self.board.set_number(test_location, 4)
        assert self.board.empty_locations == 59
        test_location = Location(0,1)
        self.board.set_number(test_location, 0)
        assert self.board.empty_locations == 60
        self.board.set_number(test_location, 0)
        assert self.board.empty_locations == 60
      
    def test_get_row(self):
        test_location = Location(2,4)
        row = self.board.get_row(test_location)
        test_row = matrix([0,0,0,0,7,6,0,0,2])
        assert (row==test_row).all(), "row = %s, test_row = %s" % (row, test_row)
        
    def test_get_column(self):
        test_location = Location(2,4)
        column = self.board.get_column(test_location)
        test_column = matrix([[0], [0], [7], [1], [0], [0], [5], [0], [0]])
        assert (column==test_column).all(), "column = %s, test_column = %s" % (column, test_column)
        
    def test_get_sector(self):
        test_location = Location(2,4)
        sector = self.board.get_sector(test_location)
        assert sector.shape == (3,3), "sector.shape = %s" % sector.shape
        test_sector = matrix([[0, 0, 0],[0, 0, 0],[0, 7, 6]])
        assert (sector==test_sector).all(), "sector = %s, test_sector = %s" % (sector, test_sector)
        
    def test_is_in_row(self):
        test_location = Location(2,4)
        expected_results = [False, True, False, False, False, True, True, False, False]
        f = vectorize(self.board.is_in_row, excluded = [1])
        results = f(range(1, self.board.dimension + 1), test_location)
        assert len(results) == len(expected_results)
        assert (results == expected_results).all()
        #for x in range (self.board.dimension):
        #    assert self.board.is_in_row(x + 1, test_location) == expected_results[x]

    def test_is_in_column(self):
        test_location = Location(2,4)
        expected_results = [True, False, False, False, True, False, True, False, False]
        f = vectorize(self.board.is_in_column, excluded = [1])
        results = f(range(1, self.board.dimension + 1), test_location)
        assert len(results) == len(expected_results)
        assert (results == expected_results).all()
        
    def test_is_in_sector(self):
        test_location = Location(2,4)
        expected_results = [False, False, False, False, False, True, True, False, False]
        f = vectorize(self.board.is_in_sector, excluded = [1])
        results = f(range(1, self.board.dimension + 1), test_location)
        assert len(results) == len(expected_results)
        assert (results == expected_results).all()
        
        #for x in range(self.board.dimension):
        #    test = self.board.is_in_sector(x + 1, test_location) == expected_results[x]
        #    assert test, "x = %s, expected_results[%s] = %s, board.is_in_sector(%s, %s) = %s" % (x, x, expected_results[x], x, test_location, test )
        
    def test_is_legal(self):
        test_location = Location(2,4)
        expected_results = [False, False, True, True, False, False, False, True, True]
        for x in range(self.board.dimension):
            assert self.board.is_legal(x + 1, test_location) == expected_results[x]
예제 #5
0
 def setUp(self):
     self.board = Board(9)
     self.puzzle = '0 9 4 0 0 0 1 3 0; 0 0 0 0 0 0 0 0 0; 0 0 0 0 7 6 0 0 2; 0 8 0 0 1 0 0 0 0; 0 3 2 0 0 0 0 0 0; 0 0 0 2 0 0 0 6 0; 0 0 0 0 5 0 4 0 0; 0 0 0 0 0 8 0 0 7; 0 0 6 3 0 4 0 0 8' 
     self.board.set_puzzle(self.puzzle)