Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

If you need guidance, you will find some detailed instruction (below) to assist you. This is not the only approach that will work but it will present one possibility. The sample below uses Classes (a little Object oriented programming) but, if you prefer a different approach, you do not have to use classes in your project.

To represent the Tetris shapes, you can create a Class called "TetrisShape". In the class, declare a 2-D array of chars that is 4x4 in size (you could also use 4x2 but that would require different algorithm). Call the array "shapeArray". For convenience, declare it as public (but, it should not be your practice when you work in the software industry). You might need more dimensions for the array if you want to statically assign (hard code) all the shape rotations (3-d) for all the shapes (4-d). In this example, the rotation of the shapes is done by value swapping in the 4x4 array.

Now, populate or initialize the "shapeArray". There are three (3) options for doing this:

Option 1:Add a function in the class (call it "populateShapeArray(int shapeType)"), that will take an integer which will denote the type of shape.

Option 2:Use a non-default constructor. In this case, when you create an object, you will take an integer as the constructor argument, which will denote the type of shape, as shown below.

TetrisShape currentShape = new TetrisShape(shapeType);// shapeType is an int

Option 3: Use a setter "setShape(int shapeType)" function.

Regardless of which option you chose above, you would use a switch statement to assign the 4x4 array values for the specific shape. For example, for 'L', it can be something like the following:
shapeArray[0][0] = ' '; shapeArray[1][0] = 'X'; shapeArray[2][0] = ' '; shapeArray[3][0] = ' ';
shapeArray[0][1] = ' '; shapeArray[1][1] = 'X'; shapeArray[2][1] = ' '; shapeArray[3][1] = ' ';
shapeArray[0][2] = ' '; shapeArray[1][2] = 'X'; shapeArray[2][2] = 'X'; shapeArray[3][2] = ' ';
shapeArray[0][3] = ' '; shapeArray[1][3] = ' '; shapeArray[2][3] = ' '; shapeArray[3][3] = ' ';

Always keep record of the top left corner location (x,y dimensions) of the shape that is falling. This record will indicate where to draw the shape in the bucket after the shape moves. Initially, the location should be, (6, 0), which is the top middle of the bucket. Store the coordinates in the "TetrisShape" class asshapeTopLeftXandshapeTopLeftY. Note, x changes horizontally, and y changes vertically.
In this step you will generate one shape. To accomplish this, in themain()function, before the while loop (game loop), do the following:
Generate a random number from 0 to 6.

Use one of the options presented in step 2 above to create a TetrisShape object.

Create a global function called "updatebucket(TetrisShape localTetrisShape)" to populate the bucket with values of the "shapeArray". Here, you will use two nested for loops to go over theshapeArrayof 4x4. You need to provide theTetrisShapeobject (created in step '4b' above) to the function as an argument in order to access itsshapeArray. Inside the loop, you will do this.
bucket[i+shapeTopLeftX][j+shapeTopLeftY] = localTetrisShape.shapeArray[i][j];
You are actually imposing the shape values on to the bucket.
Call the"updatebucket(TetrisShape localTetrisShape)" function frommain()using the object created in step 4b.
Display the bucket.

Inside the while loop (game loop), as the shape falls, the value of y will increase. In order to accomplish this you will have "newShapeTopLeftY = shapeTopLeftY + 1;" so that the shape falls one cell below. Notice that you are storing the new y value in another variable because you need both the old and the new values. Clear (assign spaces to) the current position values of the shape in the bucket using the "old" shapeTopLeftXandshapeTopLeftY. This will ensure that you do not leave any trail of the shape. Call the "updatebucket(TetrisShape localTetrisShape)" function with the "new"shapeTopLeftXandshapeTopLeftYvalues. This will ensure the shape will be displayed in the new location (newShapeTopLeftX,newShapeTopLeftY). Display the bucket. The shape is displayed in the new location of the bucket. Store the newshapeTopLeftXandshapeTopLeftYvalues to the oldshapeTopLeftXandshapeTopLeftY values to get ready for the next loop. Sleep for a while to slow down the game.

Java, Programming

  • Category:- Java
  • Reference No.:- M92400209
  • Price:- $10

Priced at Now at $10, Verified Solution

Have any Question?


Related Questions in Java

Part a specification - robot simulationpart a

PART A Specification - Robot Simulation PART A Requirements To complete this assignment you will use the supplied eclipse project Robot P1/. It is already set up to execute a simple arm movement loop which you will build ...

Answer the following question whats the difference public

Answer the following Question : What's the difference public inheritance and private inheritance? What can derived classes inherit from base classes? What cannot be inherited from base classes?

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Operating systems assignment -problem 1 sharing the bridgea

Operating Systems Assignment - Problem 1: Sharing the Bridge A new single lane bridge is constructed to connect the North Island of New Zealand to the South Island of New Zealand. Farmers from each island use the bridge ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment - java program using array of objectsobjectives

Assignment - JAVA Program using array of objects Objectives - This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details - For this assignment, you are required to develop a Menu ...

Assessment instructionsin this assessment you will complete

Assessment Instructions In this assessment, you will complete the programming of two Java class methods in a console application that registers students for courses in a term of study. The application is written using th ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As