initial commit
This commit is contained in:
108
doc/pieces_representation.md
Normal file
108
doc/pieces_representation.md
Normal file
@@ -0,0 +1,108 @@
|
||||
# Pieces representation
|
||||
|
||||
## What are polyominos ?
|
||||
|
||||
In this game, pieces are represented as a polyomino with a color.
|
||||
Polyominos are mathematical objects consisting of multiple edge-touching squares.
|
||||
There must be a path from every cell to every other cell, going from square to square only through the sides and not the corners.
|
||||
Polyominos can be classified in 3 ways:
|
||||
|
||||
- Fixed polyominos : only translation is allowed
|
||||
- One-sided polyominos : only translation and rotation are allowed
|
||||
- Free polyomins : translation, rotation, and reflection are allowed
|
||||
|
||||
For more detailed informations about polyominos, check the [Wikipedia page](https://en.wikipedia.org/wiki/Polyomino).
|
||||
|
||||
Most stacker game uses one-sided polyominos, which results in 7 polyominos of size 4, also known as tetrominos.
|
||||
In this game too, one-sided polyominos will be used since we will only allow to move and rotate the pieces.
|
||||
|
||||
Internally, Polyominos are represented as a set of Cell.
|
||||
This means the cells can be in any order but can't be duplicates.
|
||||
A cell is simply a position on a 2D grid, so a polyomino is determined by the position of its cells.
|
||||
This means however that 2 polyominos of same shape but different positions will be interpreted as different polyominos.
|
||||
To solve this, we normalize the position of the polyominos so that their left-most column is at x=0 and their bottom-most row at y=0.
|
||||
|
||||
Even if there is only 7 one-sided 4-minos, there is already more than 9,000 one-sided 10-minos.
|
||||
Since the aim of this game is to be playable with polyominos of any size, listing all polyominos manually isn't viable.
|
||||
We will need a way to:
|
||||
|
||||
1. Generate all one-sided polyominos of size n
|
||||
2. Set their spawn positions and rotation centers
|
||||
|
||||
Aditionally, for this game we will also a want a way to separate the polyominos into multiple categories, specifically to allow removing those with holes who are more unplayable.
|
||||
|
||||
## Ordering the polyominos
|
||||
|
||||
For practical reasons, we want to be able to sort all polyominos of the same size.
|
||||
This is done very simply:
|
||||
|
||||
- If one polyomino has an inferior length than another, it is deemed inferior
|
||||
- If two polyomino have the same length, we check all the cells of their square, from left to right, and repeating from up to bottom, for the first position where a polyomino has a cell that another doesn't, the polyomino with the cell is deemed inferior
|
||||
|
||||
Once the polyomino are ordered, it is very simple to attribute them a color, we can simply iterate through the list while looping over the color list.
|
||||
|
||||
## 1. Generating polyominos
|
||||
|
||||
The method used to generate polyominos is similar to the [inductive method](https://en.wikipedia.org/wiki/Polyomino#Inductive_algorithms) described in the previously mentionned Wikipedia page.
|
||||
|
||||
The algorithm is the following:
|
||||
|
||||
1. Add a single cell at position (0,0), and number it with 0
|
||||
2. Call the generator function
|
||||
1. If we get a polyomino of the size we want:
|
||||
1. We rotate it in its 4 possible rotations and sort them
|
||||
2. If the polyomino was generated in its lowest rotation, we add it to the list, else we discard it
|
||||
3. Stop this instance of the function (the function is recursive, see step 2.3.2)
|
||||
2. Else we number each adjacent cell to the polyomino with a number higher than the last numbered cell, unless:
|
||||
1. If a cell was already numbered then we don't touch it
|
||||
2. If a cell is on top of the polyomino then we don't number it
|
||||
3. If a cell is below y=0, or at exactly x=0 and y<0, then we don't number it
|
||||
3. For each cell with a higher number than the last added one:
|
||||
1. We add this cell to the polyomino
|
||||
2. We call the generator function (recursive function!)
|
||||
3. We remove this cell from the polyomino
|
||||
3. Return the list of polyominos
|
||||
|
||||
The exact number of one-sided polyominos up to size 12 (and higher, but we only generated up to size 12) is known, and this method generated exactly theses numbers, without duplicates.
|
||||
|
||||
By marking cells and adding only ones that have a higher number than the last one everytime, we generate each fixed polyomino exactly n times. By ignoring the cells below y=0, or at exactly x=0 and y<0, we generate each fixed polyomino exactly 1 time.
|
||||
An one-sided polyomino has 4 rotations, some of which can be the same if the polyomino has symmetries. 2 rotations that are the same corresponds to 1 fixed polyomino, we will refer to theses 2 rotation as 1 "unique" rotation.
|
||||
Because we generate every fixed polyomino exactly 1 time, that means each "unique" rotation of an one-sided polyomino is generated exactly one-time, which includes its lowest rotation. That's how we can get away with only checking the rotation of the polyomino and not comparing it to the rest of the generated polyominos.
|
||||
|
||||
## 2. Setting the spawn position of polyominos
|
||||
|
||||
When a polyomino is created, an attribute named its length is computed. This is simply the max between its width and its height. Thus the polyomino can be inscribed in a square of size ``length``.
|
||||
So now we can assume the polyomino is always inscribed in a square of origin (0,0) and size ``length`` (which should always be the case under normal circumstances). This make the rotation center very easy to find as it is simply the center of this square, and rotating is as simple as rotating a matrix:
|
||||
|
||||
- Clockwise (CW) rotation : ``x, y = y, (length - 1) - x``
|
||||
- 180° rotation : ``x, y = (length - 1) - x, (length - 1) - y``
|
||||
- Counter-clockwise (CCW) rotation : ``x, y = (length - 1) - y, x``
|
||||
|
||||
_Note we set the origin at the bottom-left corner instead of the up-left corner in a matrix, so the formulae aren't exactly the same._
|
||||
|
||||
The second challenge comes in finding a normalized spawn position for pieces. To do this, we first need to find which rotation the piece needs to spawn on, and then center it in the middle of its square.
|
||||
For the rotation, **we want to find the side which is both the widest and the flattest**.
|
||||
**Widest** means that we prefer if the piece is oriented horizontally rather than vertically.
|
||||
**Flattest** means we prefer the side with the most cell at the bottom of the piece.
|
||||
The current algorithm for doing so is the following:
|
||||
|
||||
1. Check if the polyomino has more lines horizontally or vertically, this will determine either 2 or 4 sides which are widest than the others, and the others will be discarded
|
||||
2. For each potential side check the number of cell on the first line, if one has more than every others, then this side is choosed, else we only keep the sides that tied and repeat for the next line
|
||||
3. If we still have at least 2 sides tied, we do the same again but check the flatness of the side to the left instead, this will make the pieces overall have more cells to their left at spawn
|
||||
4. If there is still no winner, we sort the remaining sides (by simulating having them selectionned and then sort the resulting polyominos) and keep the lowest
|
||||
5. We rotate the piece so that the chosen side ends up at the bottom of the polyomino
|
||||
6. We center the polyomino inside its square, since we chose the widest side it will always fill the width of the square, but for the height it can be asymmetric, in that case we place it one line closer to the top than the bottom
|
||||
|
||||
_Note we could actually just skip straight to step 4 because it always give the same orientation, but we want the spawn rotations to follow somewhat of a logic. Step 1 to 3 actually already works for 99% of polyominos and they constrain step 4 too by preselecting certain sides._
|
||||
|
||||
## 3. Separating the polyominos into categories
|
||||
|
||||
For this game, we want the polyominos to be broken down into 3 categories:
|
||||
|
||||
- Convex: this is said of a polyomino where every row and column is formed of at most one continous line of cells
|
||||
- Holeless: the polyominos which are neither convex nor have a hole
|
||||
- Others: the polyominos who have a hole (thoses are by definition not convex)
|
||||
|
||||
To check for convexity, we simply iterate trough each row and column, and check cell by cell if they are all contiguous.
|
||||
|
||||
To check for holes, we list every empty cells starting from the exterior of the square of the polyomino, then add every adjacent empty cell recursively. If the cell has an hole then there is at least one empty cell we could not attaign, and since we know the size of the square and of the polyomino, we can compute wheter we have the right number of empty cells.
|
||||
Reference in New Issue
Block a user