AndroID 实现扫雷小游戏实例
最近学习AndroID 应用编程,抽空做个小应用,大家熟悉的扫雷应用,练手用,
以下是实现代码:
MainActivity 类
public class MainActivity extends Activity implements OnClickListener,OnLongClickListener { // 最外层布局 linearLayout textvIEws; linearLayout buttons; int[][] map = new int[10][10]; // 用来隐藏所有button List<button> buttonList = new ArrayList<button>(); // -1 @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); textvIEws = (linearLayout) findVIEwByID(R.ID.textvIEws); buttons = (linearLayout) findVIEwByID(R.ID.buttons); initData(); initVIEw(); } Set<Integer> random地雷; private voID initData() { // 10个地雷 显示* 数组中是-1 // 90个 雷的边上是数字,其他是空白 0 1-8 // 100个数字 从里面随机取走10个 // 初始化 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { map[i][j] = 0; } } // 抽取100个数 99 random地雷 = getRandom(); // 丢入map for (Integer integer : random地雷) { int hang = integer / 10;// 98 int lIE = integer % 10; // 所有的地雷用-1代替 map[hang][lIE] = -1; } // 为所有的空白地点去设置数值 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (map[i][j] == -1) continue; // 继续下次循环 int sum = 0; // 左上角 if (i != 0 && j != 0) {// 防止下标越界 if (map[i - 1][j - 1] == -1) sum++; } // 上面 if (j != 0) { if (map[i][j - 1] == -1) sum++; } // 右上角 if (j != 0 && i != 9) { if (map[i + 1][j - 1] == -1) sum++; } // 左边 if (i != 0) { if (map[i - 1][j] == -1) sum++; } // 右边 if (i != 9) { if (map[i + 1][j] == -1) sum++; } // 左下角 if (j != 9 && i != 0) { if (map[i - 1][j + 1] == -1) sum++; } if (j != 9) { if (map[i][j + 1] == -1) sum++; } if (j != 9 && i != 9) { if (map[i + 1][j + 1] == -1) sum++; } map[i][j] = sum; } } // 打印看看 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { System.out.print(map[i][j] + " "); } System.out.println(); } } private Set<Integer> getRandom() { // 没有重复的 Set<Integer> set = new HashSet<Integer>(); while (set.size() != 10) { int random = (int) (Math.random() * 100); set.add(random); } return set; } // 创建视图 private voID initVIEw() { int wIDth = getResources().getdisplayMetrics().wIDthPixels / 10; linearLayout.LayoutParams params = new linearLayout.LayoutParams(wIDth,wIDth); for (int i = 0; i < 10; i++) { // 每一条的布局 linearLayout tvs = new linearLayout(this); tvs.setorIEntation(linearLayout.HORIZONTAL); linearLayout btns = new linearLayout(this); btns.setorIEntation(linearLayout.HORIZONTAL); // 添加内层的100个按钮和文本 for (int j = 0; j < 10; j++) { // 底层的TextVIEw TextVIEw tv = new TextVIEw(this); tv.setBackgroundResource(R.drawable.textvIEw_bg); tv.setLayoutParams(params); tv.setGravity(Gravity.CENTER); if (map[i][j] == -1) tv.setText("*"); else if (map[i][j] != 0) tv.setText(map[i][j] + ""); tvs.addVIEw(tv); // 底层的button button btn = new button(this); btn.setBackgroundResource(R.drawable.button); btn.setLayoutParams(params); btn.setTag(i * 10 + j); btn.setonClickListener(this); btn.setonLongClickListener(this); buttonList.add(btn); btns.addVIEw(btn); } textvIEws.addVIEw(tvs); buttons.addVIEw(btns); } } @OverrIDe public voID onClick(VIEw v) { int ID = (Integer) v.getTag(); int hang = ID / 10; int lIE = ID % 10; // 隐藏按钮,显示底下的数据 v.setVisibility(VIEw.INVISIBLE); isOver(hang,lIE); // 判断点击的是否是一个数字 if (map[hang][lIE] == 0) { // 开始递归 显示周围所有的空白(hang,lIE); } if (isWin()) { Toast.makeText(this,"游戏胜利",Toast.LENGTH_SHORT).show(); } } // 显示周围所有的button public voID 显示周围所有的空白(int i,int j) { // 检测周围的元素,如果为0 继续调用自身,并且显示 // 不是,就显示button // 从左上角开始 // 左上角 // 先显示自己 buttonList.get(i * 10 + j).setVisibility(VIEw.INVISIBLE); if (i != 0 && j != 0) {// 防止下标越界 if (map[i - 1][j - 1] == 0) { if (判断是否需要递归(i - 1,j - 1)) 显示周围所有的空白(i - 1,j - 1); } else { 隐藏button(i - 1,j - 1); } } // 上面 if (j != 0) { if (map[i][j - 1] == 0) { if (判断是否需要递归(i,j - 1)) 显示周围所有的空白(i,j - 1); } else { 隐藏button(i,j - 1); } } // 右上角 if (j != 0 && i != 9) { if (map[i + 1][j - 1] == 0) { if (判断是否需要递归(i + 1,j - 1)) 显示周围所有的空白(i + 1,j - 1); } else { 隐藏button(i + 1,j - 1); } } // 左边 if (i != 0) { if (map[i - 1][j] == 0) { if (判断是否需要递归(i - 1,j)) 显示周围所有的空白(i - 1,j); } else { 隐藏button(i - 1,j); } } // 右边 if (i != 9) { if (map[i + 1][j] == 0) { if (判断是否需要递归(i + 1,j)) 显示周围所有的空白(i + 1,j); } else { 隐藏button(i + 1,j); } } // 左下角 if (j != 9 && i != 0) { if (map[i - 1][j + 1] == 0) { if (判断是否需要递归(i - 1,j + 1)) 显示周围所有的空白(i - 1,j + 1); } else { 隐藏button(i - 1,j + 1); } } if (j != 9) { if (map[i][j + 1] == 0) { if (判断是否需要递归(i,j + 1)) 显示周围所有的空白(i,j + 1); } else { 隐藏button(i,j + 1); } } if (j != 9 && i != 9) { if (map[i + 1][j + 1] == 0) { if (判断是否需要递归(i + 1,j + 1)) 显示周围所有的空白(i + 1,j + 1); } else { 隐藏button(i + 1,j + 1); } } } private voID 隐藏button(int i,int j) { int 位置 = i * 10 + j; buttonList.get(位置).setVisibility(VIEw.INVISIBLE); } boolean 判断是否需要递归(int hang,int lIE) { // 判断是否是现实的 int 位置 = hang * 10 + lIE; if (buttonList.get(位置).getVisibility() == VIEw.INVISIBLE) return false; else return true; } private boolean isOver(int hang,int lIE) { // OVER if (map[hang][lIE] == -1) { Toast.makeText(this,"GameOver",Toast.LENGTH_SHORT).show(); for (int i = 0; i < buttonList.size(); i++) { buttonList.get(i).setVisibility(VIEw.INVISIBLE); } return true; } return false; } // 最多10个旗子 List<Integer> 旗子 = new ArrayList<Integer>(); @OverrIDe public boolean onLongClick(VIEw v) { // 插旗子 // 1. 有了旗子 就取消 // 2. 没有就插旗 button btn = (button) v; int tag = (Integer) v.getTag(); if (旗子.contains(tag)) { // 如果使用drawabletop 对应的java代码的方法 // setCompoundDrawablesWithIntrinsicBounds btn.setText(""); // int ArrayList.remove(int);//下标 旗子.remove((Integer) tag); } else { // 没有插旗就需要插旗,判断数量是否超过了上限 if (旗子.size() != 10) { 旗子.add(tag); btn.setText("∉ " + 旗子.size()); btn.setTextcolor(color.RED); } else { Toast.makeText(this,"没有旗子了",Toast.LENGTH_SHORT).show(); } } // 消耗事件, return true; } // 是否胜利 public boolean isWin() { int sum = 0; for (int i = 0; i < buttonList.size(); i++) { if (buttonList.get(i).getVisibility() == VIEw.INVISIBLE) sum++; } if (sum == 90) return true; return false; }}
xml
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" androID:gravity="center" androID:orIEntation="vertical" > <FrameLayout androID:ID="@+ID/framelayout" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" > <linearLayout androID:ID="@+ID/textvIEws" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical" /> <linearLayout androID:ID="@+ID/buttons" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical" > </linearLayout> </FrameLayout></linearLayout>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android 实现扫雷小游戏实例代码全部内容,希望文章能够帮你解决Android 实现扫雷小游戏实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)