Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions com/zetcode/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import com.zetcode.Shape.Tetrominoe;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Collections;
import java.util.LinkedList;

public class Board extends JPanel {

Expand All @@ -27,6 +26,7 @@ public class Board extends JPanel {
private JLabel statusbar;
private Shape curPiece;
private Tetrominoe[] board;
private LinkedList<Shape> pieceBag;

public Board(Tetris parent) {

Expand Down Expand Up @@ -59,6 +59,7 @@ void start() {

curPiece = new Shape();
board = new Tetrominoe[BOARD_WIDTH * BOARD_HEIGHT];
pieceBag = new LinkedList<>();

clearBoard();
newPiece();
Expand Down Expand Up @@ -174,7 +175,10 @@ private void pieceDropped() {

private void newPiece() {

curPiece.setRandomShape();
while ((curPiece = pieceBag.poll()) == null) {
newBag();
}

curX = BOARD_WIDTH / 2 + 1;
curY = BOARD_HEIGHT - 1 + curPiece.minY();

Expand All @@ -188,8 +192,15 @@ private void newPiece() {
}
}

private boolean tryMove(Shape newPiece, int newX, int newY) {
private void newBag() {
for (Tetrominoe tetrominoe : Tetrominoe.values()) {
if (tetrominoe.equals(Tetrominoe.NoShape)) continue;
pieceBag.offer((new Shape()).setShape(tetrominoe));
}
Collections.shuffle(pieceBag);
}

private boolean tryMove(Shape newPiece, int newX, int newY) {
for (int i = 0; i < 4; i++) {

int x = newX + newPiece.x(i);
Expand Down Expand Up @@ -256,7 +267,7 @@ private void removeFullLines() {

private void drawSquare(Graphics g, int x, int y, Tetrominoe shape) {

Color colors[] = {new Color(0, 0, 0), new Color(204, 102, 102),
Color[] colors = {new Color(0, 0, 0), new Color(204, 102, 102),
new Color(102, 204, 102), new Color(102, 102, 204),
new Color(204, 204, 102), new Color(204, 102, 204),
new Color(102, 204, 204), new Color(218, 170, 0)
Expand Down Expand Up @@ -328,10 +339,9 @@ public void keyPressed(KeyEvent e) {
case KeyEvent.VK_P -> pause();
case KeyEvent.VK_LEFT -> tryMove(curPiece, curX - 1, curY);
case KeyEvent.VK_RIGHT -> tryMove(curPiece, curX + 1, curY);
case KeyEvent.VK_DOWN -> tryMove(curPiece.rotateRight(), curX, curY);
case KeyEvent.VK_UP -> tryMove(curPiece.rotateLeft(), curX, curY);
case KeyEvent.VK_DOWN -> tryMove(curPiece.rotateLeft(), curX, curY);
case KeyEvent.VK_UP -> tryMove(curPiece.rotateRight(), curX, curY);
case KeyEvent.VK_SPACE -> dropDown();
case KeyEvent.VK_D -> oneLineDown();
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions com/zetcode/Shape.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ protected enum Tetrominoe { NoShape, ZShape, SShape, LineShape,
TShape, SquareShape, LShape, MirroredLShape }

private Tetrominoe pieceShape;
private int coords[][];
private int[][] coords;
private int[][][] coordsTable;


Expand All @@ -35,17 +35,15 @@ private void initShape() {
setShape(Tetrominoe.NoShape);
}

protected void setShape(Tetrominoe shape) {
protected Shape setShape(Tetrominoe shape) {

for (int i = 0; i < 4 ; i++) {
for (int i = 0; i < 4; i++) {

for (int j = 0; j < 2; ++j) {

coords[i][j] = coordsTable[shape.ordinal()][i][j];
}
System.arraycopy(coordsTable[shape.ordinal()][i], 0, coords[i], 0, 2);
}

pieceShape = shape;
return this;
}

private void setX(int index, int x) { coords[index][0] = x; }
Expand Down