From 560d2fa630526d81eb28576b68af3fc354ad71d4 Mon Sep 17 00:00:00 2001 From: hebinhai <15767956765@163.com> Date: Tue, 10 May 2016 13:24:35 +0800 Subject: [PATCH 1/4] =?UTF-8?q?#93=20#94=20#95=20#50=20=E5=AE=9E=E9=AA=8C7?= =?UTF-8?q?/=E5=AE=9E=E9=AA=8C8/=E5=AE=9E=E9=AA=8C9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Com1314080901110/GameActivity.java | 6 +- .../Com1314080901110Activity.java | 4 +- .../com1314080901110/adapter/BoomAdapter.java | 146 ++++++++++++++++ .../entity/GameGroundEntity.java | 161 ++++++++++++++++++ .../com1314080901110/entity/GridEntity.java | 88 ++++++++++ app/src/main/res/layout/activity_game.xml | 36 ++++ app/src/main/res/layout/other.xml | 5 + 7 files changed, 441 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/adapter/BoomAdapter.java create mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/entity/GameGroundEntity.java create mode 100644 app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/entity/GridEntity.java create mode 100644 app/src/main/res/layout/activity_game.xml create mode 100644 app/src/main/res/layout/other.xml diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Com1314080901110/GameActivity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/Com1314080901110/GameActivity.java index 3ad1e3db..161bf921 100644 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/Com1314080901110/GameActivity.java +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/Com1314080901110/GameActivity.java @@ -1,4 +1,4 @@ -package com.saolei.minesweeper; +package edu.hzuapps.androidworks.homeworks.com1314080901110; import android.content.Intent; import android.os.Message; @@ -14,8 +14,8 @@ import android.widget.TextView; import android.widget.Toast; -import com.saolei.minesweeper.adapter.BoomAdapter; -import com.saolei.minesweeper.entity.GridEntity; +import edu.hzuapps.androidworks.homeworks.com1314080901110.adapter.BoomAdapter; +import edu.hzuapps.androidworks.homeworks.com1314080901110.entity.GridEntity; import java.util.Timer; import java.util.TimerTask; diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/Com1314080901110Activity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/Com1314080901110Activity.java index 343cd99b..7133358b 100644 --- a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/Com1314080901110Activity.java +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/Com1314080901110Activity.java @@ -1,4 +1,4 @@ -package com.saolei.minesweeper; +package edu.hzuapps.androidworks.homeworks.com1314080901110; import android.content.DialogInterface; import android.content.Intent; @@ -12,7 +12,7 @@ import android.widget.EditText; import android.widget.Toast; -public class Com1314080901110Activity extends AppCompatActivity { +public class com1314080901110Activity extends AppCompatActivity { private Button toPlay; private Button setDifficulty; public String level="10"; diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/adapter/BoomAdapter.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/adapter/BoomAdapter.java new file mode 100644 index 00000000..a853a880 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/adapter/BoomAdapter.java @@ -0,0 +1,146 @@ +package edu.hzuapps.androidworks.homeworks.com1314080901110.adapter; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AbsListView; +import android.widget.BaseAdapter; +import android.widget.GridView; +import android.widget.ImageView; + +import edu.hzuapps.androidworks.homeworks.com1314080901110.R; +import edu.hzuapps.androidworks.homeworks.com1314080901110.entity.GameGroundEntity; +import edu.hzuapps.androidworks.homeworks.com1314080901110.entity.GridEntity; + +/** + * Created by Administrator on 2016/5/4. + */ +public class BoomAdapter extends BaseAdapter{ + /** + * 方法:用来计算适配器一共要设置多少个内容对象 + * 返回值为对象的个数,也就是reurn level*level + * */ +// 游戏难度 + private int level; + private GridView gv; +// 初始化游戏场地 + private GameGroundEntity gameGround; +// 初始化上下文环境 + private Context context; + /** + * 含参构造方法: + * */ + public BoomAdapter(int level,GridView gv,Context context){ + this.level=level; + this.gv=gv; + this.context=context; + this.gameGround=new GameGroundEntity(level); + } + @Override + public int getCount() { + return level*level; + } + /** + * 方法:获取每个格子对象 + * @param position 格子编号,位置下标 + * @return 格子类型的GameGroundEntity + * */ + @Override + public GridEntity getItem(int position) { +// 调用GameGroundEntity中的getEntity方法获取格子对象 + return gameGround.getEntity(position); + } + /** + * 方法:通过适配器给每个格子对象编号或下标获取id值 + * @return long类型,在java中,byte和short可自动转换为int,int可自动转换为long + * */ + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { +// 判断适配器接收到的,用于显示游戏界面的控件是否为空,如果为空则指定用于显示游戏界面的xml文件 + if(convertView==null){ + convertView= LayoutInflater.from(context).inflate(R.layout.other,null); + } + ((ImageView)convertView).setImageResource(getRes(getItem(position))); + AbsListView.LayoutParams params=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,gv.getWidth()/level); + convertView.setLayoutParams(params); + return convertView; + } + /** + * 方法:设置格子对象的背景图片 + * 不同状态下设置不同的背景图片 + * @param grid :格子对象 + * */ + public int getRes(GridEntity grid){ +// 设置格子对象的背景图片的ID为0 + int resID=0; +// 判断,如果格子对象被标记了且标记正确 + if(grid.isFlag()&&!grid.isFlagWrong()){ + resID=R.drawable.i_flag; + } +// 判断,如果格子对象被标记了但标记不正确 + else if(grid.isFlag()&&grid.isFlagWrong()){ + resID=R.drawable.i14; + } +// 判断,如果格子对象没有被点击,isShow()属性为false + else if(!grid.isShow()){ + resID=R.drawable.i00; + } +// 判断,格子对象是地雷且非自动显示 + else if(grid.isBoom()&&!grid.isAutoShow()){ + resID=R.drawable.i13; + } +// 判断,格子对象是地雷,自动显示 + else if(grid.isBoom()&&grid.isAutoShow()){ + resID=R.drawable.i12; + } +// 判断,格子周围没有地雷,是空白格 + else if(grid.getBoomsCount()==0){ + resID=R.drawable.i09; + } +// 判断,格子中卫有地雷,个数为1-8个 + else if(grid.getBoomsCount()!=0){ +// 动态拼接图片名,格式为图片名称,图片类型,资源所在包名 + resID=context.getResources().getIdentifier("i0"+grid.getBoomsCount(),"drawable",context.getPackageName()); + } + return resID; + } + /** + * 方法:判断游戏胜利 + * */ + public boolean isWin(){ + return gameGround.isWin(); + } + /** + * 方法:在游戏结束时展示所有没被猜中的雷*/ + public void showAllBooms(){ + gameGround.showAllBooms(); +// notify通知、提醒,Data数据,Set设置,让Changed改变 + notifyDataSetChanged(); + } + /** + * 方法:展示无雷区域 + * */ + public void showNotBoomsArea(int position){ + gameGround.showNotBoomArea(position); + notifyDataSetChanged(); + } + /** + * 方法:调用游戏场地实例中的获取格子对象方法 + * */ + public GridEntity getEntity(int position){ + return gameGround.getEntity(position); + } + /** + *方法:检查标记状态的方法 + * */ + public void checkFlag(){ + gameGround.checkFlag(); + notifyDataSetChanged(); + } +} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/entity/GameGroundEntity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/entity/GameGroundEntity.java new file mode 100644 index 00000000..63607ade --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/entity/GameGroundEntity.java @@ -0,0 +1,161 @@ +package edu.hzuapps.androidworks.homeworks.com1314080901110.entity; + +import java.util.Random; + +/** + * Created by Administrator on 2016/5/4. + */ +public class GameGroundEntity { +// 游戏难度 + private int level; +// 每局游戏雷的总数 + private int allBoomsCount; +// 只能存入GridEntity类型对象的二维数组 + private GridEntity [][] allGrid; +// 一个规范化的边界格子对象 + private GridEntity sideGrid=new GridEntity(false,false,true,0); +// 含参构造方法 + public GameGroundEntity(int level){ + this.level=level; + this.allBoomsCount=level; + this.allGrid=new GridEntity[level+2][level+2]; + init(); + } + /** + * 方法:初始化游戏 + * 1.给allGrid数组赋值 + * 2.初始化雷所在的格子位置 + * 3.为每个能见到的格子标注周围八个格子的地雷数量 + * */ + public void init(){ + for(int i=0;i=level*level){ + return; + } + int x=position/level+1; + int y=position%level+1; + if(allGrid[x][y].isSide()){ + return; + } + if(allGrid[x][y].getBoomsCount()!=0||allGrid[x][y].isShow()){ + allGrid[x][y].setIsShow(true); + return; + } + allGrid[x][y].setIsShow(true); + for(int ii=x-1;ii<=x+1;ii++){ + for(int jj=y-1;jj<=y+1;jj++){ + if(ii<=0||jj<=0||ii>=level+1||jj>=level+1){ + continue; + }else{ + showNotBoomArea((ii-1)*level+(jj-1)); + } + } + } + } +} diff --git a/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/entity/GridEntity.java b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/entity/GridEntity.java new file mode 100644 index 00000000..27ef72b3 --- /dev/null +++ b/app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/entity/GridEntity.java @@ -0,0 +1,88 @@ +package edu.hzuapps.androidworks.homeworks.com1314080901110.entity; + +/** + * Created by Administrator on 2016/5/4. + */ +public class GridEntity { +// 定义雷的数量 + private int boomsCount; +// 判断是否为雷 + private boolean isBoom; +// 判断是否被标记 + private boolean isFlag; +// 判断标记是否错误 + private boolean isFlagWrong; +// 格子是否被展示 + private boolean isShow; +// 格子是否自动展示 + private boolean isAutoShow; +// 判断是否为边界格子 + private boolean isSide; + + public int getBoomsCount() { + return boomsCount; + } + + public void setBoomsCount(int boomsCount) { + this.boomsCount = boomsCount; + } + + public boolean isBoom() { + return isBoom; + } + + public void setIsBoom(boolean isBoom) { + this.isBoom = isBoom; + } + + public boolean isFlag() { + return isFlag; + } + + public void setIsFlag(boolean isFlag) { + this.isFlag = isFlag; + } + + public boolean isFlagWrong() { + return isFlagWrong; + } + + public void setIsFlagWrong(boolean isFlagWrong) { + this.isFlagWrong = isFlagWrong; + } + + public boolean isShow() { + return isShow; + } + + public void setIsShow(boolean isShow) { + this.isShow = isShow; + } + + public boolean isAutoShow() { + return isAutoShow; + } + + public void setIsAutoShow(boolean isAutoShow) { + this.isAutoShow = isAutoShow; + } + + public boolean isSide() { + return isSide; + } + + public void setIsSide(boolean isSide) { + this.isSide = isSide; + } +// 无参构造方法 + public GridEntity(){ + + } +// 含参构造方法 + public GridEntity(boolean isShow,boolean isFlag,boolean isSide,int boomsCount){ + this.isShow=isShow; + this.isFlag=isFlag; + this.isSide=isSide; + this.boomsCount=boomsCount; + } +} diff --git a/app/src/main/res/layout/activity_game.xml b/app/src/main/res/layout/activity_game.xml new file mode 100644 index 00000000..7fe92552 --- /dev/null +++ b/app/src/main/res/layout/activity_game.xml @@ -0,0 +1,36 @@ + + + + +