import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Tetrominoppp here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Tetrominoppp extends Actor
{ protected static final int NORTH = 0;
protected static final int WEST = 1;
protected static final int SOUTH = 2;
protected static final int EAST = 3;
protected Block[] b;
int direction;
boolean dead;
private int counter;
Tetrominoppp(String color) { setImage("thumb-350-253358.jpeg");
b = new Block[4];
for (int i = 0; i < 4; i++) { b[i] = new Block(color); }
counter = 0;
dead = false;
Backgroundectworld.getWorld().addObject(this, 0,
backgroundectworld.getWorld().getHeight() - 1); }
abstract protected void setDirection();
abstract protected Block leftMost();
abstract protected Block rightMost();
abstract protected boolean turnPossible();
void delete() {
for (int i = 0; i < 4; i++) { getWorld().removeObject(b[i]);
}
dead = true; }
public void act() { backgroundectworld world = backgroundectworld.getWorld(); if (world.getCurrentTetromino() == null) { world.gameOver();
return; } if (dead) return;
boolean keyAction = false; if (Greenfoot.isKeyDown("left") && counter < 4) { if (left()) { counter++; keyAction = true; } }
if (Greenfoot.isKeyDown("right") && counter < 4) { if (right()) { counter++; keyAction = true; } }
if (Greenfoot.isKeyDown("up") && counter < 3) { if (turnLeft()) { counter++; keyAction = true; } }
if (Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown(" ")) { down(); return; }
if (keyAction) { return; }
oneDown(); counter = 0; }
boolean left() { if (leftOccupied()) return false; for (int i = 0; i < 4; i++) { b[i].setLocation(b[i].getX() - 1, b[i].getY()); } return true; }
boolean leftOccupied() { if (leftMost().getX() == 0) { return true; }
backgroundectworld world = backgroundectworld.getWorld();blocks: for (int i = 0; i < 4; i++) { java.until.List list = world.getObjectsAt(b[i].getX() - 1, b[i].getY(), Block.class);
if (list.size() == 0) { continue; }
Actor a = (Actor) list.get(0); for (int j = 0; j < 4; j++) { if (i == j) continue; if (a == b[j]) continue blocks; } return true; } return false; }
Boolean right() { if (rightOccupied()) return false; for (int i = 0; i < 4; i++){ b[i].setLocation(b[i].getX() + 1, b[i].getY()); } return true; }
boolean rightOccupied() { if (rightMost().getX() == backgroundectworld.getWorld().getWidth() - 1) { return true; }
backgroundectworld world = backgroundectworld.getWorld(); blocks: for (int i = 0; i < 4; i++) {
List list = world.getObjectsAt(b[i].getX() + 1, b[i].getY(), Block.class);
if (list.size() == 0) { continue; }
Actor a = (Actor) list.get(0); for (int j = 0; j < 4; j++) { if (i == j) continue; if (a == b[j]) continue blocks; } return true; } return false; }
boolean turnLeft() { if (!turnPossible()) { return false; } int oldDir = direction; direction = (direction + 1) % 4; setDirection(); backgroundectworld world = backgroundectworld.getWorld(); for (int i = o; i < 4; i++) {java.util.List list = world.getObjectsAt(b[i].getX(), b[i].getY(), null);
if (list.size() > 1) { direction = oldDir; setDirection(); return false; } } return false; }
boolean oneDown() { for (int i = 0; i < 4; i++) { if(!blockFree(i)) { checkRows(); die(); return false; } } for (int i = 0; i < 4; i++) { b[i].setLocation(b[i].getX(), b[i].getY() + 1); } return true; }
void down() {while (oneDown()) ; }
boolean blockFree (int index) { backgroundectworld world = backgroundectworld.getWorld(); rows: for (int r = world.getHeight() - 3; r >= 0; r--) {
cols: for (int c = 0; c< world.getWidth(); c++){ java.util.List blocks = world.getObjectsAt(c, r, Block.class);
if (blocks.size() == 0) { continue rows; } } clearRows(r); noOfRows++; landslide(r);
r++; }
if (noOfRows > 0) { world.newPoints(noOfRows); } }
void clearRow(int row) { backgroundectworld = backgroundectworld.getWorld(); for (int c = 0; c < world.getWidth(); c++) { world.removeObjects(world.getObjectAt(c,row, Block.class)); } }
void landslide (int row) { backgroundectworld = backgroundectworld.getWorld(); for (int r = row - 1; r >= 0; r--) { for (int c = 0; c< world.getWidth(); c++) { java.util.List blocks = world.getObjectsAt(c, r, Block.class); if (blocks.size() > 0) { Block block = (Block) blocks.get(0); block.setLocation(block.getX, block.getY() + 1); } } } }
void die() { backgroundectworld = backgroundectworld.getWorld(); world.removeObject(this); dead = true; Tetromino tetro = world.genTetromino(); if (checkEnd(tetro)) { tetro.delete(); world.setCurrentTetromino(null); world.gameOver(); } else { backgroundectworld.getWorld().CurrentTetrominoset(tetro); } }
boolean checkEnd(Tetrominoppp tetro) { backgroundectworld = backgroundectworld.getWorld(); for (int i = 0; i < 4; i++) { java.util.List list = world.getObjectsAt(tetro.b[i].getX(), tetro.b[i].getY(), Block.class); if (list.size() > 1) { return true; } } return false; }
int genDirection() { return (int) (Math.random() * 4); }
int length (int number) { if (number < 10) return 1; return length(number /10) + 1; } }
