国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 實現掃雷小游戲實例代碼

2019-12-12 04:13:37
字體:
來源:轉載
供稿:網友

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>

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 睢宁县| 介休市| 建始县| 永登县| 贞丰县| 宁河县| 诸城市| 金川县| 连城县| 吉林市| 湛江市| 扶绥县| 娄烦县| 调兵山市| 余干县| 集贤县| 万全县| 蒙山县| 商城县| 邯郸市| 达日县| 淮安市| 华坪县| 石城县| 张家港市| 仙游县| 清涧县| 贺州市| 天长市| 垫江县| 靖远县| 宜州市| 建湖县| 长寿区| 泰来县| 屏东县| 酉阳| 贵溪市| 浦江县| 英吉沙县| 布尔津县|