Tracking and Placing a Pentomino
Let's learn how to control shape movements in Phoenix LiveView.
The Shape
constructor takes in a rotation, reflection, and location value and returns the struct that represents the orientation and location of that shape on the board. This rotation, reflection, and location will be the result of a user selecting a shape and moving it around the board before finally dropping it into place. A user may do any combination of rotating the shape, reflecting the shape, or moving it up and down on the board before deciding to place it. So, we need a way to track all of these user inputs in one place before feeding the final rotation, reflection, and location values into our Shape
constructor. We’ll implement the Pentomino
module to do exactly that.
Like the Shape
module, the Pentomino
module will produce structs that know the shape name, rotation, reflection, and location. Most importantly, however, the Pentomino
module will implement a series of reducers that we can string together given a set of user inputs to change the rotation, reflection, and location of the pentomino. Then, the pentomino will be converted into a shape in order to be placed on the board at the correct set of points.
If you’re thinking that this process—creating a Pentomino
struct, transforming it with a series of reducers, and then converting it into a shape that can be displayed to the user—sounds a lot like CRC, you’d be right!
Let’s implement our new module.
Get hands-on with 1400+ tech skills courses.