安卓小游戏——扫雷(bfs算法实现)

纯原生纯手写的安卓扫雷小游戏,运用了简单的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;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
安卓 扫雷源码 添加重新开始按钮支持 Android studio ====================================== Risky Project Location: ----------------------- The tools *should* handle project locations in any directory. However, due to bugs, placing projects in directories containing spaces in the path, or characters like ", ' and &, have had issues. We're working to eliminate these bugs, but to save yourself headaches you may want to move your project to a location where this is not a problem. D:\Program Files\android_s_workplace\terrysaolei - Ignored Files: -------------- The following files were *not* copied into the new Gradle project; you should evaluate whether these are still needed in your project and if so manually move them: * ic_launcher-web.png * proguard-project.txt Moved Files: ------------ Android Gradle projects use a different directory structure than ADT Eclipse projects. Here's how the projects were restructured: * AndroidManifest.xml => app\src\main\AndroidManifest.xml * assets\ => app\src\main\assets * res\ => app\src\main\res\ * src\ => app\src\main\java\ Next Steps: ----------- You can now build the project. The Gradle project needs network connectivity to download dependencies. Bugs: ----- If for some reason your project does not build, and you determine that it is due to a bug or limitation of the Eclipse to Gradle importer, please file a bug at http://b.android.com with category Component-Tools. (This import summary is for your information only, and can be deleted after import once you are satisfied with the results.)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值