package com.VertexVerveInc.Games;
import java.util.Random;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableRow.LayoutParams;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
public class MinesweeperGame extends Activity
{
private TextView txtMineCount;
private TextView txtTimer;
private ImageButton btnSmile;
private TableLayout mineField; // table layout to add mines to
private Block blocks[][]; // blocks for mine field
private int blockDimension = 24; // width of each block
private int blockPadding = 2; // padding between blocks
private int numberOfRowsInMineField = 9;
private int numberOfColumnsInMineField = 9;
private int totalNumberOfMines = 10;
// timer to keep track of time elapsed
private Handler timer = new Handler();
private int secondsPassed = 0;
private boolean isTimerStarted; // check if timer already started or not
private boolean areMinesSet; // check if mines are planted in blocks
private boolean isGameOver;
private int minesToFind; // number of mines yet to be discovered
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtMineCount = (TextView) findViewById(R.id.MineCount);
txtTimer = (TextView) findViewById(R.id.Timer);
// set font style for timer and mine count to LCD style
Typeface lcdFont = Typeface.createFromAsset(getAssets(),
"fonts/lcd2mono.ttf");
txtMineCount.setTypeface(lcdFont);
txtTimer.setTypeface(lcdFont);
btnSmile = (ImageButton) findViewById(R.id.Smiley);
btnSmile.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
endExistingGame();
startNewGame();
}
});
mineField = (TableLayout)findViewById(R.id.MineField);
showDialog("Click smiley to start New Game", 2000, true, false);
}
private void startNewGame()
{
// plant mines and do rest of the calculations
createMineField();
// display all blocks in UI
showMineField();
minesToFind = totalNumberOfMines;
isGameOver = false;
secondsPassed = 0;
}
private void showMineField()
{
// remember we will not show 0th and last Row and Columns
// they are used for calculation purposes only
for (int row = 1; row < numberOfRowsInMineField + 1; row++)
{
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams((blockDimension + 2 * blockPadding) * numberOfColumnsInMineField, blockDimension + 2 * blockPadding));
for (int column = 1; column < numberOfColumnsInMineField + 1; column++)
{
blocks[row][column].setLayoutParams(new LayoutParams(
blockDimension + 2 * blockPadding,
blockDimension + 2 * blockPadding));
blocks[row][column].setPadding(blockPadding, blockPadding, blockPadding, blockPadding);
tableRow.addView(blocks[row][column]);
}
mineField.addView(tableRow,new TableLayout.LayoutParams(
(blockDimension + 2 * blockPadding) * numberOfColumnsInMineField, blockDimension + 2 * blockPadding));
}
}
private void endExistingGame()
{
stopTimer(); // stop if timer is running
txtTimer.setText("000"); // revert all text
txtMineCount.setText("000"); // revert mines count
btnSmile.setBackgroundResource(R.drawable.smile);
// remove all rows from mineField TableLayout
mineField.removeAllViews();
// set all variables to support end of game
isTimerStarted = false;
areMinesSet = false;
isGameOver = false;
minesToFind = 0;
}
private void createMineField()
{
// we take one row extra row for each side
// overall two extra rows and two extra columns
// first and last row/column are used for calculations purposes only
// x|xxxxxxxxxxxxxx|x
// ------------------
// x| |x
// x| |x
// ------------------
// x|xxxxxxxxxxxxxx|x
// the row and columns marked as x are just used to keep counts of near by mines
blocks = new Block[numberOfRowsInMineField + 2][numberOfColumnsInMineField + 2];
for (int row = 0; row < numberOfRowsInMineField + 2; row++)
{
for (int column = 0; column < numberOfColumnsInMineField + 2; column++)
{
blocks[row][column] = new Block(this);
blocks[row][column].setDefaults();
// pass current row and column number as final int's to event listeners
// this way we can ensure that each event listener is associated to
// particular instance of block only
final int currentRow = row;
final int currentColumn = column;
// add Click Listener
// this is treated as Left Mouse click
blocks[row][column].setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
// start timer on first click
if (!isTimerStarted)
{
startTimer();
isTimerStarted = true;
}
// set mines on first click
if (!areMinesSet)
{
areMinesSet = true;
setMines(currentRow, currentColumn);
}
// this is not first click
// check if current block is flagged
// if flagged the don't do anything
// as that operation is handled by LongClick
// if block is not flagged then uncover nearby blocks
// till we get numbered mines
if (!blocks[currentRow][currentColumn].isFlagged())
{
// open nearby blocks till we get numbered blocks
rippleUncover(currentRow, currentColumn);
// did we clicked a mine
if (blocks[currentRow][currentColumn].hasMine())
{
// Oops, game over
finishGame(currentRow,currentColumn);
}
// check if we win the game
if (checkGameWin())
{
// mark game as win
winGame();
}
}
}
});
// add Long Click listener
// this is treated as right mouse click listener
blocks[row][column].setOnLongClickListener(new OnLongClickListener()
{
public boolean onLongClick(View view)
{
// simulate a left-right (middle) click
// if it is a long click on an opened mine then
// open all surrounding blocks
if (!blocks[currentRow][currentColumn].isCovered() && (blocks[currentRow][currentColumn].getNumberOfMinesInSorrounding() > 0) && !isGameOver)
{
int nearbyFlaggedBlocks = 0;
for (int previousRow = -1; previousRow < 2; previousRow++)
{
for (int previousColumn = -1; previousColumn < 2; previousColumn++)
{
if (blocks[currentRow + previousRow][currentColumn + previousColumn].isFlagged())
{
nearbyFlaggedBlocks++;
}
}
}
// if flagged block count is equal to nearby mine count
// then open nearby blocks
if (nearbyFlaggedBlocks == blocks[currentRow][currentColumn].getNumberOfMinesInSorrounding())
{
for (int previousRow = -1; previousRow < 2; previousRow++)
{
for (int previousColumn = -1; previousColumn < 2; previousColumn++)
{
// don't open flagged blocks
if (!blocks[currentRow + previousRow][currentColumn + previousColumn].isFlagged())
{
// open blocks till we get numbered block
rippleUncover(currentRow + previousRow, currentColumn + previousColumn);
// did we clicked a mine
if (blocks[currentRow + previousRow][currentColumn + previousColumn].hasMine())
{
// oops game over
finishGame(currentRow + previousRow, currentColumn + previousColumn);
}
// did we win the game
if
- 1
- 2
- 3
- 4
- 5
- 6
前往页