Android 實(shí)現(xiàn)掃雷小游戲?qū)嵗a
Android 實(shí)現(xiàn)掃雷小游戲?qū)嵗?/strong>
最近學(xué)習(xí)Android 應(yīng)用編程,抽空做個(gè)小應(yīng)用,大家熟悉的掃雷應(yīng)用,練手用,
以下是實(shí)現(xiàn)代碼:
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個(gè)地雷 顯示* 數(shù)組中是-1
// 90個(gè) 雷的邊上是數(shù)字,其他是空白 0 1-8
// 100個(gè)數(shù)字 從里面隨機(jī)取走10個(gè)
// 初始化
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
map[i][j] = 0;
}
}
// 抽取100個(gè)數(shù) 99
random地雷 = getRandom();
// 丟入map
for (Integer integer : random地雷) {
int hang = integer / 10;// 98
int lie = integer % 10;
// 所有的地雷用-1代替
map[hang][lie] = -1;
}
// 為所有的空白地點(diǎn)去設(shè)置數(shù)值
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (map[i][j] == -1)
continue; // 繼續(xù)下次循環(huán)
int sum = 0;
// 左上角
if (i != 0 && j != 0) {// 防止下標(biāo)越界
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() {
// 沒有重復(fù)的
Set<Integer> set = new HashSet<Integer>();
while (set.size() != 10) {
int random = (int) (Math.random() * 100);
set.add(random);
}
return set;
}
// 創(chuàng)建視圖
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);
// 添加內(nèi)層的100個(gè)按鈕和文本
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;
// 隱藏按鈕,顯示底下的數(shù)據(jù)
v.setVisibility(View.INVISIBLE);
isOver(hang, lie);
// 判斷點(diǎn)擊的是否是一個(gè)數(shù)字
if (map[hang][lie] == 0) {
// 開始遞歸
顯示周圍所有的空白(hang, lie);
}
if (isWin()) {
Toast.makeText(this, "游戲勝利", Toast.LENGTH_SHORT).show();
}
}
// 顯示周圍所有的button
public void 顯示周圍所有的空白(int i, int j) {
// 檢測周圍的元素,如果為0 繼續(xù)調(diào)用自身,并且顯示
// 不是,就顯示button
// 從左上角開始
// 左上角
// 先顯示自己
buttonList.get(i * 10 + j).setVisibility(View.INVISIBLE);
if (i != 0 && j != 0) {// 防止下標(biāo)越界
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) {
// 判斷是否是現(xiàn)實(shí)的
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個(gè)旗子
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 對應(yīng)的java代碼的方法
// setCompoundDrawablesWithIntrinsicBounds
btn.setText("");
// int ArrayList.remove(int);//下標(biāo)
旗子.remove((Integer) tag);
} else {
// 沒有插旗就需要插旗,判斷數(shù)量是否超過了上限
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>
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android Handler之消息循環(huán)的深入解析
本篇文章是對Handler消息循環(huán)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android編程實(shí)現(xiàn)簡單流量管理功能實(shí)例
這篇文章主要介紹了Android編程實(shí)現(xiàn)簡單流量管理功能的方法,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)流量監(jiān)控所涉及的功能模塊與布局技巧,需要的朋友可以參考下2016-02-02
Android在線更新SDK的方法(使用國內(nèi)鏡像)
這篇文章主要介紹了Android在線更新SDK的方法,分別介紹了修改hosts文件使用谷歌官方鏡像更新及使用國內(nèi)鏡像更新SDK的方法,非常簡單實(shí)用,需要的朋友可以參考下2015-12-12
Flutter實(shí)現(xiàn)笑嘻嘻的動態(tài)表情的示例代碼
這篇文章主要為大家介紹了如何利用Flutter實(shí)現(xiàn)笑嘻嘻的動態(tài)表情,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Flutter有一定幫助,感興趣的可以了解一下2022-04-04
Kotlin使用協(xié)程實(shí)現(xiàn)高效并發(fā)程序流程詳解
這篇文章主要介紹了Kotlin使用協(xié)程實(shí)現(xiàn)高效并發(fā)程序流程,協(xié)程屬于Kotlin中非常有特色的一項(xiàng)技術(shù),因?yàn)榇蟛糠志幊陶Z言中是沒有協(xié)程這個(gè)概念的。那么什么是協(xié)程呢?它其實(shí)和線程有點(diǎn)相似,可以簡單地將它理解成一種輕量級的線程2023-01-01
Android scrollTo和scrollBy方法使用解析
在一個(gè)View中,系統(tǒng)提供了scrollTo、scrollBy兩種方式來改變一個(gè)View的位置,下面通過本文給大家介紹Android scrollTo和scrollBy方法使用解析,需要的朋友參考下吧2018-01-01
用Android?studio實(shí)現(xiàn)簡易計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了用Android?studio實(shí)現(xiàn)簡易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05

