安卓android 扫雷
时间: 2023-10-30 13:39:06 浏览: 148
你想要我推荐一款安卓扫雷游戏吗?以下是几款比较不错的安卓扫雷游戏:
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>
```
注意:该示例代码仅供学习参考,可能存在不足之处,使用时请注意安全。
阅读全文