/** * 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 : TetrisGame.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. // //======================================================================= /* * @author HÃ¥vard Wigtil * @version $Revision$, $Date$ */ package org.pegadi.games.tetris; import java.util.Random; public class TetrisGame { public static final int MAXBRICK = 4; //max # of brick in a block public static final int BRICKSIZE= 16; //brick image size public static final int BRICKTYPE= 7; //max # of types of brick public static final int MAXLEVEL = 10; //max # of level public static final int BLOCKWNDW= 10; public static final int BLOCKWNDH= 20; private boolean rotateLeft; //decides what way the falling block will rotate private int mWndMap[][] = new int[BLOCKWNDH][BLOCKWNDW]; private Random mRandomizer = null ; private FallingBlock mNewBlock = null; private FallingBlock mCurrentBlock = null; private int mLevel; private int mScore = 0; private int mLines = 0; public TetrisGame(int level, boolean rotateLeft) { mLevel = level; mRandomizer = new Random(); for ( int i=0; i< BLOCKWNDH; i++) for ( int j=0; j< BLOCKWNDW; j++) mWndMap[i][j] = -1; this.rotateLeft = rotateLeft; createNewBlock(); } public FallingBlock getCurrentBlock() { return mCurrentBlock; } public FallingBlock getNextBlock() { return mNewBlock; } public int getWndMap(int y, int x) { return mWndMap[y][x]; } public int getLevel() { return mLevel ; } /** * Increases the current level by one. */ public void increaseLevel() { mLevel++; } /** * Increases the current linecount. * * @param lines Count to increase. */ public void increaseLines(int lines) { mLines += lines; if (mLevel > MAXLEVEL && (mLines / 10) >= mLevel) { increaseLevel(); } } /** * Returns the current linecount. */ public int getLines() { return mLines; } public int getScore() { return mScore ; } /** * Increases the score, and also increases the level if the linecount is * high enough. * * @param score Number to add to the score. */ public void increaseScore(int score ) { mScore += score; } /** * Insert the current block into the map. */ public void modifyMap() { int fbkX, fbkY, fbkW, fbkH, type; fbkX = mCurrentBlock.getX(); fbkY = mCurrentBlock.getY(); fbkW = mCurrentBlock.getW(); fbkH = mCurrentBlock.getH(); type = mCurrentBlock.getType(); for ( int y = 0; y<fbkH; y++ ) for ( int x = 0; x<fbkW; x++ ) if ( mCurrentBlock.getMapEntry( y, x ) == 1 ) mWndMap[fbkY+y][fbkX+x] = type; } /** * Check line1 to line2, clear lines if filled up. */ public int checkLine( int line1, int line2 ) { int y, x, y1, x1; int linecount = 0; for ( y = line1; y <= line2; y++ ) { for ( x = 0; (x < BLOCKWNDW) && (mWndMap[y][x] >= 0); x++ ) ; //--- clear line y ------- if ( x == BLOCKWNDW ) { linecount++; // move down one whole line for ( y1 = y-1; y1 >=0 ; y1-- ) for ( x1 = 0; x1 < BLOCKWNDW; x1++ ) mWndMap[y1+1][x1] = mWndMap[y1][x1]; // fill line0 with blank for ( x1 = 0; x1 < BLOCKWNDW; x1++ ) mWndMap[0][x1] = -1; } //if } return linecount; } public FallingBlock createNewBlock() { mCurrentBlock = mNewBlock; //new block created for preview float j = mRandomizer.nextFloat(); int i = (int)(j * 7.0 ) ; mNewBlock = new FallingBlock(this, i, rotateLeft) ; return ( mCurrentBlock ); } }