Tetris - Rotate Piece and stay

1

Our POO teacher put us to perform this problem, rather it was correcting the code. I already managed to make the pieces move left and right and put the score, what I want is that the pieces can be rotated and that they remain fixed when they fall, as they leave.

// CS101 Lab 9  
// Name:  
// Lab Section:  
// Email:  
// Board.java  

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);
        }
    }
}
// CS101 Lab 9  
// Name:  
// Lab Section:  
// Email:  
// Grid.java  

import javax.swing.JComponent;
import java.awt.*;

public class Grid extends JComponent {
    int contents[][];  // each rows is an array of integers

    public Grid(int[][] contents) {
        this.contents = contents;
        Dimension d = new Dimension(getColumns()*Tetris.SQUARE_SIZE,
                                    getRows()*Tetris.SQUARE_SIZE);
        setSize(d);
        setPreferredSize(d);
        setOpaque(false);
    }

    public int getColumns() {
        return contents[0].length;
    }

    public int getRows() {
        return contents.length;
    } 

    void paintSquare(int row, int col, Graphics g) {
        if (contents[row][col] != 0)
            g.fillRect(Tetris.SQUARE_SIZE*col+1,
                        Tetris.SQUARE_SIZE*row+1,
                        Tetris.SQUARE_SIZE-2,
                        Tetris.SQUARE_SIZE-2);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int row = 0; row < contents.length; row++) {
            for (int col = 0; col < contents[row].length; col++) {
                    paintSquare(row,col,g);
            }               
        }
    }
}
// CS101 Lab 9  
// Name:  
// Lab Section:  
// Email:  
// Piece.java  

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();
    }

    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() {
            if(currentX>0)
                currentX--;
    }

    synchronized void moveRight() {
            if(currentX<(Board.COLUMNS-1))
                currentX++;
    }

    synchronized void rotateClockwise() {

    }

    synchronized void rotateCounterclockwise() {
    }

    void fall() {
        // esto es mio
        int y=(int) currentY;

         for (int i= y;i<Board.ROWS-getRows();i++)
         {
            currentY ++;
            updateLocation();
            repaint();
            Tetris.sleep(200);


         Tetris.sleep(50); 


    //    hasta aqui

        // replace the following by your code
        //Tetris.sleep(1);
    }
        }

    synchronized void drop() {
            currentY--;
            updateLocation();

    }
}
// CS101 Lab 9  
// Name:  
// Lab Section:  
// Email:  
// PieceFactory.java  

public class PieceFactory {

    public static final int[][] L1 =
        {{1,1},
         {0,1},
         {0,1}
        };

    public static final int[][] L2 =
        {{0,1},
         {0,1},
         {1,1}
        };

    public static final int[][] T =
        {{0,1},
         {1,1},
         {0,1}
        };

    public static final int[][] BOX =
        {{1,1},
         {1,1}
        };

    public static final int[][] BAR =
        {{0,1},
         {0,1},
         {0,1},
         {0,1} //{1,1,1,1}
        };

    public static final int[][] STEP1 =
        {{1,0},
         {1,1},
         {0,1}
        };

    public static final int[][] STEP2 =
        {{0,1},
         {1,1},
         {1,0}
        };

        public static final int[][] NUEVO =
        {{1,1},
         {1,1},
         {0,1}
        };
    public static final int[][][] SHAPES = {L1,L2,T,BOX,BAR,STEP1,STEP2,NUEVO};

    public static Piece createPiece() {
        int[][] s = SHAPES[(int) (Math.random()*SHAPES.length)];
        switch ((int) (Math.random()*11)) {
            case 0:
            case 1:
            case 2:
            case 3:
            default: return new Piece(s);
        }
    }
}
// CS101 Lab 9  
// Name:  
// Lab Section:  
// Email:  
// Tetris.java  

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;
    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) {

    }
}
    
asked by POO 29.11.2017 в 05:13
source

1 answer

0

Here I send you a way to do it. Exchanging rows by columns. and vice versa. This algorithm does the rotation no matter how the arrangement comes.

public int[][] rotacion(int [][] pieza){
   int filas = pieza.length;
   int columnas = pieza[0].length;

    int nuevo[][] = new int[columnas][filas]; //intercambiamos filas por columnas y viceversa


    for(int c = 0; c < columnas; c++){ // recorremos las columnas primero
        for(int f = 0; f < filas; f++){ // recorremos las filas

            nuevo[columnas - 1 - c][f] = pieza[f][c]; // Hacemos el intercambio

        }
    }        

    System.out.println("=============MOSTRANDO RESULTADO================");
    for(int f = 0; f < nuevo.length; f++){
        for(int c = 0; c < nuevo[0].length; c++){
            System.out.print(nuevo[f][c]);
        }
        System.out.println("");
    }

    return nuevo;
}
    
answered by 01.12.2017 в 02:27