---
description: Pseudo code that simulates the manual yarrow stalk divination process
---

# Appendix: Hexagram Divination Code



```
# Abstract base class for divination methods
class DivinationMethod:
    def generate_line(self):
        raise NotImplementedError("Subclasses must implement generate_line()")
    
    def cast_reading(self):
        raise NotImplementedError("Subclasses must implement cast_reading()")

# Example implementation with key methods abstracted
class YarrowStalkDivination(DivinationMethod):
    def __init__(self, total_stalks=49):
        self.total_stalks = total_stalks
        self.valid_outcomes = {
            "OLD_YIN": 6,
            "YOUNG_YANG": 7,
            "YOUNG_YIN": 8,
            "OLD_YANG": 9
        }
    
    def divide_stalks(self):
        """
        Abstract method for dividing stalks
        Returns: tuple of (pile1, pile2)
        """
        pass
    
    def count_remainder(self, pile):
        """
        Abstract method for counting remainders
        Returns: integer remainder
        """
        pass
    
    def calculate_line_value(self, divisions):
        """
        Abstract method for calculating line value
        Returns: integer (6, 7, 8, or 9)
        """
        pass
    
    def generate_line(self):
        """
        Public interface for generating a single line
        Returns the numerical value of the line (6-9)
        """
        divisions = []
        stalks = self.total_stalks
        
        # Perform the three divisions
        for _ in range(3):
            pile1, pile2 = self.divide_stalks()
            remainder = self.count_remainder(pile1 + pile2)
            divisions.append(remainder)
            stalks -= remainder
        
        return self.calculate_line_value(divisions)
    
    def cast_reading(self):
        """
        Generates a complete hexagram reading
        Returns: list of 6 integers representing the lines
        """
        return [self.generate_line() for _ in range(6)]

# Example usage and extension point
class CustomYarrowMethod(YarrowStalkDivination):
    def divide_stalks(self):
        # Implement your proprietary division method here
        pass
    
    def count_remainder(self, pile):
        # Implement your proprietary counting method here
        pass
    
    def calculate_line_value(self, divisions):
        # Implement your proprietary calculation method here
        pass

# Example interface for generating readings
def perform_divination():
    method = CustomYarrowMethod()
    hexagram = method.cast_reading()
    return hexagram
```

1. `divideStalks(stalks)`: Simulates dividing the stalks into two random piles.
2. `countByFours(pile)`: Simulates counting out stalks in groups of four and returning the remainder.
3. `yarrowStalkLine()`: Performs the entire process for generating one line:
   * Does the first division, setting aside one stalk.
   * Performs the second and third divisions.
   * Calculates the total and returns the appropriate line value.
4. `castHexagram()`: Calls `yarrowStalkLine()` six times to generate a complete hexagram.

This implementation closely replicates the traditional yarrow stalk method, including:

* Starting with 49 stalks
* Performing three divisions for each line
* Calculating the results based on the sums of these divisions

The probabilities generated by this method will match those of the traditional yarrow stalk method:

* 6 (Old Yin): 1/16 chance
* 7 (Young Yang): 5/16 chance
* 8 (Young Yin): 7/16 chance
* 9 (Old Yang): 3/16 chance
