Why create a new tutorial on an object - Java Tetris

I'm just new to Java and I found this good tutorial for creating a Java Tetris game.

I don’t have a mentor or tutor to help me with this - I’ve been looking for one forever :( therefore, I am currently learning Java and PHP :)

Anyway, I found a website: http://zetcode.com/tutorials/javagamestutorial/tetris/

One program method that I don't get in the Shape.java class:

 public Shape rotateLeft() { if (pieceShape == Tetrominoes.SquareShape) return this; Shape result = new Shape(); result.pieceShape = pieceShape; for (int i = 0; i < 4; ++i) { result.setX(i, y(i)); result.setY(i, -x(i)); } return result; } 

Why do we need to create a new Object Shape result = new Shape(); if he can already get the current part from the pieceShape variable?

+4
source share
4 answers

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.

+5
source

The method you posted returns a shape that is rotated to the left. If you did not create a new shape, the original shape, which is a class field and used throughout, would be rotated.

In the case of a square shape that does not change when turning left, you can still return the original one.

+3
source

Despite carefully looking at the tutorial, I would say the following: since Shape is represented as a separate element, and rotateLeft() can be a method of the Shape instance, it may also normally rotate the element in place, i.e. Do not return a rotated copy, but change the coordinates of the block of the current form. Creating a new falling element would mean creating a new Shape with a default orientation.

0
source

It seems that the author does not mutate Shape in rotateLeft() , because it cannot be taken as a valid step. In Board inner TAdapter class calls tryMove() and sets only the current Shape ( curShape variable), if acceptable. If it was modified before this check, he would have to install it again if it is invalid. You can probably call this method rotatedLeftCopy() to indicate that it does not change state. Or the check must be done in advance, and then rotateLeft() will safely change the Shape in place.

0
source

Source: https://habr.com/ru/post/1343747/


All Articles