Java的eclipse实现Android编程开发app实现井字棋的功能,要求给出MainActivity和layout布局的代码
好的,下面是一个简单的井字棋(Tic-Tac-Toe)游戏的实现,包括MainActivity
和布局文件activity_main.xml
。这个示例将帮助你在Eclipse中使用Java进行Android开发。
package com.example.tictactoe;
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 boolean playerXTurn = true; // true for X, false for O
private int[][] board = new int[3][3]; // 0 for empty, 1 for X, 2 for O
private TextView statusTextView;
private Button[][] buttons = new Button[3][3];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
statusTextView = findViewById(R.id.statusTextView);
GridLayout gridLayout = findViewById(R.id.gridLayout);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j] = (Button) gridLayout.getChildAt(i * 3 + j);
final int finalI = i;
final int finalJ = j;
buttons[i][j].setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onButtonClick(finalI, finalJ);
}
});
}
}
resetBoard();
}
private void onButtonClick(int row, int col) {
if (board[row][col] != 0) {
return; // Cell already occupied
}
board[row][col] = playerXTurn ? 1 : 2;
buttons[row][col].setText(playerXTurn ? "X" : "O");
if (checkWin()) {
statusTextView.setText(playerXTurn ? "Player X wins!" : "Player O wins!");
disableButtons();
} else if (isBoardFull()) {
statusTextView.setText("It's a draw!");
} else {
playerXTurn = !playerXTurn;
statusTextView.setText(playerXTurn ? "Player X's turn" : "Player O's turn");
}
}
private boolean checkWin() {
for (int i = 0; i < 3; i++) {
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != 0) {
return true;
}
if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != 0) {
return true;
}
}
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != 0) {
return true;
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != 0) {
return true;
}
return false;
}
private boolean isBoardFull() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == 0) {
return false;
}
}
}
return true;
}
private void disableButtons() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].setEnabled(false);
}
}
}
private void resetBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = 0;
buttons[i][j].setText("");
buttons[i][j].setEnabled(true);
}
}
playerXTurn = true;
statusTextView.setText("Player X's turn");
}
public void onResetButtonClick(View view) {
resetBoard();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/statusTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player X's turn"
android:textSize="24sp"
android:layout_gravity="center_horizontal"
android:paddingBottom="16dp"/>
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:columnCount="3"
android:rowCount="3">
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_row="0"
android:layout_column="0"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="32sp"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_row="0"
android:layout_column="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="32sp"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_row="0"
android:layout_column="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="32sp"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_row="1"
android:layout_column="0"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="32sp"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_row="1"
android:layout_column="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="32sp"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_row="1"
android:layout_column="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="32sp"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_row="2"
android:layout_column="0"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="32sp"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_row="2"
android:layout_column="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="32sp"/>
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_row="2"
android:layout_column="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:textSize="32sp"/>
</GridLayout>
<Button
android:id="@+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:onClick="onResetButtonClick"
android:layout_gravity="center_horizontal"
android:paddingTop="16dp"/>
</LinearLayout>
MainActivity.java:
playerXTurn
变量用于跟踪当前轮到哪位玩家。board
数组用于存储棋盘状态。statusTextView
显示当前游戏状态。buttons
数组存储每个按钮的引用。onButtonClick
方法处理按钮点击事件,更新棋盘状态并检查是否有玩家获胜或平局。checkWin
方法检查是否有玩家获胜。isBoardFull
方法检查棋盘是否已满。disableButtons
方法在游戏结束后禁用所有按钮。resetBoard
方法重置棋盘状态。onResetButtonClick
方法处理重置按钮点击事件。activity_main.xml:
LinearLayout
作为根布局,包含一个 TextView
显示游戏状态,一个 GridLayout
显示井字棋棋盘,以及一个重置按钮。将这两个文件添加到你的Eclipse项目中,并确保你的项目配置正确。运行应用程序,你将看到一个简单的井字棋游戏。希望这对你有所帮助!