Android Studio软件实现五子棋小游戏代码
时间: 2023-08-05 21:18:21 浏览: 123
Android studio五子棋
5星 · 资源好评率100%
好的,下面是一个简单的五子棋小游戏的代码示例,仅供参考:
XML布局文件:activity_main.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_restart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重新开始"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/text_view_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏开始"
android:textSize="20sp"
android:layout_below="@+id/button_restart"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
<GridLayout
android:id="@+id/grid_layout_board"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="15"
android:columnCount="15"
android:layout_below="@+id/text_view_status"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:alignmentMode="alignMargins"
android:columnOrderPreserved="false">
</GridLayout>
</RelativeLayout>
```
Java代码:MainActivity.java
```java
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final int ROWS = 15;
private static final int COLS = 15;
private static final int EMPTY = 0;
private static final int BLACK = 1;
private static final int WHITE = 2;
private int[][] board = new int[ROWS][COLS];
private int currentPlayer = BLACK;
private boolean gameOver = false;
private GridLayout gridLayoutBoard;
private Button buttonRestart;
private TextView textViewStatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridLayoutBoard = (GridLayout) findViewById(R.id.grid_layout_board);
buttonRestart = (Button) findViewById(R.id.button_restart);
textViewStatus = (TextView) findViewById(R.id.text_view_status);
// 创建棋盘
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
BoardCell cell = new BoardCell(this, row, col);
gridLayoutBoard.addView(cell);
}
}
// 添加重新开始游戏的按钮事件
buttonRestart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
restartGame();
}
});
}
// 重新开始游戏
private void restartGame() {
currentPlayer = BLACK;
gameOver = false;
textViewStatus.setText("游戏开始");
// 清空棋盘
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
board[row][col] = EMPTY;
BoardCell cell = (BoardCell) gridLayoutBoard.getChildAt(row * COLS + col);
cell.setPlayer(EMPTY);
}
}
}
// 检查是否有一方获胜
private boolean checkWin(int player) {
// TODO: 实现胜负判断逻辑
return false;
}
// 更新游戏状态
private void updateGameStatus() {
if (checkWin(currentPlayer)) {
gameOver = true;
textViewStatus.setText(currentPlayer == BLACK ? "黑方获胜" : "白方获胜");
} else if (currentPlayer == BLACK) {
currentPlayer = WHITE;
textViewStatus.setText("轮到白方");
} else {
currentPlayer = BLACK;
textViewStatus.setText("轮到黑方");
}
}
// 棋盘单元格类
private class BoardCell extends androidx.appcompat.widget.AppCompatImageView {
private int row;
private int col;
private int player = EMPTY;
public BoardCell(MainActivity context, int row, int col) {
super(context);
this.row = row;
this.col = col;
setPadding(2, 2, 2, 2);
setLayoutParams(new GridLayout.LayoutParams(GridLayout.spec(row), GridLayout.spec(col)));
setBackgroundColor(Color.parseColor("#FFC0CB"));
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (gameOver) {
return;
}
if (player != EMPTY) {
return;
}
player = currentPlayer;
setImageResource(currentPlayer == BLACK ? R.drawable.black_chess : R.drawable.white_chess);
board[row][col] = currentPlayer;
updateGameStatus();
}
});
}
public void setPlayer(int player) {
this.player = player;
setImageResource(player == BLACK ? R.drawable.black_chess : player == WHITE ? R.drawable.white_chess : 0);
}
}
}
```
以上代码仅供参考,实际项目中需要根据实际需要进行修改和优化。
阅读全文