Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java application called NewTetris that allows a new way to play Tetris.

ID: 3532852 • Letter: W

Question

Write a Java application called NewTetris that allows a new way to play Tetris. It is well known that in Tetris there are seven kinds of tetriminos, each of which is made up of four minos.The application window is divided into three bands: top, middle and bottom. When the application starts, seven different tetriminos will appear within the top band, as shown in the Java applet below. These seven tetrinimos are referred to as model tetriminos. A user can add tetriminos into the middle band tocreate a meaningful shape. A tetrimino that is no longer useful can be deleted byputting it into the bottom band.

Specifically, a user can perform the following four operations to create a meaningful shape in the middle band.

Add: to add a tetrimino, a user can use the left mouse button to drag the corresponding model tetrimino located within the top band. When a model tetrimino is selected to be dragged, a duplicate is made out of the model. It is the duplicate that is actually dragged. The model tetrimino stays at the same location.

Move: To move a duplicate tetrimino, use the left mouse button to drag it. The model tetrimino can't be moved.

Rotate: To rotate a duplicate tetrimino, use the right mouse button to click one of its four minos. Please note the tetrimino will rotate about the center of the mino that is clicked. The model tetrimino can't be rotated.

Delete: at the end of the moving or rotating operation, if the tetrimino intersects with the bottom band, the tetrimino will be deleted. Since a model tetrimino can't be moved or rotated, it can't be deleted.


Requirements/Recommendations:

You shall write a class named CMino to describe a mino.

You shall write a class named CTetriMino to describe seven different tetriminos.

You can use collections to keep a record of all the tetriminos added so far.

Use event handlers to add, move, rotate and delete a tetrimino.

After rotating a point (x1, y1) about another point (x0, y0) counterclockwise through an angle of theta in radians, its new location (x2, y2) can be represented as follows.

x2=x0+(x1-x0)*cos(theta)-(y1-y0)*sin(theta)

y2=y0+(x1-x0)*sin(theta)+(y1-y0)*cos(theta)


Next:

Extend the NewTetris application to be a two-user network application that allows two users on two different machines to play the NewTetris game together. Both users have their own application windows on their own machines. Whatever one user has performed an operation within his/her window will be immediately reflected on the window of the other user too. Therefore both windows will show exactly the same tetrimino collection.

Difficulty: this is a typical distributed computing project. The difficulty is how to handle the conflicting operations simultaneously performed by the two users. For example, what to do when two users want to drag the same tetrimino at the same time? At the minimum, you can assume that the users will make sure no conflicting operations will be performed by themselves. If you choose to implement an advanced solution, please describe your idea in the email message body.

Explanation / Answer

import java.awt.*; import java.awt.event.*; class Tetris extends Frame { public static void main(String [] args) { Tetris world = new Tetris(); world.show(); } // data fields public static final int FrameWidth = 300; public static final int FrameHeight = 400; private PieceMover player; public Tetris () { setSize (FrameWidth, FrameHeight); setTitle("Tetris"); addKeyListener (new keyDown()); //requestFocus(); player = new PieceMover(this); player.start(); } // interface point between two threads private int currentCommand = Piece.Down; public synchronized int getCommand(int nextCommand) { int oldCommand = currentCommand; currentCommand = nextCommand; return oldCommand; } private class keyDown extends KeyAdapter { public void keyPressed (KeyEvent e) { char key = e.getKeyChar(); switch (key) { case 'g': player.newGame(); break; case 'j': getCommand(Piece.Left); break; case 'k': getCommand(Piece.Rotate); break; case 'l': getCommand(Piece.Right); break; case 'q': System.exit(0); } } } public void update (Graphics g) { Image buffer = createImage (FrameWidth, FrameHeight); Graphics sg = buffer.getGraphics(); sg.clearRect(0, 0, FrameWidth, FrameHeight); player.paint(sg); g.drawImage (buffer, 0, 0, this); } } class PieceMover extends Thread { // private data fields private Tetris controller; private Color table[][]; private Piece currentPiece; private int score = 0; // constructor public PieceMover (Tetris t) { controller = t; table = new Color[15][30]; newGame(); } // thread starting point public void run () { while (dropPiece()) { } } // other methods public void newGame () { for (int i = 0; i < 30; i++) for (int j = 0; j < 15; j++) table[j][i] = Color.white; } public void paint (Graphics g) { for (int i = 0; i < 30; i++) for (int j = 0; j < 15; j++) { g.setColor(table[j][i]); g.fillRect(20+10*j, 350-10*i, 10, 10); } g.setColor(Color.blue); g.drawString("Score " + score, 200, 200); } private boolean dropPiece () { int piecetype = 1 + (int) (7 * Math.random()); currentPiece = new Piece(piecetype); if (! currentPiece.move(Piece.Down, table)) return false; int command = controller.getCommand(Piece.Down); while (currentPiece.move(command, table)) { controller.repaint(); yield(); try { sleep(100); } catch(InterruptedException e) { } command = controller.getCommand(Piece.Down); } // piece cannot move, check score checkScore(); return true; } private void checkScore() { for (int i = 0; i < 30; i++) { boolean scored = true; for (int j = 0; j < 15; j++) if (table[j][i] == Color.white) scored = false; if (scored) { score += 10; moveDown(i); i = i - 1; // check row again } } } private void moveDown (int start) { for (int i = start; i < 30; i++) for (int j = 0; j < 15; j++) if (i < 29) table[j][i] = table[j][i+1]; else table[j][i] = Color.white; } }