📅  最后修改于: 2023-12-03 15:38:30.567000             🧑  作者: Mango
Tic Tac Toe是一款简单的井字棋游戏,可以用来练习Android应用程序开发的基础知识。
在本文中,我们将介绍如何在Android平台上构建一个Tic Tac Toe游戏。
Tic Tac Toe游戏的程序流程如下:
首先,打开Android Studio并创建一个新的项目。
设计游戏界面需要使用布局文件 activity_main.xml
,在该布局文件中使用GridView控件绘制棋盘格。
<GridView
android:id="@+id/gridView"
android:numColumns="3"
android:layout_width="300dp"
android:layout_height="300dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
public enum CellType {
EMPTY,
CROSS,
CIRCLE
}
public class Cell {
public int x;
public int y;
public CellType type;
public Cell(int x, int y, CellType type) {
this.x = x;
this.y = y;
this.type = type;
}
}
public class BoardAdapter extends BaseAdapter {
private Context context;
private ArrayList<Cell> cells;
public BoardAdapter(Context context, ArrayList<Cell> cells) {
this.context = context;
this.cells = cells;
}
@Override
public int getCount() {
return cells.size();
}
@Override
public Object getItem(int position) {
return cells.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.cell, null);
}
ImageView imageView = v.findViewById(R.id.imgView);
if(cells.get(position).type == CellType.CROSS) {
imageView.setImageResource(R.drawable.cross);
}
else if(cells.get(position).type == CellType.CIRCLE) {
imageView.setImageResource(R.drawable.circle);
}
else {
imageView.setImageResource(0);
}
return v;
}
}
public class Game {
private static final int BOARD_SIZE = 3;
private ArrayList<Cell> cells = new ArrayList<>();
private BoardAdapter adapter;
private CellType currentPlayer;
private boolean gameOver;
public Game() {
for(int y = 0; y < BOARD_SIZE; y++) {
for(int x = 0; x < BOARD_SIZE; x++) {
cells.add(new Cell(x, y, CellType.EMPTY));
}
}
currentPlayer = CellType.CROSS;
gameOver = false;
}
public ArrayList<Cell> getCells() {
return cells;
}
public BoardAdapter getAdapter() {
return adapter;
}
public boolean isGameOver() {
return gameOver;
}
public void play(int x, int y) { }
private Cell getCell(int x, int y) { }
private boolean checkWin() { }
private boolean checkLine(int x1, int y1, int x2, int y2, int x3, int y3) { }
private boolean checkFull() { }
private void switchPlayer() { }
}
private void switchPlayer() {
if(currentPlayer == CellType.CROSS) {
currentPlayer = CellType.CIRCLE;
}
else {
currentPlayer = CellType.CROSS;
}
}
我们需要修改 checkBox()
方法,以在玩家落子后检查游戏是否已经结束。如果游戏结束,我们会设置 gameOver
变量,并显示谁赢了。
public void play(int x, int y) {
if(gameOver) {
return;
}
Cell cell = getCell(x, y);
if(cell.type != CellType.EMPTY) {
return;
}
cell.type = currentPlayer;
adapter.notifyDataSetChanged();
if(checkWin()) {
gameOver = true;
Toast.makeText((MainActivity)context, currentPlayer + " wins!", Toast.LENGTH_LONG).show();
return;
}
if(checkFull()) {
gameOver = true;
Toast.makeText((MainActivity)context, "It's a draw!", Toast.LENGTH_LONG).show();
return;
}
switchPlayer();
}
下面我们来实现 checkWin()
方法,该方法会检查当前局面是否有胜利者。我们需要检查九种可能的胜利方式:三行、三列和两个对角线。
private boolean checkWin() {
return checkLine(0, 0, 0, 1, 0, 2) || checkLine(1, 0, 1, 1, 1, 2) ||
checkLine(2, 0, 2, 1, 2, 2) || checkLine(0, 0, 1, 0, 2, 0) ||
checkLine(0, 1, 1, 1, 2, 1) || checkLine(0, 2, 1, 2, 2, 2) ||
checkLine(0, 0, 1, 1, 2, 2) || checkLine(2, 0, 1, 1, 0, 2);
}
private boolean checkLine(int x1, int y1, int x2, int y2, int x3, int y3) {
return cells.get(y1 * BOARD_SIZE + x1).type == cells.get(y2 * BOARD_SIZE + x2).type &&
cells.get(y2 * BOARD_SIZE + x2).type == cells.get(y3 * BOARD_SIZE + x3).type &&
cells.get(y1 * BOARD_SIZE + x1).type != CellType.EMPTY;
}
我们需要实现 checkFull()
方法,该方法会检查当前是否已满局。当当前玩家无法下棋时,才会宣布满局。
private boolean checkFull() {
for(Cell cell : cells) {
if(cell.type == CellType.EMPTY) {
return false;
}
}
return true;
}
至此,游戏逻辑已经全部实现。
我们需要在MainActivity中监听GridView的点击事件,用于处理用户交互。
public class MainActivity extends AppCompatActivity {
private GridView gridView;
private Game game;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = findViewById(R.id.gridView);
game = new Game();
game.setContext(this);
game.setGridView(gridView);
}
public void play(View view) {
game.reset();
}
}
Tic Tac Toe游戏是一个简单而有趣的游戏,通过学习本文,你已经学会了如何在Android平台上构建一个Tic Tac Toe游戏。对于初学者来说,这是一个非常好的练习项目,可以帮助你更好地理解Android应用程序开发的原理和方法。