/**
* Copyright 1999-2009 The Pegadi Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//=======================================================================
// File : FallingBlock.java
// Software : Tetris
// Author : Steve Fu (steve@intrinsa.com foureyes@aimnet.com)
// Date : 04/22/96
// Version : 1.1 ( JDK 1.0)
//
// Copyright (c) 1996 Steve Fu. All Rights Reserved.
//
// Permission to use, modify, copy, distribute this software for
// NON-COMMERCIAL or COMMERCIAL purposes is hereby granted subject
// to the following exceptions:
// 1. This copyright information must remain as whole or part of
// header of this file or its modified versions.
// 2. Credits must be given to the original author, Steve Fu, for
// any redistribution of the software.
//
// AUTHOR MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
// THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. AUTHOR SHALL NOT BE LIABLE FOR
// ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
// DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
//
//=======================================================================
package org.pegadi.games.tetris;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FallingBlock
{
private TetrisGame mGame = null;
private int mMap[][] = new int[TetrisGame.MAXBRICK][TetrisGame.MAXBRICK];
private int mBlockW;
private int mBlockH;
private int mBlockXPos;
private int mBlockYPos;
private int mBlockType;
private boolean rotateLeft;
private final Logger log = LoggerFactory.getLogger(getClass());
/**
*/
public FallingBlock(TetrisGame theGame, int type, boolean rotateLeft)
{
this.rotateLeft = rotateLeft;
String strBlk;
switch (type) {
case 0:
strBlk = "XX\tXX\t\0" ;
break;
case 1:
strBlk = "XX \t XX\t\0" ;
break;
case 2:
strBlk = " XX\tXX \t\0" ;
break;
case 3:
strBlk = " X \tXXX\t\0" ;
break;
case 4:
strBlk = "XXXX\t\0" ;
break;
case 5:
strBlk = "XXX\tX\t0";
break;
case 6:
strBlk = "XXX\t X\t0";
break;
default:
throw new IllegalArgumentException("The \"type\" parameter must be between 0 and 6.");
}
mGame = theGame;
loadBlockMap(strBlk);
mBlockYPos = -1;
mBlockXPos = TetrisGame.BLOCKWNDW/2 - mBlockW /2 ;
mBlockType = type;
}
/**
*/
public int getMapEntry ( int y, int x)
{
return( mMap[y][x] );
}
public int getType() {
return mBlockType;
}
public int getX()
{
return mBlockXPos;
}
public int getY()
{
return mBlockYPos;
}
public int getW()
{
return mBlockW;
}
public int getH()
{
return mBlockH;
}
public boolean moveBlockDown()
{
if ((mBlockYPos + mBlockH) >= TetrisGame.BLOCKWNDH ) {
return false;
}
else {
for ( int y = 0; y< mBlockH; y++ ) {
for ( int x = 0; x< mBlockW; x++ ) {
if ((y+mBlockYPos+1) >= 0) {
if ( (mMap[y][x] == 1) &&
(mGame.getWndMap(y+mBlockYPos+1, x+mBlockXPos) >= 0) ) {
return false;
}
}
}
} // else
mBlockYPos++;
return true;
}
}
public boolean moveBlockLeft()
{
if ( mBlockXPos == 0 ) {
return false;
}
else {
for ( int x = 0; x < mBlockW; x++ ) {
for ( int y = 0; y< mBlockH; y++ ) {
if ((y+mBlockYPos) >= 0) {
if ( (mMap[y][x] == 1 ) &&
(mGame.getWndMap(y+mBlockYPos, x+mBlockXPos-1) >= 0) )
{
return false;
} //if
}
}
}
mBlockXPos--;
return true;
}
}
public boolean moveBlockRight()
{
if ((mBlockXPos + mBlockW) >= TetrisGame.BLOCKWNDW ) {
return false;
}
else {
for ( int x = mBlockW-1 ; x>=0 ; x-- ) {
for ( int y = 0; y< mBlockH; y++ ) {
if ((y+mBlockYPos) >= 0) {
if ( (mMap[y][x] == 1 ) &&
(mGame.getWndMap(y+mBlockYPos, x+mBlockXPos+1) >= 0) ) {
return false;
} //if
}
}
}
mBlockXPos++;
return true;
}
}
public boolean rotateBlock() {
// temp data elements
int tBlockW = mBlockH;
int tBlockH = mBlockW;
int tBlockXPos = (mBlockW/2 + mBlockXPos) - tBlockW/2 ;
int tBlockYPos = (mBlockH/2 + mBlockYPos) - tBlockH/2;
/*
if (tBlockYPos < 0) {
tBlockYPos = 0;
}
*/
if ((tBlockYPos + tBlockH - 1 >= TetrisGame.BLOCKWNDH ) ||
//(tBlockYPos < 0 ) ||
(tBlockXPos + tBlockW - 1 >= TetrisGame.BLOCKWNDW ) ||
(tBlockXPos < 0 ) ) {
return false;
}
//rotation mapping
int tMap[][] = new int[TetrisGame.MAXBRICK][TetrisGame.MAXBRICK];
int tY;
if(rotateLeft) //left-rotating
for ( int j = 0; j< tBlockH; j++ ) {
for ( int i = 0; i<tBlockW; i++ ) {
tY = tBlockW-1-i;
if (tY < 0) {
log.error("Y is out of range (value: " + tY + ")");
}
else {
tMap[j][tY] = mMap[i][j];
}
}
}
else{//right-rotating
for ( int j = 0; j< tBlockH; j++ ) {
for ( int i = 0; i< tBlockW; i++ ) {
tY = tBlockH-1-j;
if (tY < 0) {
log.error("Y is out of range (value: " + tY + ")");
}
else {
tMap[tY][i] = mMap[i][j];
}
}
}
}
//verify rotation
int y,x;
for ( y = 0; y< tBlockH; y++ ) {
for ( x = 0; x< tBlockW; x++ ) {
if (tBlockYPos >= 0) {
if ((tMap[y][x] == 1) &&
(mGame.getWndMap(y+tBlockYPos, x+tBlockXPos) >= 0) ) {
return false;
}
}
}
}
//successful
for ( y = 0; y< tBlockH; y++ ) {
for ( x = 0; x< tBlockW; x++ ) {
mMap[y][x]=tMap[y][x];
}
}
mBlockW = tBlockW;
mBlockH = tBlockH;
mBlockXPos = tBlockXPos ;
mBlockYPos = tBlockYPos ;
return true;
}
protected void loadBlockMap( String cb)
{
mBlockW = 0;
mBlockH = 0;
int i, j;
for ( i=0; i< TetrisGame.MAXBRICK; i++ )
for ( j=0; j< TetrisGame.MAXBRICK; j++ )
mMap[i][j] = 0 ;
i=0;
j=0;
for ( int k =0; k < cb.length(); k++)
{
switch ( cb.charAt(k) )
{
case '\t':
mBlockH++;
if ( j > mBlockW ) mBlockW = j;
i++;
j=0;
break;
case 'X':
mMap[i][j] = 1;
j++;
break;
case ' ':
mMap[i][j] = 0;
j++;
break;
default :
break;
} // switch
} //for
}
}