纯原生纯手写的安卓扫雷小游戏,运用了简单的bfs原理和recycview局部刷新,代码非常少
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.ActivityOptions;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.function.Function;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
Button time;
public static int width = 10;
private Site[][] data = new Site[width][width];
private int nx[] = {0, 1, 1, 1, 0, -1, -1, -1};
private int ny[] = {-1, -1, 0, 1, 1, 1, 0, -1};
private boolean gameOver = false;
private int dateTime = 0;
private GridAdapter gridAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recyc);
time = findViewById(R.id.time_tv);
initRecyc();
initTIme();
}
private void initTIme() {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
if(gameOver) this.cancel();
dateTime ++;
runOnUiThread(new Runnable() {
@Override
public void run() {
time.setText(dateTime+"");
}
});
}
}, 1000, 1000);
}
private void initRecyc() {
recyclerView.setLayoutManager(new GridLayoutManager(this, width){
@Override
public boolean canScrollVertically() {
return false;
}
});
loadData();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
Function<String, Void> myFun = new Function<String, Void>() {
@Override
public Void apply(String s) {
boolean flag = false;
if (s.equals("over")) {
flag = true;
}
gameOver = true;
new AlertDialog.Builder(MainActivity.this).setTitle("游戏结束")
.setMessage(flag ? "恭喜你通关了!" : "你踩到了雷,本局游戏结束")
.setPositiveButton("重新开始", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
startActivity(new Intent(MainActivity.this, MainActivity.class),
ActivityOptions.makeSceneTransitionAnimation(MainActivity.this).toBundle());
finish();
}
}).setNegativeButton("返回", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
}).setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
for (int i = 0; i < width; i++) {
for (int j = 0; j < width; j++) {
data[i][j].isShow = true;
gridAdapter.notifyDataSetChanged();
}
}
}
}).show();
return null;
}
};
gridAdapter = new GridAdapter(this, data, myFun);
recyclerView.setAdapter(gridAdapter);
}
}
private void loadData() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < width; j++) {
data[i][j] = new Site(i, j, 0, false);
}
}
Random random = new Random();
int x = 0, y = 0;
for (int i = 0; i < width; i++) {
do {
int ran = random.nextInt(width * width);
x = ran % width;
y = ran / width;
} while (data[x][y].cnt == -1);
data[x][y].cnt = -1;
}
for (int i = 0; i < width; i++) {
for (int j = 0; j < width; j++) {
data[i][j].cnt = scanf(i, j); /*周围炸弹*/
}
}
}
private int scanf(int i, int j) {
if(data[i][j].cnt == -1) return -1;
int size = 0;
for (int k = 0; k < nx.length; k++) {
int x = i + nx[k];
int y = j + ny[k];
if (x < 0 || y < 0 || x >= width || y >= width) continue;
if(data[x][y].cnt == -1) size ++;
}
return size;
}
}