android扫雷源码
时间: 2023-08-31 09:10:16 浏览: 51
以下是一个基于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表示地雷,其他数字表示周围地雷的数量。它还使用另外两个二维数组来跟踪哪些方格已经被揭示和哪些方格被标记。在点击和长按方格时,它会根据游戏规则揭示或标记方格,并在必要时检查游戏是否结束。