def get_part_positions(self, root_position=None): """Get the hole positions of each part of the noodle based on the root position. Args: root_position: The position of the root part. If not specified the root position will be discovered from the subclass. Returns: A list of the hole position integers representing each part of the noodle. """ if root_position is None: root_position = getattr(self, 'position') positions = [root_position] for i in range(1, 5): position = holes.find_position(positions[-1], getattr(self, 'part{}'.format(i))) if position is None: raise PositionUnavailableException( 'The position for part {} is off the board'.format(i)) positions.append(position) return positions
def _find_root_pos(self, noodle, part_pos, hole_index): """Find the board hole position for the root part of the noodle.""" index = hole_index # Traverse backwards along the noodle to the root position for pos in reversed(range(part_pos)): index = holes.find_position( index, orientation.opposite(noodle.parts[pos])) if index is None: raise PositionUnavailableException( 'Part {} of the noodle is not on the board'.format(pos)) return index
def _unoccupied_holes(self): """Return a sequence of the hole numbers on the board that are empty.""" occupied = set() for noodle in self.noodles: position = noodle.position occupied.add(position) for part in noodle.parts: position = holes.find_position(position, part) occupied.add(position) return set(range(35)) - occupied
def _draw_noodle(self, noodle, position, fade_duration=0): last_position = position try: colour = noodle.colour except AttributeError: colour = noodle.noodle.colour try: image = NOODLE_IMAGES[noodle.designation] except AttributeError: image = NOODLE_IMAGES[noodle.noodle.designation] def show_image(item): x1, y1, x2, y2 = self._canvas.bbox(item) def show(): self._canvas.itemconfig(item, fill='#4d4d4d') self._canvas.create_image( x1 + settings.image_offsets['x'] + ((x2 - x1) // 2), y1 + settings.image_offsets['y'] + ((y2 - y1) // 2), image=image) return show self._fade.fadein(self._holes[last_position], colour, duration=fade_duration, onfaded=show_image(self._holes[last_position])) self._canvas.itemconfig(self._holes[last_position], outline='#4d4d4d', width=2) for part in noodle.parts: last_position = holes.find_position(last_position, part) self._fade.fadein(self._holes[last_position], colour, duration=fade_duration, onfaded=show_image(self._holes[last_position])) self._canvas.itemconfig(self._holes[last_position], outline='#4d4d4d', width=2)
def test_neighbour_positions_hole0(self): self.assertEqual(find_position(0, orientation.E), 1)
def test_no_neighbour(self): self.assertIsNone(find_position(0, orientation.W))
def test_invalid_position(self): self.assertIsNone(find_position(100, orientation.E))