To fill in the board square covered by a piece, it
would be helpful to have a method similar to the one above that copies
the non-zero entries from the given array into the corresponding board
squares.
I need to implement this now, but I have the following questions:
where do I put the contents array? I don't see a board array in the program
do
i need to put the contents array inside the board once it was laid down
or once it appears? and how do i know a piece was laid down? what if
statement should i use for that?
Code:
import java.awt.*;
public class Board extends Grid {
public static final int COLUMNS = 16;
public static final int ROWS = 32;
public static final Color BLUE = new Color(0,0,128,40);
public Board() {
super(new int[ROWS][COLUMNS]);
setSize(COLUMNS*Tetris.SQUARE_SIZE,
ROWS*Tetris.SQUARE_SIZE);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(BLUE);
paintStripes(g);
}
void paintStripes(Graphics g) {
for (int i = 0; i < COLUMNS; i += 4) {
g.fillRect(i*Tetris.SQUARE_SIZE,0,
Tetris.SQUARE_SIZE*2,Tetris.SQUARE_SIZE*ROWS);
}
}
}
Code:
public class PieceFactory {
public static final int[][] L1 =
{{1,1,0,0},
{0,1,0,0},
{0,1,0,0},
{0,0,0,0},
};
public static final int[][] L2 =
{{0,1,0,0},
{0,1,0,0},
{1,1,0,0},
{0,0,0,0},
};
public static final int[][] T =
{{0,1,0,0},
{1,1,0,0},
{0,1,0,0},
{0,0,0,0},
};
public static final int[][] BOX =
{{1,1,0,0},
{1,1,0,0},
{0,0,0,0},
{0,0,0,0},
};
public static final int[][] BAR =
{{1,1,1,1},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
};
public static final int[][] STEP1 =
{{1,0,0,0},
{1,1,0,0},
{0,1,0,0},
{0,0,0,0},
};
public static final int[][] STEP2 =
{{0,1,0,0},
{1,1,0,0},
{1,0,0,0},
{0,0,0,0},
};
public static final int[][][] SHAPES = {L1,L2,T,BOX,BAR,STEP1,STEP2};
public static Piece createPiece() {
int[][] s = SHAPES[(int) (Math.random()*SHAPES.length)];
switch ((int) (Math.random()*10)) {
case 0:
case 1:
case 2:
case 3:
default: return new Piece(s);
}
}
}
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Tetris extends JFrame implements KeyListener {
public static final int SQUARE_SIZE = 10; // 10 by 10 pixels
static Board board;
static Tetris game;
JPanel mainPanel;
public Piece currentPiece;
int score = 0;
JButton scoreButton;
public Tetris() {
super("Tetris");
game = this;
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
scoreButton = new JButton("0");
scoreButton.setEnabled(false);
pane.add(scoreButton,BorderLayout.NORTH);
board = new Board();
mainPanel = new JPanel();
mainPanel.setLayout(null);
mainPanel.add(board);
mainPanel.setPreferredSize
(new Dimension(Board.COLUMNS*SQUARE_SIZE,
Board.ROWS*SQUARE_SIZE));
pane.add(mainPanel);
addKeyListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
pack();
show();
setResizable(false);
}
public void addToScore(int v) {
score += v;
scoreButton.setText(""+score);
}
public int getScore() {
return score;
}
static Board getBoard() {
return board;
}
static Tetris getGame() {
return game;
}
static void sleep(int milliseconds) {
try {
Thread.sleep(milliseconds);
} catch (InterruptedException ie) {
}
}
public static void main(String[] args) {
Tetris game = new Tetris();
while (true) {
game.dropPiece();
}
}
void dropPiece() {
currentPiece = PieceFactory.createPiece();
mainPanel.add(currentPiece);
currentPiece.repaint();
currentPiece.fall();
//mainPanel.remove(currentPiece);
board.repaint();
addToScore(1);
}
public void keyPressed(KeyEvent event) {
int key = event.getKeyCode();
switch (key) {
case KeyEvent.VK_UP: // up arrow
case KeyEvent.VK_KP_UP:
currentPiece.rotateCounterclockwise();
break;
case KeyEvent.VK_DOWN: // down arrow
case KeyEvent.VK_KP_DOWN:
currentPiece.rotateClockwise();
break;
case KeyEvent.VK_LEFT: // left arrow
case KeyEvent.VK_KP_LEFT:
currentPiece.moveLeft();
break;
case KeyEvent.VK_RIGHT: // right arrow
case KeyEvent.VK_KP_RIGHT:
currentPiece.moveRight();
break;
case KeyEvent.VK_SPACE: // space bar
currentPiece.drop();
}
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
}
Code:
public class Piece extends Grid {
int currentX; // current X location on the board
double currentY; // current Y location on the board
public Piece(int shape[][]) {
super(shape);
currentX = 7;
currentY = 2;
updateLocation();
}
public Piece(int shape[][], currentX, currentY) {
super(shape);
this.currentX = currentX;
this.currentY = currentY;
updateLocation()
}
void updateSize() {
setSize(Tetris.SQUARE_SIZE*getColumns(),
Tetris.SQUARE_SIZE*getRows());
}
void updateLocation() {
setLocation(Tetris.SQUARE_SIZE*currentX,
(int) (Tetris.SQUARE_SIZE*currentY));
}
synchronized void moveDown() {
}
synchronized void moveLeft() {
currentX--;
updateLocation();
}
synchronized void moveRight() {
currentX++;
updateLocation();
}
synchronized void rotateClockwise() {
int x = currentX;
int y = currentY;
currentPiece = new Piece(s, x, y);
updateLocation();
}
synchronized void rotateCounterclockwise() {
}
void fall() {
// replace the following by your code
Tetris.sleep(2000);
}
synchronized void drop() {
currentY++;
updateLocation();
}
public static int [][] multiplyMatrix(int [][] m1)
{
int [][] m2 =
{{0,0,0,1},
{0,0,1,0},
{0,1,0,0},
{1,0,0,0},
};
int[][] result = new int[4][4];
// multiply
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
for (int k=0; k<4; k++)
if (m1[i][k] * m2[k][j] > 0)
{
result[i][j] = 1;
}
else
{
result[i][j] = 0;
}
return result;
}
}