Question: I am making a simple tetris game. It is almost completed I just have a few questions. How would I implement gravity into my 2d

I am making a simple tetris game. It is almost completed I just have a few questions. How would I implement gravity into my 2d array to make each piece fall to the bottom? Another thing that I am having issues with is getting a new random current piece to place into the board. If anyone can give any kind of help it will be well appreciated.

here is my code:

import java.util.*;

public class Prog1 { private static final int DEFAULT_COLS = 6; private static final int DEFAULT_ROWS = 7; private static int ZEE = 0; private static int TEE = 1; private static int ES = 2; private static int EYE = 3; private static int OH = 4; private static int JAY = 5; private static int EL = 6;

private char[][] getRandomPiece(){ int rint = new java.util.Random().nextInt(7); // Magic number 7 if (rint == ZEE) return new char[][]{{'#', '#', ' '}, {' ', '#', '#'}}; else if (rint == TEE) return new char[][]{{' ', '#', ' '}, {'#', '#', '#'}}; else if (rint == ES) return new char[][]{{' ', '#', '#'}, {'#', '#', ' '}}; else if (rint == EYE) return new char[][]{{'#', '#', '#', '#'}, {' ', ' ', ' ', ' '}}; else if (rint == OH) return new char[][]{{'#', '#'}, {'#', '#'}}; else if (rint == JAY) return new char[][]{{'#', ' ', ' '}, {'#', '#', '#'}}; else //if (rint == EL) return new char[][]{{' ', ' ', '#'}, {'#', '#', '#'}}; }

public void printCurrentPiece(){ if (currentPiece == null) return; System.out.println("Current piece:"); System.out.println(); for (int i = 0; i < currentPiece.length; i++){ System.out.print("|"); for (int j = 0; j < currentPiece[0].length; j++){ System.out.print(currentPiece[i][j]); System.out.print("|"); } System.out.println(); } System.out.println();

}

private char[][] board; private char[][] currentPiece;

/** * Return the width of the current piece. */ public int widthOfCurrentPiece() { System.out.print("Specify column in which to place current piece (0 - "); System.out.print(board[0].length - currentPiece[0].length); System.out.println(")");

return currentPiece[0].length; }

/** * Initialize the board array to a new char array with numRows rows and * numCols columns. * Set each element of the board array to ' '. * Assign to currentPiece a new random piece. */ public void initializeBoard(int numRows, int numCols){ board = new char[numRows][numCols]; for (int r = 0; r < board.length; r++){ for (int c = 0; c < board[0].length; c++){ board[r][c] = ' '; if(board[board.length - 1][c] == ' '){ board[board.length - 1][c] = '_'; } } } currentPiece = getRandomPiece();

}

public void printBoard() { for (int r = 0; r < board.length; r++){ System.out.print("|"); for (int c = 0; c < board[r].length; c++){ System.out.print(board[r][c]); System.out.print("|"); } System.out.println(); } int hold = 0; int numberOfSpaces = 2; for(int i = 0; i < board[0].length; i++){ while(hold != board[0].length){ String space = String.format("%"+ numberOfSpaces +"s", hold); System.out.printf(space); hold++; } } System.out.println(); }

/** * Given a valid columnSelection as its only parameter, this method * places the current piece on the board. The columnSelection * parameter is the index of the column in which the user wishes to * place the current piece. * Return true if the game is over and false otherwise. */ public boolean placeCurrentPiece(int columnSelection){ // main method I need help in int counter; int counter2 = 0;

if(currentPiece.length == 2){ counter = board.length - 2; }else{ counter = board.length - 1; }

for(int r = 0; r < currentPiece.length; r++){ for (int c = 0; c < currentPiece[r].length; c++){ if(board[board.length - 1][c] == '_'){

// if counter is equal to 5 it will place the piece in the last row of the array. I need to make it so if that column is selected again //depending on the length of the piece it will be placed directly on top of the first piece.

board[r + counter][c + columnSelection] = currentPiece[r][c];

}else if(board[board.length - 1][c] != '_' || board[board.length - 1][c] == ' '){

board[r + counter][c + columnSelection] = currentPiece[r][c]; } } }

for(int r = 0; r < board.length; r++){ for (int c = 0; c < board[r].length; c++){ if(board[board.length - 1][c] == ' '){ board[board.length - 1][c] = '_'; } } }

// the game is over when the current piece placed exceeds the limit of the board

return false; }

public static void main(String args[]){ Scanner in = new Scanner(System.in); boolean gameOver = false; Prog1 game = new Prog1(); game.initializeBoard(DEFAULT_ROWS, DEFAULT_COLS); while (!gameOver){ game.printCurrentPiece(); game.printBoard(); game.widthOfCurrentPiece(); int col = in.nextInt(); game.placeCurrentPiece(col); //how to get different random pieces?

// Fill in the missing details for the game loop. System.out.println(" "); } System.out.println("Game OVER!"); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!