Java Tic Tac趾板-2块带碎片的板

Java Tic Tac趾板-2块带碎片的板,java,android,Java,Android,在这个问题上,我有: 在游戏中,我需要两个棋盘,其中一个是另一个的副本。然后单击其中一块板,根据player1Turn变量插入“X”或“O”符号 每个棋盘属于一个玩家,因此第一个棋盘属于“X”,第二个棋盘属于“O”。在游戏过程中,两个棋盘必须具有相同的状态,因此它们必须同步 到目前为止,我成功地制作了这块板,但我不知道如何添加另一块,也不知道如何使用碎片 1.我如何制作两块带有碎片的复制板 -我的应用程序只适用于第一个棋盘- 像这样 public类MainActivity扩展AppCompat

在这个问题上,我有:

在游戏中,我需要两个棋盘,其中一个是另一个的副本。然后单击其中一块板,根据
player1Turn
变量插入“X”或“O”符号

每个棋盘属于一个玩家,因此第一个棋盘属于“X”,第二个棋盘属于“O”。在游戏过程中,两个棋盘必须具有相同的状态,因此它们必须同步

到目前为止,我成功地制作了这块板,但我不知道如何添加另一块,也不知道如何使用碎片

1.我如何制作两块带有碎片的复制板

-我的应用程序只适用于第一个棋盘-

像这样

public类MainActivity扩展AppCompatActivity实现View.OnClickListener{
私有按钮[][]按钮X=新按钮[3][3];
私有按钮[][]按钮SO=新按钮[3][3];
私有布尔player1Turn=true;
私人整数整数;
私人int播放器1分;
私人int播放器2分;
私有文本视图文本视图播放器1;
私有文本视图文本视图播放器2;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewPlayer1=findViewById(R.id.text\u view\u p1);
textViewPlayer2=findViewById(R.id.text\u view\u p2);
对于(int i=0;i<3;i++){
对于(int j=0;j<3;j++){
字符串buttonID=“button_X”+i+j;
int resID=getResources().getIdentifier(buttonID,“id”,getPackageName());
buttonsX[i][j]=findViewById(剩余);
buttonsX[i][j].setOnClickListener(此);
}
}
Button buttonReset=findViewById(R.id.按钮重置);
buttonReset.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
重置游戏();
}
});
}
@凌驾
公共void onClick(视图v){
if(!((按钮)v.getText().toString()等于(“”){
返回;
}
如果(播放者1转){
((按钮)v.setText(“X”);
}否则{
(按钮)v.setText(“O”);
}
roundCount++;
if(checkForWin()){
如果(播放者1转){
player1Wins();
}否则{
player2Wins();
}
}else if(roundCount==9){
draw();
}否则{
player1Turn=!player1Turn;
}
}
私有布尔checkForWin(){
字符串[][]字段=新字符串[3][3];
对于(int i=0;i<3;i++){
对于(int j=0;j<3;j++){
字段[i][j]=按钮X[i][j].getText().toString();
}
}
对于(int i=0;i<3;i++){
如果(字段[i][0]。等于(字段[i][1])
&&字段[i][0]。等于(字段[i][2])
&&!字段[i][0]。等于(“”){
返回true;
}
}
对于(int i=0;i<3;i++){
如果(字段[0][i]。等于(字段[1][i])
&&字段[0][i]。等于(字段[2][i])
&&!字段[0][i]。等于(“”){
返回true;
}
}
如果(字段[0][0]。等于(字段[1][1])
&&字段[0][0]。等于(字段[2][2])
&&!字段[0][0]。等于(“”){
返回true;
}
如果(字段[0][2]。等于(字段[1][1])
&&字段[0][2]。等于(字段[2][0])
&&!字段[0][2]。等于(“”){
返回true;
}
返回false;
}
私人void player1Wins(){
玩家1分++;
Toast.makeText(这是“玩家1获胜!”,Toast.LENGTH_SHORT.show();
updatePointsText();
重置板();
}
私有void player2Wins(){
player2Points++;
Toast.makeText(这是“玩家2获胜!”,Toast.LENGTH_SHORT.show();
updatePointsText();
重置板();
}
私人提款(){
Toast.makeText(这个“Draw!”,Toast.LENGTH_SHORT.show();
重置板();
}
私有void updatePointsText(){
textViewPlayer1.setText(“播放器1:+Player1点数”);
textViewPlayer2.setText(“玩家2:+Player2分”);
}
私人发展局(){
对于(int i=0;i<3;i++){
对于(int j=0;j<3;j++){
按钮X[i][j].setText(“”);
}
}
轮数=0;
player1Turn=true;
}
私人游戏(){
玩家1分=0;
玩家2分=0;
updatePointsText();
重置板();
}
这是content_main.xml文件


可能有一种解决方案如下所示

请注意,它并不完美,存在大量重复代码和更多问题,因此由您来改进。尽管它可以工作,但如果某些东西工作,并不意味着它可以标记为一段好代码。

首先创建一个新片段:

<Right-click-on-project> -> New -> Fragment -> Fragment(Blank)
BoardFragment.java
应如下所示:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;


public class BoardFragment extends Fragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_board, container, false);
    }

}
然后用以下代码片段更改activity_main.xml,记住在
片段
标记中更改包

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/text_view_p8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DOPPIOTRIX"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text_view_p1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:freezesText="true"
            android:text="Player 1: "
            android:textSize="20sp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/text_view_p1_score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:freezesText="true"
            android:text="0"
            android:textSize="20sp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/text_view_p2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginBottom="24dp"
            android:freezesText="true"
            android:text="Player 2: "
            android:textSize="20sp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/text_view_p2_score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:freezesText="true"
            android:text="0"
            android:textSize="20sp"
            android:visibility="visible" />

        <Button
            android:id="@+id/button_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="reset"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:visibility="visible" />

    </LinearLayout>

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:gravity="center"
        android:text='GIOCATORE "X"'
        android:visibility="visible" />

    <!-- Change  andorid:name-->
    <fragment
        android:name="hr.hello.dm.test.BoardFragment"
        android:id="@+id/grid_X"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text='GIOCATORE "O"' />

    <!-- Change  andorid:name-->
    <fragment
        android:name="hr.hello.dm.test.BoardFragment"
        android:id="@+id/grid_O"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />


</LinearLayout>

最后是Java文件

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.gridlayout.widget.GridLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {



    private boolean player1Turn = true;

    private int roundCount;

    private int player1Points;
    private int player2Points;

    private TextView textViewPlayer1Score;
    private TextView textViewPlayer2Score;
    private GridLayout gridLayoutX;
    private GridLayout gridLayoutO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        textViewPlayer1Score = findViewById(R.id.text_view_p1_score);
        textViewPlayer2Score = findViewById(R.id.text_view_p2_score);

        gridLayoutX = findViewById(R.id.grid_X);

        int id = 0;
        for(View v : gridLayoutX.getTouchables()){
            v.setId(id++);
            v.setOnClickListener(this);
        }

        gridLayoutO = findViewById(R.id.grid_O);
        for(View v : gridLayoutO.getTouchables()){
            v.setId(id++);
            v.setOnClickListener(this);
        }

        Button buttonReset = findViewById(R.id.button_reset);
        buttonReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetGame();
            }
        });
    }


    @Override
    public void onClick(View v) {
        if (!((Button) v).getText().toString().equals("")) {
            return;
        }

        GridLayout currentGridLayout = player1Turn ? gridLayoutX : gridLayoutO;


        if (player1Turn && currentGridLayout.findViewById(v.getId()) != null) {
            ((Button) v).setText("X");
            ((Button) gridLayoutO.getTouchables().get(v.getId())).setText("X");
        } else if (!player1Turn  && currentGridLayout.findViewById(v.getId()) != null){
            ((Button) v).setText("O");
            ((Button) gridLayoutX.getTouchables().get(v.getId() - 9)).setText("O");
        } else {
            return;
        }

        roundCount++;

        if (checkForWin()) {
            if (player1Turn) {
                player1Wins();
            } else {
                player2Wins();
            }
        } else if (roundCount == 9) {
            draw();
        } else {
            player1Turn = !player1Turn;
        }

    }

    private boolean checkForWin() {
        String[][] field = new String[3][3];

        for (int i = 0; i < 9; i++) {
                field[i/3][i%3] = ((Button) gridLayoutX.getTouchables().get(i)).getText().toString();
        }

        for (int i = 0; i < 3; i++) {
            if (field[i][0].equals(field[i][1])
                    && field[i][0].equals(field[i][2])
                    && !field[i][0].equals("")) {
                return true;
            }
        }

        for (int i = 0; i < 3; i++) {
            if (field[0][i].equals(field[1][i])
                    && field[0][i].equals(field[2][i])
                    && !field[0][i].equals("")) {
                return true;
            }
        }

        if (field[0][0].equals(field[1][1])
                && field[0][0].equals(field[2][2])
                && !field[0][0].equals("")) {
            return true;
        }

        if (field[0][2].equals(field[1][1])
                && field[0][2].equals(field[2][0])
                && !field[0][2].equals("")) {
            return true;
        }

        return false;
    }

    private void player1Wins() {
        player1Points++;
        Toast.makeText(this, "Player 1 wins!", Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
    }

    private void player2Wins() {
        player2Points++;
        Toast.makeText(this, "Player 2 wins!", Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
    }

    private void draw() {
        Toast.makeText(this, "Draw!", Toast.LENGTH_SHORT).show();
        resetBoard();
    }

    private void updatePointsText() {
        textViewPlayer1Score.setText(String.valueOf(player1Points));
        textViewPlayer2Score.setText(String.valueOf(player2Points));
    }

    private void resetBoard() {
        for(View v : gridLayoutX.getTouchables()){
            ((Button) v).setText("");
        }

        for(View v : gridLayoutO.getTouchables()){
            ((Button) v).setText("");
        }

        roundCount = 0;
        player1Turn = true;
    }

    private void resetGame() {
        player1Points = 0;
        player2Points = 0;
        updatePointsText();
        resetBoard();
    }
}
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
导入androidx.appcompat.app.appcompat活动;
导入androidx.gridlayout.widget.gridlayout;
公共类MainActivity扩展AppCompatActivity实现View.OnClickListener{
私有布尔player1Turn=true;
私人整数整数;
私人int播放器1分;
私人int播放器2分;
私有文本视图文本视图播放器1核心;
私有TextView textViewPlayer2Score;
专用GridLayout gridLayoutX;
专用GridLayout gridLayoutO;
@爱
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;


public class BoardFragment extends Fragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_board, container, false);
    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/text_view_p8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DOPPIOTRIX"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/text_view_p1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:freezesText="true"
            android:text="Player 1: "
            android:textSize="20sp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/text_view_p1_score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:freezesText="true"
            android:text="0"
            android:textSize="20sp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/text_view_p2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginBottom="24dp"
            android:freezesText="true"
            android:text="Player 2: "
            android:textSize="20sp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/text_view_p2_score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:freezesText="true"
            android:text="0"
            android:textSize="20sp"
            android:visibility="visible" />

        <Button
            android:id="@+id/button_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="reset"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:visibility="visible" />

    </LinearLayout>

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:gravity="center"
        android:text='GIOCATORE "X"'
        android:visibility="visible" />

    <!-- Change  andorid:name-->
    <fragment
        android:name="hr.hello.dm.test.BoardFragment"
        android:id="@+id/grid_X"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text='GIOCATORE "O"' />

    <!-- Change  andorid:name-->
    <fragment
        android:name="hr.hello.dm.test.BoardFragment"
        android:id="@+id/grid_O"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />


</LinearLayout>
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.gridlayout.widget.GridLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {



    private boolean player1Turn = true;

    private int roundCount;

    private int player1Points;
    private int player2Points;

    private TextView textViewPlayer1Score;
    private TextView textViewPlayer2Score;
    private GridLayout gridLayoutX;
    private GridLayout gridLayoutO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        textViewPlayer1Score = findViewById(R.id.text_view_p1_score);
        textViewPlayer2Score = findViewById(R.id.text_view_p2_score);

        gridLayoutX = findViewById(R.id.grid_X);

        int id = 0;
        for(View v : gridLayoutX.getTouchables()){
            v.setId(id++);
            v.setOnClickListener(this);
        }

        gridLayoutO = findViewById(R.id.grid_O);
        for(View v : gridLayoutO.getTouchables()){
            v.setId(id++);
            v.setOnClickListener(this);
        }

        Button buttonReset = findViewById(R.id.button_reset);
        buttonReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetGame();
            }
        });
    }


    @Override
    public void onClick(View v) {
        if (!((Button) v).getText().toString().equals("")) {
            return;
        }

        GridLayout currentGridLayout = player1Turn ? gridLayoutX : gridLayoutO;


        if (player1Turn && currentGridLayout.findViewById(v.getId()) != null) {
            ((Button) v).setText("X");
            ((Button) gridLayoutO.getTouchables().get(v.getId())).setText("X");
        } else if (!player1Turn  && currentGridLayout.findViewById(v.getId()) != null){
            ((Button) v).setText("O");
            ((Button) gridLayoutX.getTouchables().get(v.getId() - 9)).setText("O");
        } else {
            return;
        }

        roundCount++;

        if (checkForWin()) {
            if (player1Turn) {
                player1Wins();
            } else {
                player2Wins();
            }
        } else if (roundCount == 9) {
            draw();
        } else {
            player1Turn = !player1Turn;
        }

    }

    private boolean checkForWin() {
        String[][] field = new String[3][3];

        for (int i = 0; i < 9; i++) {
                field[i/3][i%3] = ((Button) gridLayoutX.getTouchables().get(i)).getText().toString();
        }

        for (int i = 0; i < 3; i++) {
            if (field[i][0].equals(field[i][1])
                    && field[i][0].equals(field[i][2])
                    && !field[i][0].equals("")) {
                return true;
            }
        }

        for (int i = 0; i < 3; i++) {
            if (field[0][i].equals(field[1][i])
                    && field[0][i].equals(field[2][i])
                    && !field[0][i].equals("")) {
                return true;
            }
        }

        if (field[0][0].equals(field[1][1])
                && field[0][0].equals(field[2][2])
                && !field[0][0].equals("")) {
            return true;
        }

        if (field[0][2].equals(field[1][1])
                && field[0][2].equals(field[2][0])
                && !field[0][2].equals("")) {
            return true;
        }

        return false;
    }

    private void player1Wins() {
        player1Points++;
        Toast.makeText(this, "Player 1 wins!", Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
    }

    private void player2Wins() {
        player2Points++;
        Toast.makeText(this, "Player 2 wins!", Toast.LENGTH_SHORT).show();
        updatePointsText();
        resetBoard();
    }

    private void draw() {
        Toast.makeText(this, "Draw!", Toast.LENGTH_SHORT).show();
        resetBoard();
    }

    private void updatePointsText() {
        textViewPlayer1Score.setText(String.valueOf(player1Points));
        textViewPlayer2Score.setText(String.valueOf(player2Points));
    }

    private void resetBoard() {
        for(View v : gridLayoutX.getTouchables()){
            ((Button) v).setText("");
        }

        for(View v : gridLayoutO.getTouchables()){
            ((Button) v).setText("");
        }

        roundCount = 0;
        player1Turn = true;
    }

    private void resetGame() {
        player1Points = 0;
        player2Points = 0;
        updatePointsText();
        resetBoard();
    }
}