이제 랭킹도 만들고 싶은데


데이터베이스 유료임 ?







public class TetrisBoard
{
// constants
public static final int EMPTINESS = 0;
public static final int RESULT_FULL_BOARD = -1;
public static final int PIECE_ROWS = 5;
public static final int PIECE_COLUMNS = 5;
public static final int PIECE_DIMENSION = (PIECE_ROWS * PIECE_COLUMNS);

// size of stack
int mStackRows;
int mStackColumns;
int mStackDimension;

// board elements
ArrayListmStack;
ArrayListmPiece;
int mPieceX;
int mPieceY;

// constructors
TetrisBoard(int stackRows, int stackColumns)
{
// create and prepare board elements.
mStack = new ArrayList();
mPiece = new ArrayList();

prepareStack(stackRows, stackColumns);
preparePiece();
}

void prepareStack(int stackRows, int stackColumns)
{
// prevent too small stack
if (stackColumns < 5)
stackColumns = 5;

if (stackRows < 5)
stackRows = 5;

mStackRows = stackRows;
mStackColumns = stackColumns;
mStackDimension = (stackRows * stackColumns);

// prepare stack cells
mStack.clear();
for (int i = 0; i < mStackDimension; i++)
mStack.add(EMPTINESS);
}

void preparePiece()
{
mPiece.clear();
for (int i = 0; i < PIECE_DIMENSION; i++)
mPiece.add(EMPTINESS);

mPieceX = 0;
mPieceY = 0;
}

// public methods
public void initialize()
{
// initialize stack
for (int i = 0; i < mStackDimension; i++)
mStack.set(i, EMPTINESS);

// reset piece
resetPiece();
}

public int update()
{
// Update the board after any change is made.

// Check if the piece has landed on the stack
if (isPieceLanded() == true)
{
// If so, merge the piece into the stack
mergePieceIntoStack();

// Update the stack and get the result of the update.
// RESULT_FULL_BOARD(-1) means full stack (game over),
// 0 means no row has been cleared,
// 1 or more means the number of rows that have been cleared.
int result = updateStack();

// reset the piece.
if (result != RESULT_FULL_BOARD)
resetPiece();

// return the result
return result;
}
return 0;
}

public boolean isFull()
{
// return whether the stack is full or not
return isStackFull();
}

public int getAt(int col, int row)
{
// this method returns the color of the block located at the specified position.
int idx = posToIndexInStack(col, row);

// if there is any stack block at the position, return the color
if (mStack.get(idx) != EMPTINESS)
return mStack.get(idx);

// if there is the piece nearby,
if ((col >= mPieceX) && (col < mPieceX + PIECE_COLUMNS)
&& (row >= mPieceY) && (row < mPieceY + PIECE_ROWS))
{
// get the index of the piece block indicated by the position
int idx_p, col_p, row_p;
col_p = col - mPieceX;
row_p = row - mPieceY;
idx_p = (PIECE_COLUMNS * row_p) + col_p;

// if there is any block at the position, return the color.
if (mPiece.get(idx_p) != EMPTINESS)
return mPiece.get(idx_p);
}
return EMPTINESS;
}

public int getColumns()
{

return mStackColumns;
}

public int getRows()
{

return mStackRows;
}

public int getPieceX()
{

return mPieceX;
}

public int getPieceY()
{

return mPieceY;
}

// inner methods
protected boolean isPieceLanded()
{
int row, col, i_stack;

// Iterate every block in the piece.
for (int i = 0; i < PIECE_DIMENSION; i++)
{
if (mPiece.get(i) != EMPTINESS)
{
// Get the row, column position of the block.
row = rowBlockInPiece(i);
col = colBlockInPiece(i);

// Check if landed on the floor.
if (row == 0)
return true;
// Check if landed on another block in the stack.
else if (row <= mStackRows)
{
// Check if there is any stack-block below the block.
i_stack = posToIndexInStack(col, row - 1);
if (mStack.get(i_stack) != EMPTINESS)
return true;
}
}
}
return false;
}

protected void mergePieceIntoStack()
{
int i_stack;
int row, col;

// Iterate every block in the piece.
for (int i = 0; i < PIECE_DIMENSION; i++)
{
if (mPiece.get(i) != EMPTINESS)
{
// Merge the block into the stack.
col = colBlockInPiece(i);
row = rowBlockInPiece(i);
i_stack = posToIndexInStack(col, row);
if (i_stack < mStackDimension)
{
mStack.set(i_stack, mPiece.get(i));
mPiece.set(i, EMPTINESS);
}
}
}
}

protected int updateStack()
{
int row, n_cleared;

// Check if you have full stack.
if (isStackFull())
{
// If so, return RESULT_FULL_BOARD(-1) as a result.
return RESULT_FULL_BOARD;
}

// Clear every full rows in the stack, and get the number.
n_cleared = 0;
// Iterate every row in the stack.
for (row = 0; !isRowEmpty(row);)
{
// Check if the row is full.
if (isRowFull(row))
{
// If so, clear the row and raise the number.
clearRow(row);
n_cleared++;
}
// If not, check the upper row.
else row++;
}

// return the number of rows that have been cleared.
return n_cleared;
}

protected boolean isStackFull()
{
// Iterate every column in the stack.
for (int col = 0; col < mStackColumns; col++)
{
// Check if the column is full.
if (mStack.get(mStackDimension - 1 - col) != EMPTINESS)
{
return true;
}
}
return false;
}

protected boolean isRowEmpty(int row)
{
int col, idx;

// Iterate every position in the given row.
for (col = 0; col < mStackColumns; col++)
{
// Check if there is any presence of block in the row.
idx = posToIndexInStack(col, row);
if (mStack.get(idx) != EMPTINESS)
return false;
}
return true;
}

protected boolean isRowFull(int row)
{
int col, idx;

// Iterate every position in the given row.
for (col = 0; col < mStackColumns; col++)
{
// Check if there is any absence of block in the row.
idx = posToIndexInStack(col, row);
if (mStack.get(idx) == EMPTINESS)
return false;
}
return true;
}

protected void clearRow(int row)
{
boolean isTopmost;
int col, idx;

// Check if the row is the topmost one.
if (row == mStackRows - 1)
isTopmost = true;
else
isTopmost = isRowEmpty(row + 1);

// If the row is the topmost one,
if (isTopmost)
{
// Remove the given row.
for (col = 0; col < mStackColumns; col++)
{
idx = posToIndexInStack(col, row);
mStack.set(idx, EMPTINESS);
}
}
// If the row is not the topmost one,
else
{
// Remove every blocks at the row.
for (int i = 0; i < mStackColumns; i++)
{
idx = posToIndexInStack(0, row);
mStack.remove(idx);
}

// Since the number of rows has decreased, make new one at the top.
for (col = 0; col < mStackColumns; col++)
mStack.add(EMPTINESS);
}
}

protected void resetPiece()
{
// Reset the piece with random shape type
int shapeType = (int)(Math.random() * SAMPLE_SIZE);
int colorType = (int)(Math.random() * COLOR_SIZE);

for (int i = 0; i < PIECE_DIMENSION; i++)
{
if (SAMPLES[shapeType][i] != 0)
mPiece.set(i, COLORS[colorType]);
else
mPiece.set(i, EMPTINESS);
}

// Place the piece at the ceiling center.
mPieceX = (int)(mStackColumns/2) - (int)(PIECE_COLUMNS/2);
mPieceY = mStackRows;
}

protected boolean movePiece(int offset_x, int offset_y)
{
// Check whether the moved piece makes an overlapping or not.
if (isValidPiece(mPiece, mPieceX + offset_x, mPieceY + offset_y) == true)
{
// If not, move the piece.
mPieceX += offset_x;
mPieceY += offset_y;
return true;
}
return false;
}

protected boolean rotatePiece(boolean is_clockwise)
{
ArrayList rotated;
int col, row;
int x, y;
int x_rotated, y_rotated;
int col_rotated, row_rotated;
int i_rotated;

// The result of rotation will be kept in the 'rotated' array for examination.
rotated = new ArrayList();
for (int i = 0; i < PIECE_DIMENSION; i++)
rotated.add(EMPTINESS);

// Iterate every block in the piece.
for (int i = 0; i < PIECE_DIMENSION; i++)
{
if (mPiece.get(i) != EMPTINESS)
{
// Get the row, column position of the block, relative to the left-bottom of the piece.
col = (int) (i % PIECE_COLUMNS);
row = (int) (i / PIECE_COLUMNS);
// Get the new position of the block, relative to the center of the piece.
x = col - (int) (PIECE_COLUMNS / 2);
y = row - (int) (PIECE_ROWS / 2);
// Rotated the position around the center of the piece.
if (is_clockwise == true)
{
x_rotated = y;
y_rotated = -x;
}
else
{
x_rotated = -y;
y_rotated = x;
}
// Get the index at which the rotated block has to be placed.
col_rotated = x_rotated + (int) (PIECE_COLUMNS / 2);
row_rotated = y_rotated + (int) (PIECE_ROWS / 2);
i_rotated = (row_rotated * PIECE_COLUMNS) + col_rotated;

// Keep the result in the 'rotated' array.
rotated.set(i_rotated, mPiece.get(i));
}
}

// Check whether the 'rotated' array makes an overlapping or not.
if (isValidPiece(rotated, mPieceX, mPieceY) == true)
{
// If not, apply the result to the piece.
for (int i = 0; i < PIECE_DIMENSION; i++)
mPiece.set(i, rotated.get(i));

return true;
}
return false;
}

protected boolean isValidPiece(ArrayList test, int pos_x, int pos_y)
{
int col, row, i_stack;

/*
test : a piece that will be checked if it makes an overlapping with another block in the stack.
pos_x : column position of the piece.
pos_y : row position of the piece.
*/

// Iterate every block in the test piece.
for (int i = 0; i < PIECE_DIMENSION; i++)
{
if (test.get(i) != EMPTINESS)
{
// Get the row, column position of the block.
col = pos_x + (int) (i % PIECE_COLUMNS);
row = pos_y + (int) (i / PIECE_COLUMNS);
i_stack = posToIndexInStack(col, row);

// Check if it makes an overlapping with the left wall.
if (col < 0)
return false;
// Check if it makes an overlapping with the right wall.
if (col > mStackColumns - 1)
return false;
// Check if it makes an overlapping with the floor.
if (row < 0)
return false;
// Check if it makes an overlapping with another stack-block.
if (i_stack < mStackDimension)
{
if (mStack.get(i_stack) != EMPTINESS)
return false;
}
}
}
return true;
}

protected int colBlockInStack(int idx)
{

return (int) (idx % mStackColumns);
}
protected int rowBlockInStack(int idx)
{

return (int) (idx / mStackColumns);
}
protected int colBlockInPiece(int idx)
{

return (mPieceX + (int) (idx % PIECE_COLUMNS));
}
protected int rowBlockInPiece(int idx)
{

return (mPieceY + (int)(idx / PIECE_COLUMNS));
}
protected int posToIndexInStack(int col, int row)
{

return ((mStackColumns * row) + col);
}

// sample of pieces
final static int SAMPLE_SIZE = 5;
final static int[][] SAMPLES =
{
{
0, 0, 0, 0, 0,
0, 1, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 0, 0
},
{
0, 0, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0
},
{
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 1, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 0, 0
},
{
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 1, 0, 0,
0, 0, 0, 0, 0
},
{
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 1, 1, 0, 0,
0, 1, 1, 0, 0,
0, 0, 0, 0, 0
},
};
final static int COLOR_SIZE = 4;
final static int COLORS[] =
{
0xff6897bb, 0xffbada55, 0xffffd700, 0xffff4040
};
// end of class
}