安卓android 扫雷
时间: 2023-10-30 08:39:06 浏览: 61
你想要我推荐一款安卓扫雷游戏吗?以下是几款比较不错的安卓扫雷游戏:
1. Minesweeper Classic - Puzzle Minesweeper: 这是一款经典的扫雷游戏,界面简洁,操作简单,同时还拥有多种游戏难度供玩家挑战。
2. Minesweeper Go: 这款扫雷游戏拥有多种难度级别和多种主题样式,让玩家可以根据自己的喜好自由选择。
3. MineSweeper Pro: 这是一款专业级别的扫雷游戏,拥有多种游戏模式和挑战任务,让玩家可以不断提升自己的游戏水平。
希望以上推荐能够对你有所帮助!
相关问题
android扫雷代码
以下是一个简单的 Android 扫雷游戏的示例代码,供参考:
MainActivity.java:
```
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final int MINE_NUMBER = 10;
private static final int ROW_NUMBER = 10;
private static final int COL_NUMBER = 10;
private int[][] mGameBoard = new int[ROW_NUMBER][COL_NUMBER];
private Button[][] mButtons = new Button[ROW_NUMBER][COL_NUMBER];
private boolean[][] mIsMine = new boolean[ROW_NUMBER][COL_NUMBER];
private boolean[][] mIsOpened = new boolean[ROW_NUMBER][COL_NUMBER];
private boolean mIsGameOver = false;
private TextView mTvResult;
private GridLayout mGridLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTvResult = findViewById(R.id.tv_result);
mGridLayout = findViewById(R.id.grid_layout);
initGameBoard();
initButtons();
}
@Override
public void onClick(View v) {
if (mIsGameOver) {
return;
}
int row = (int) v.getTag(R.id.tag_row);
int col = (int) v.getTag(R.id.tag_col);
if (mIsOpened[row][col]) {
return;
}
if (mIsMine[row][col]) {
revealAllMines();
mTvResult.setText("You lose!");
mIsGameOver = true;
return;
}
openCell(row, col);
if (checkWin()) {
revealAllMines();
mTvResult.setText("You win!");
mIsGameOver = true;
}
}
private void initGameBoard() {
int cnt = 0;
Random random = new Random();
while (cnt < MINE_NUMBER) {
int row = random.nextInt(ROW_NUMBER);
int col = random.nextInt(COL_NUMBER);
if (!mIsMine[row][col]) {
mIsMine[row][col] = true;
cnt++;
}
}
for (int i = 0; i < ROW_NUMBER; i++) {
for (int j = 0; j < COL_NUMBER; j++) {
if (!mIsMine[i][j]) {
int cntMine = countNeighborMine(i, j);
mGameBoard[i][j] = cntMine;
}
}
}
}
private void initButtons() {
for (int i = 0; i < ROW_NUMBER; i++) {
for (int j = 0; j < COL_NUMBER; j++) {
Button button = new Button(this);
button.setTag(R.id.tag_row, i);
button.setTag(R.id.tag_col, j);
button.setOnClickListener(this);
mButtons[i][j] = button;
mGridLayout.addView(button);
}
}
}
private int countNeighborMine(int row, int col) {
int cnt = 0;
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (i < 0 || i >= ROW_NUMBER || j < 0 || j >= COL_NUMBER) {
continue;
}
if (mIsMine[i][j]) {
cnt++;
}
}
}
return cnt;
}
private void openCell(int row, int col) {
mIsOpened[row][col] = true;
mButtons[row][col].setEnabled(false);
if (mGameBoard[row][col] > 0) {
mButtons[row][col].setText(String.valueOf(mGameBoard[row][col]));
return;
}
for (int i = row - 1; i <= row + 1; i++) {
for (int j = col - 1; j <= col + 1; j++) {
if (i < 0 || i >= ROW_NUMBER || j < 0 || j >= COL_NUMBER) {
continue;
}
if (mIsOpened[i][j]) {
continue;
}
openCell(i, j);
}
}
}
private boolean checkWin() {
for (int i = 0; i < ROW_NUMBER; i++) {
for (int j = 0; j < COL_NUMBER; j++) {
if (!mIsMine[i][j] && !mIsOpened[i][j]) {
return false;
}
}
}
return true;
}
private void revealAllMines() {
for (int i = 0; i < ROW_NUMBER; i++) {
for (int j = 0; j < COL_NUMBER; j++) {
if (mIsMine[i][j]) {
mButtons[i][j].setText("X");
}
}
}
}
}
```
activity_main.xml:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:textAlignment="center"
android:text=""/>
<GridLayout
android:id="@+id/grid_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:columnCount="10"
android:rowCount="10"/>
</LinearLayout>
```
注意:该示例代码仅供学习参考,可能存在不足之处,使用时请注意安全。
android扫雷源码
以下是一个基于Java的Android扫雷游戏的源码示例,你可以参考一下:
```java
public class MainActivity extends AppCompatActivity {
private static final int NUM_ROWS = 10;
private static final int NUM_COLS = 10;
private static final int NUM_MINES = 15;
private Button[][] buttons = new Button[NUM_ROWS][NUM_COLS];
private int[][] counts = new int[NUM_ROWS][NUM_COLS];
private boolean[][] revealed = new boolean[NUM_ROWS][NUM_COLS];
private boolean[][] flagged = new boolean[NUM_ROWS][NUM_COLS];
private int numMines = NUM_MINES;
private boolean gameOver = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
generateMines();
for (int row = 0; row < NUM_ROWS; row++) {
for (int col = 0; col < NUM_COLS; col++) {
Button button = new Button(this);
button.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
button.setBackgroundResource(R.drawable.button_bg);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (gameOver) {
return;
}
Button button = (Button) v;
int row = (int) button.getTag(R.id.row);
int col = (int) button.getTag(R.id.col);
if (flagged[row][col]) {
return;
}
if (counts[row][col] == -1) {
gameOver();
} else {
reveal(row, col);
checkWin();
}
}
});
button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (gameOver) {
return true;
}
Button button = (Button) v;
int row = (int) button.getTag(R.id.row);
int col = (int) button.getTag(R.id.col);
if (!revealed[row][col]) {
flagged[row][col] = !flagged[row][col];
updateButton(button, row, col);
checkWin();
}
return true;
}
});
LinearLayout rowLayout = findViewById(getResources().getIdentifier("row_" + row, "id", getPackageName()));
rowLayout.addView(button);
buttons[row][col] = button;
button.setTag(R.id.row, row);
button.setTag(R.id.col, col);
}
}
}
private void generateMines() {
Random random = new Random();
for (int i = 0; i < NUM_MINES; i++) {
int row, col;
do {
row = random.nextInt(NUM_ROWS);
col = random.nextInt(NUM_COLS);
} while (counts[row][col] == -1);
counts[row][col] = -1;
for (int r = row - 1; r <= row + 1; r++) {
for (int c = col - 1; c <= col + 1; c++) {
if (r >= 0 && r < NUM_ROWS && c >= 0 && c < NUM_COLS && counts[r][c] != -1) {
counts[r][c]++;
}
}
}
}
}
private void reveal(int row, int col) {
if (revealed[row][col]) {
return;
}
revealed[row][col] = true;
Button button = buttons[row][col];
updateButton(button, row, col);
if (counts[row][col] == 0) {
for (int r = row - 1; r <= row + 1; r++) {
for (int c = col - 1; c <= col + 1; c++) {
if (r >= 0 && r < NUM_ROWS && c >= 0 && c < NUM_COLS && (r != row || c != col)) {
reveal(r, c);
}
}
}
}
}
private void updateButton(Button button, int row, int col) {
if (flagged[row][col]) {
button.setText("F");
} else if (revealed[row][col]) {
int count = counts[row][col];
if (count == 0) {
button.setText("");
} else {
button.setText(String.valueOf(count));
}
} else {
button.setText("");
}
}
private void gameOver() {
gameOver = true;
for (int row = 0; row < NUM_ROWS; row++) {
for (int col = 0; col < NUM_COLS; col++) {
if (counts[row][col] == -1) {
revealed[row][col] = true;
updateButton(buttons[row][col], row, col);
}
}
}
Toast.makeText(this, "Game over!", Toast.LENGTH_SHORT).show();
}
private void checkWin() {
int numRevealed = 0;
for (int row = 0; row < NUM_ROWS; row++) {
for (int col = 0; col < NUM_COLS; col++) {
if (revealed[row][col]) {
numRevealed++;
}
}
}
if (numRevealed == NUM_ROWS * NUM_COLS - NUM_MINES) {
Toast.makeText(this, "You win!", Toast.LENGTH_SHORT).show();
}
}
}
```
这个示例代码使用一个二维数组来表示扫雷游戏的方格,其中-1表示地雷,其他数字表示周围地雷的数量。它还使用另外两个二维数组来跟踪哪些方格已经被揭示和哪些方格被标记。在点击和长按方格时,它会根据游戏规则揭示或标记方格,并在必要时检查游戏是否结束。