用Android写一个扫雷的小游戏
时间: 2023-12-29 10:18:11 浏览: 34
好的,以下是一个简单的扫雷小游戏的Android代码:
首先,在布局文件中添加一个GridView和一个Button。GridView用于显示雷区,Button用于开始游戏。
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="10"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp"
android:background="@color/gray"/>
<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Game"
android:layout_centerInParent="true"/>
</RelativeLayout>
```
接着,在MainActivity中实现扫雷逻辑:
```java
public class MainActivity extends AppCompatActivity {
private GridView mGridView;
private Button mStartButton;
private int mGridSize = 10;
private int mMineCount = 10;
private MineAdapter mAdapter;
private MineGame mGame;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView = findViewById(R.id.grid_view);
mStartButton = findViewById(R.id.start_button);
mGame = new MineGame(mGridSize, mMineCount);
mAdapter = new MineAdapter(this, mGame);
mGridView.setAdapter(mAdapter);
mStartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mGame.reset();
mAdapter.notifyDataSetChanged();
}
});
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mGame.isGameOver()) {
return;
}
int row = position / mGridSize;
int col = position % mGridSize;
if (mGame.isMine(row, col)) {
mGame.setGameOver(true);
mAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Game Over", Toast.LENGTH_SHORT).show();
} else {
mGame.openCell(row, col);
mAdapter.notifyDataSetChanged();
if (mGame.isWin()) {
mGame.setGameOver(true);
mAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "You Win!", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
```
其中,MineGame是一个代表扫雷游戏的类,包含了一些扫雷逻辑,如reset()方法用于初始化游戏,isMine()方法用于判断某个位置是否是地雷,openCell()方法用于打开某个单元格,isWin()方法用于判断游戏是否胜利等等。MineAdapter是一个自定义的Adapter,用于将MineGame中的数据显示在GridView中。
```java
public class MineAdapter extends BaseAdapter {
private Context mContext;
private MineGame mGame;
public MineAdapter(Context context, MineGame game) {
mContext = context;
mGame = game;
}
@Override
public int getCount() {
return mGame.getSize() * mGame.getSize();
}
@Override
public Object getItem(int position) {
int row = position / mGame.getSize();
int col = position % mGame.getSize();
return mGame.getCell(row, col);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.cell_layout, parent, false);
}
int row = position / mGame.getSize();
int col = position % mGame.getSize();
Cell cell = mGame.getCell(row, col);
TextView textView = view.findViewById(R.id.cell_text);
if (cell.isOpened()) {
view.setBackgroundColor(mContext.getResources().getColor(R.color.white));
if (cell.isMine()) {
textView.setText("M");
} else {
int count = mGame.getAdjacentMineCount(row, col);
if (count > 0) {
textView.setText(String.valueOf(count));
} else {
textView.setText("");
}
}
} else {
if (mGame.isGameOver() && cell.isMine()) {
view.setBackgroundColor(mContext.getResources().getColor(R.color.red));
textView.setText("M");
} else {
view.setBackgroundColor(mContext.getResources().getColor(R.color.gray));
textView.setText("");
}
}
return view;
}
}
```
最后,添加一个Cell类,代表一个单元格。
```java
public class Cell {
private boolean mIsMine;
private boolean mIsOpened;
public Cell(boolean isMine) {
mIsMine = isMine;
mIsOpened = false;
}
public boolean isMine() {
return mIsMine;
}
public void setMine(boolean mine) {
mIsMine = mine;
}
public boolean isOpened() {
return mIsOpened;
}
public void setOpened(boolean opened) {
mIsOpened = opened;
}
}
```
这样,一个简单的扫雷游戏就完成了。