-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #673 from hebinhai/master
- Loading branch information
Showing
8 changed files
with
609 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
app/src/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/GameActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package edu.hzuapps.androidworks.homeworks.com1314080901110; | ||
|
||
import android.content.Intent; | ||
import android.os.Message; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.view.Menu; | ||
import android.view.MenuItem; | ||
import android.view.View; | ||
import android.widget.AdapterView; | ||
import android.widget.Button; | ||
import android.widget.GridView; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import edu.hzuapps.androidworks.homeworks.com1314080901110.adapter.BoomAdapter; | ||
import edu.hzuapps.androidworks.homeworks.com1314080901110.entity.GridEntity; | ||
|
||
import java.util.Timer; | ||
import java.util.TimerTask; | ||
|
||
public class GameActivity extends AppCompatActivity { | ||
private Timer timer=new Timer(); | ||
private Button startGame; | ||
private Handler handler; | ||
private int gameTime=0; | ||
private TextView showTime; | ||
private final static int MESSAGE_UPDATE_TIME=1; | ||
private BoomAdapter adapter; | ||
private GridView gv; | ||
private int level=10; | ||
private boolean isGaming=false; | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_game); | ||
Intent intent=getIntent(); | ||
level=Integer.parseInt(intent.getStringExtra("level")); | ||
gv=(GridView)findViewById(R.id.gv); | ||
adapter=new BoomAdapter(level,gv,this); | ||
gv.setNumColumns(level); | ||
gv.setAdapter(adapter); | ||
inint(); | ||
addListener(); | ||
} | ||
|
||
@Override | ||
public boolean onCreateOptionsMenu(Menu menu) { | ||
// Inflate the menu; this adds items to the action bar if it is present. | ||
getMenuInflater().inflate(R.menu.menu_game, menu); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean onOptionsItemSelected(MenuItem item) { | ||
// Handle action bar item clicks here. The action bar will | ||
// automatically handle clicks on the Home/Up button, so long | ||
// as you specify a parent activity in AndroidManifest.xml. | ||
int id = item.getItemId(); | ||
|
||
//noinspection SimplifiableIfStatement | ||
if (id == R.id.action_settings) { | ||
return true; | ||
} | ||
|
||
return super.onOptionsItemSelected(item); | ||
} | ||
|
||
public void inint(){ | ||
startGame=(Button)findViewById(R.id.startGame); | ||
showTime = (TextView) findViewById(R.id.timeView); | ||
handler = new Handler() { | ||
@Override | ||
public void handleMessage(Message msg) { | ||
if (msg.what == MESSAGE_UPDATE_TIME) { | ||
showTime.setText("已用时间:" +gameTime/60+"分"+ gameTime%60 + "秒"); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public void startGame(){ | ||
adapter=new BoomAdapter(level,gv,this); | ||
gv.setNumColumns(level); | ||
gv.setAdapter(adapter); | ||
isGaming=true; | ||
gameTime=0; | ||
timer.cancel(); | ||
timer = new Timer(); | ||
timer.schedule(new TimerTask() { | ||
@Override | ||
public void run() { | ||
gameTime++; | ||
handler.sendEmptyMessage(MESSAGE_UPDATE_TIME); | ||
} | ||
}, 0, 1000); | ||
} | ||
|
||
/** | ||
* 方法:结束游戏 | ||
* */ | ||
public void stopGame(){ | ||
isGaming=false; | ||
timer.cancel(); | ||
} | ||
|
||
public void addListener(){ | ||
startGame.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
startGame(); | ||
startGame.setText("重新开始"); | ||
} | ||
}); | ||
gv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { | ||
@Override | ||
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { | ||
if (!isGaming) { | ||
return true; | ||
} | ||
GridEntity grid = adapter.getEntity(position); | ||
if (grid.isShow()) { | ||
return true; | ||
} | ||
grid.setIsFlag(!grid.isFlag()); | ||
adapter.notifyDataSetChanged(); | ||
return true; | ||
} | ||
}); | ||
|
||
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() { | ||
@Override | ||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | ||
if(!isGaming){ | ||
return; | ||
} | ||
// 如果游戏开始,通过position获取格子对象 | ||
GridEntity grid=adapter.getItem(position); | ||
// 如果格子对象被标记,则单击无效 | ||
if(grid.isFlag()){ | ||
return; | ||
} | ||
if(!grid.isShow()){ | ||
if(grid.isBoom()){ | ||
grid.setIsShow(true); | ||
stopGame(); | ||
adapter.showAllBooms(); | ||
// 检查旗子标记是否正确 | ||
adapter.checkFlag(); | ||
Toast.makeText(getApplicationContext(),"游戏失败,请重新开始!",Toast.LENGTH_SHORT).show(); | ||
return; | ||
} | ||
if(grid.getBoomsCount()==0&&!grid.isBoom()){ | ||
// 如果格子不是雷格且周围地雷数为0,则展现无雷区域 | ||
adapter.showNotBoomsArea(position); | ||
} | ||
grid.setIsShow(true); | ||
if(adapter.isWin()){ | ||
stopGame(); | ||
Toast.makeText(getApplicationContext(),"恭喜您!闯关成功,您的用时为"+showTime.getText(),Toast.LENGTH_LONG).show(); | ||
} | ||
adapter.notifyDataSetChanged(); | ||
} | ||
} | ||
}); | ||
} | ||
} |
146 changes: 146 additions & 0 deletions
146
...rc/main/java/edu/hzuapps/androidworks/homeworks/com1314080901110/adapter/BoomAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
Oops, something went wrong.