Tetris/Java: Difference between revisions

Content added Content deleted
m (change header)
mNo edit summary
Line 5: Line 5:
import static java.lang.String.format;
import static java.lang.String.format;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Random;
import javax.swing.*;
import javax.swing.*;


Line 10: Line 11:
public static final int EMPTY = -1;
public static final int EMPTY = -1;
public static final int BORDER = -2;
public static final int BORDER = -2;

int[][] grid;


Shape fallingShape;
Shape fallingShape;
Line 20: Line 19:
int fallingShapeCol;
int fallingShapeCol;


int blockSize = Config.blockSize;
final int blockSize = Config.blockSize;
int nRows = Config.nRows;
final int nRows = Config.nRows;
int nCols = Config.nCols;
final int nCols = Config.nCols;
int topMargin = Config.topMargin;
final int topMargin = Config.topMargin;
int leftMargin = Config.leftMargin;
final int leftMargin = Config.leftMargin;

final int[][] grid = new int[nRows][nCols];


Thread fallingThread;
Thread fallingThread;
Scoreboard scoreboard;
final Scoreboard scoreboard = new Scoreboard();
static final Random rand = new Random();


public Tetris() {
public Tetris() {
Line 34: Line 36:
setFocusable(true);
setFocusable(true);


scoreboard = new Scoreboard();
grid = new int[nRows][nCols];
initGrid();
initGrid();
selectShape();
selectShape();
Line 100: Line 100:
fallingShape = preSelectedShape;
fallingShape = preSelectedShape;
Shape[] shapes = Shape.values();
Shape[] shapes = Shape.values();
preSelectedShape = shapes[(int) (Math.random() * shapes.length)];
preSelectedShape = shapes[rand.nextInt(shapes.length)];
if (fallingShape != null)
if (fallingShape != null)
fallingShape.reset();
fallingShape.reset();