Naming seems a little misleading in this tutorial. A class called Shape represents one element that falls. It seems that the Tetrominoes enum describes what kind of element it has (that is, it is "shape"!).
So, the code you created creates a new element and determines its shape.
The rotateRight() and rotateLeft() methods do not change the form itself to allow the tryMove() method to check whether the movement is legal and ignore it if it is not (for example, if you rotate an object into a wall). tryMove() just keeps the old values ββ(including the old Shape instance) when moving is not allowed. If rotateLeft() / rotateRight() changed Shape , then it will have to cancel this operation, which will complicate the code.
In addition, there are several nitpicks with this code:
- I would call the
Tetrominoes class Tetrominoes , since enum types are usually singular (since you often refer to the same element: Tetromino.SquareShape . - I would add information about the specific coordinate of each
Tetromino to this enum , effectively putting most of the logic from the setShape() method setShape() . The Board class mixes logic and presentation, it should be divided (this greatly facilitates verification).
For example, the Board class can implement all the logic without any graphics (i.e., nothing refers to java.awt or javax.swing ). Then you write a BoardPanel that draws the state of the Board and interacts with the user by calling the appropriate Board methods.
source share