Cross pollination is the process of one flower fertilizing another flower with its pollen. In my specialization project flowers can cross-pollinate with nearby flowers. When they do, their colors mix to create a new flower.
A core feature of the cross-pollination mechanic is the TryToBreed function. Let's break down the process into smaller steps.
The flowers use a State Machine. The player can plant the base colors: white, yellow, red, and blue. When the player plants a flower it will be spawned in the Seed State. When the flower grows it progresses to the next state; once a flower has reached the Bud State it is eligible for breeding. The base colors can cross-pollinate to breed the hybrid colors: pink, purple, orange, and green.
Every flower tries to breed with its eight immediate surrounding neighbors. If the breeding pair's colors are compatible and they share a vacant neighbor, a new flower is spawned. I used a lambda to compute the possible vacant shared Neighbor. I used to find lambdas complicated, but now I enjoy their succinct usefulness!
A challenge was how to store the possible color combinations. I wanted it to be a clean and readable look-up table that would easily facilitate eventual expansion of additional color combinations. I also wanted it to be order independent to erase the need of having to define combinations for both red + white and white + red. This proved to be a challenge.
After trying different versions I was unsatisfied with their implementation, they were all convoluted and hard to read. I decided to sleep on it and the next day I remembered an assignment from Network Programming where I had definied my own hash struct to keep track of the ClientData of connecting clients. I extrapolated the design I had used and applied it to my flowers. The result:
The flower look-up table could now be implemented in the clean and readable approach I wanted:
This project was greatly helped by using an agile approach. By breaking down bigger tasks into smaller tasks and iterating on them enabled me to make progress every day instead of becoming overwhelmed by an impossibly convuluted task. I am very proud of what I accomplished, specifically how easy this system would be to expand and continue to develop, the readability of my code, and how I applied design patterns from my education.