Exemplo n.º 1
0
 def __init__(self, size, blocks):
     self._size = size
     self.blocks = blocks
     for block in self.blocks:
         location = block.location
         for i in range(len(block)):
             cell = block[location]
             if cell is not None:
                 continue
             is_service, values = Kakuro._get_content(block, location)
             adjacent_block = self.find_adjacent_block(block, location)
             if adjacent_block is not None:
                 is_service_abj, values_adj = Kakuro._get_content(
                     adjacent_block, location, cell)
                 if not (is_service ^ is_service_abj):
                     new_cell = Cell(is_service, values + values_adj,
                                     location)
                 else:
                     raise ValueError("Something went wrong")
                 adjacent_block[location] = new_cell
             else:
                 new_cell = Cell(is_service, values, location)
             block[location] = new_cell
             location = location + (Location(1, 0) if block.is_horizontal
                                    else Location(0, 1))
Exemplo n.º 2
0
 def test_find_adjacent_block(self):
     block = self.kakuro.blocks[0]
     expected = [Location(0, 1), Location(0, 2)]
     actual = [
         x.location for x, y in self.kakuro.find_all_adjacents_blocks(block)
     ]
     self.assertSequenceEqual(expected, actual)
Exemplo n.º 3
0
 def find_adjacent_block(self, block, location):
     for adjacent_block in self.blocks:
         if adjacent_block == block or block.is_horizontal == adjacent_block.is_horizontal:
             continue
         current_location = location
         while current_location.x >= 0 and current_location.y >= 0:
             if adjacent_block.location == current_location:
                 if current_location.distance_on_line(location) < len(
                         adjacent_block):
                     return adjacent_block
             current_location = current_location + Location(
                 -1, 0) if not block.is_horizontal else Location(0, -1)
Exemplo n.º 4
0
 def parse(raw_kakuro):
     kakuro = []
     size = None
     is_horizontal_line = None
     for line in raw_kakuro:
         line = line.rstrip('\n')
         info = line.split(' ')
         if info[0] == 'S':
             size = tuple(map(lambda x: int(x), info[1].split('x')))
         elif info[0] == 'H':
             is_horizontal_line = True
         elif info[0] == 'V':
             is_horizontal_line = False
         else:
             if is_horizontal_line is None:
                 raise Exception(
                     'value of "is_horizontal_line" is not assigned')
             block = Block(Location.parse(',', info[0]), int(info[1]),
                           int(info[2]), is_horizontal_line)
             kakuro.append(block)
     if size is None:
         raise Exception('value of "size" is not assigned')
     return Kakuro(size, kakuro)
Exemplo n.º 5
0
 def test_parse(self):
     location = Location.parse(',', '0,1')
     self.assertEqual(0, location.x)
     self.assertEqual(1, location.y)
Exemplo n.º 6
0
 def test_get_known_values(self):
     locations =[Location(0, 2), Location(0, 3), Location(0, 4)]
     values = [2, 4, 6]
     for i in range(len(values)):
         self.block[locations[i]] = Cell(False, values[i], locations[i])
     self.assertSequenceEqual(values, self.block.get_known_values())
Exemplo n.º 7
0
 def setUp(self):
     self.block = Block(Location(0, 1), 12, 4, True)
     location = Location(0, 1)
     self.block[location] = Cell(True, 12, location)
Exemplo n.º 8
0
 def test_equals_get_block_partitions_and_get_partitions(self):
     block = Block(Location(1, 0), 16, 3, False)
     self.assertSequenceEqual(['97'], Solver.get_block_partitions(block))