?? gameboard.java
字號:
/*
* $Id: GameBoard.java, 2007-10-11, 下午04:10:18, Eric. Exp$
*
* Copyright (c) 2006-2007 Wipro Technologies. All Rights Reserved.
* This software program and documentation are copyrighted by Wipro
* Technologies.
*/
package eric.block;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Display;
/**
* Class/Interface description
*
* @author Eric
* @see Another Class
* @since 0.1
*/
public class GameBoard
{
/**
* The value is used for character storage.
*/
public static int EMPTY = -1;
private int x_columns;
private int y_rows;
private Display display = null;
private int[][] matrix;
private GC gc = null;
private int removedLines;
private int fullRowOneTime;
/**
* @param cols
* @param rows
* @param display
*/
public GameBoard(int cols, int rows, Display display)
{
this.x_columns = cols;
this.y_rows = rows;
this.display = display;
init();
}
/**
* Inital all column's xy inital value -1;
*/
void init()
{
matrix = new int[x_columns][y_rows];
for (int cols = 0; cols < x_columns; cols++)
{
for (int rows = 0; rows < y_rows; rows++)
{
matrix[cols][rows] = EMPTY;
}
}
}
/**
* @param currentBlock
* @return boolean
*/
public boolean canMoveDown(Block currentBlock)
{
if (currentBlock != null)
{
for (int i = 0; i < 4; i++)
{
int y = currentBlock.getY() / 30 + currentBlock.getBlockY()[i];
int x = currentBlock.getX() / 30 + currentBlock.getBlockX()[i];
if (y++ == y_rows - 1)
{
return false;
} else if (x > -1 && x < x_columns && matrix[x][y++] != EMPTY)
{
return false;
}
}
}
return true;
}
/**
* adjust can move block to left
*
* @param block
* @return boolean
*/
public boolean canMoveLeft(Block block)
{
if (block != null)
{
for (int i = 0; i < 4; i++)
{
int y = block.getY() / 30 + block.getBlockY()[i];
int x = block.getX() / 30 + block.getBlockX()[i];
if (x-- == 0)
{
return false;
} else if (y > -1 && matrix[x--][y] != EMPTY)
{
return false;
}
}
}
return true;
}
/**
* Remove full rows
*/
public void removeFullRows(){
for(int y = y_rows - 1; y > 0; y--){
if(isFullRow(y)){
removeRow(y);
removedLines++;
fullRowOneTime++;
y++;
}
}
}
/**
* Remove y row
* @param y int
*/
public void removeRow(int y){
if(y > 0 && y < y_rows){
for(; y > 0; y--){
for(int x = 0; x < x_columns; x++){
matrix[x][y] = matrix[x][y - 1];
}
}
}
for(int x = 0; x < x_columns; x++){
matrix[x][0] = EMPTY;
}
}
/**
* adjust can move block to right
*
* @param block
* @return boolean
*/
public boolean canMoveRight(Block block)
{
if (block != null)
{
for (int i = 0; i < 4; i++)
{
int y = block.getY() / 30 + block.getBlockY()[i];
int x = block.getX() / 30 + block.getBlockX()[i];
if (x++ == x_columns - 1)
{
return false;
} else if (y > -1 && matrix[x++][y] != EMPTY)
{
return false;
}
}
}
return true;
}
/**
*
* @param block
* @return boolean
*/
public boolean canRotate(Block block)
{
if (block != null)
{
Block newBlock = (Block) block.clone();
newBlock = newBlock.deasilBlock();
for (int i = 0; i < 4; i++)
{
int y = newBlock.getY() / 30 + newBlock.getBlockY()[i];
int x = newBlock.getX() / 30 + newBlock.getBlockX()[i];
if (y > y_rows - 1 || y < 0 || x < 0 || x > x_columns - 1)
{
return false;
} else if (matrix[x][y] != EMPTY)
{
return false;
}
}
}
return true;
}
/**
* @param msg
* String
*/
public void drawMessage(String msg)
{
gc = MyGC.getMyGC();
final Font font = new Font(display, "Helvetica", 18, SWT.NORMAL);
gc.setFont(font);
gc.setForeground(new Color(display, 255, 255, 255));
int x = (x_columns * 30 - (gc.getFontMetrics().getAverageCharWidth() * msg.length())) / 2;
int y = (y_rows * 30) / 2;
gc.drawString(msg, x, y, true);
font.dispose();
}
/**
* Draw not empty block
*/
public void draw()
{
for (int y = y_rows - 1; y >= 0; y--)
{
for (int x = 0; x < x_columns; x++)
{
if (matrix[x][y] != EMPTY)
{
int xPos = x * 30;
int yPos = y * 30;
new Square(xPos, yPos, matrix[x][y], display).draw();
}
}
}
}
/**
* @return boolean
*/
public boolean hasFullRow(){
fullRowOneTime = 0;
for(int y = y_rows - 1; y >= 0; y--){
if(isFullRow(y)){
return true;
}
}
return false;
}
/**
* @param y
* @return boolean
*/
public boolean isFullRow(int y){
for (int x = 0; x < x_columns; x++) {
if (matrix[x][y] == EMPTY) {
return false;
}
}
return true;
}
/**
* @param x
* @param y
* @return int
*/
public int getPositionValue(int x,int y )
{
return matrix[x][y];
}
/**
* @param x
* @param y
* @param value
*/
public void setPositionValue(int x, int y, int value){
matrix[x][y] = value;
}
/**
* Get remove col's number
*
* @return int
*/
public int getFullRowOneTime(){
return fullRowOneTime;
}
/**
* @return int
*/
public int getRemovedLines(){
return removedLines;
}
/**
*
* Set block's value
* @param block Block
*/
public void addBlock(Block block){
if(block != null ){
for(int i = 0; i < 4; i++){
int x = (block.getX() / 30) + block.getBlockX()[i];
int y = (block.getY() / 30) + block.getBlockY()[i];
int blockY = block.getY() + block.getBlockY()[i] * 30;
if(blockY > -1 && matrix[x][y] == EMPTY)
matrix[x][y] = block.getBlockType();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -