Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在井字游戏中阻止玩家?_Java_Android_Android Studio_Tic Tac Toe - Fatal编程技术网

Java 如何在井字游戏中阻止玩家?

Java 如何在井字游戏中阻止玩家?,java,android,android-studio,tic-tac-toe,Java,Android,Android Studio,Tic Tac Toe,我的Tic Tac Toe桌: | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | 每个数字都是一个可以单击的按钮 我认为块方法是我出错的地方。在这种方法中,我希望按钮1和5中玩家的“x”标记被按钮9中计算机的“O”标记阻止。我该怎么做呢 块方法: 完整代码: package com.example.a1621656.noughtandcrosss; 导入android.content.DialogInterface; 导入android.os.Bundle;

我的Tic Tac Toe桌:


| 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | 每个数字都是一个可以单击的按钮

我认为块方法是我出错的地方。在这种方法中,我希望按钮1和5中玩家的“x”标记被按钮9中计算机的“O”标记阻止。我该怎么做呢

块方法: 完整代码:
package com.example.a1621656.noughtandcrosss;
导入android.content.DialogInterface;
导入android.os.Bundle;
导入android.support.design.widget.FloatingActionButton;
导入android.support.design.widget.Snackbar;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.Button;
导入com.google.android.gms.appindexing.Action;
导入com.google.android.gms.AppIndex.AppIndex;
导入com.google.android.gms.common.api.GoogleAppClient;
导入静态android.R.attr.button;
公共类MainActivity扩展AppCompatActivity实现View.OnClickListener{
按钮[]按钮=新按钮[10];
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
//fab.setOnClickListener();
按钮[1]=(按钮)findViewById(R.id.one);
按钮[1]。setOnClickListener(此);
按钮[2]=(按钮)findViewById(R.id.two);
按钮[2]。setOnClickListener(此);
按钮[3]=(按钮)findViewById(R.id.three);
按钮[3]。setOnClickListener(此);
按钮[4]=(按钮)findViewById(R.id.four);
按钮[4]。setOnClickListener(此);
按钮[5]=(按钮)findViewById(R.id.five);
按钮[5]。setOnClickListener(此);
按钮[6]=(按钮)findViewById(R.id.six);
按钮[6]。setOnClickListener(此);
按钮[7]=(按钮)findViewById(R.id.seven);
按钮[7]。setOnClickListener(此);
按钮[8]=(按钮)findViewById(R.id.8);
按钮[8]。setOnClickListener(此);
按钮[9]=(按钮)findViewById(R.id.nine);
按钮[9]。setOnClickListener(此);
按钮[1]。setText(“”);
按钮[1]。设置已启用(true);
按钮NewGame=(按钮)findViewById(R.id.new_游戏);
按钮重做=(按钮)findViewById(R.id.redo\u move);
按钮撤消=(按钮)findViewById(R.id.undo\u move);
}
公共空间移动(int i){
按钮[i].setText(“X”);
按钮[i].setOnClickListener(此);
}
//回应1-5种方法是基于玩家最后一步的计算机移动。
公营机构{

对于(int i=5;我使用3x3字节数组,用0初始化,在每个按钮上单击用1初始化每个索引并检查可能的条件,然后用1初始化它
isEnabled()
返回一个
布尔值
。您必须使用
&
|
而不是
*
@UmaKanth它不起作用。
                public void PlayerMoveBlocker() {
                    if (buttons[1].isEnabled() * buttons[5].isEnabled()) {
                        buttons[9].setText("O");
                        buttons[9].setEnabled(false);
                        break;
                    }else {
                        break;
                    }
                }
            package com.example.a1621656.noughtandcrosses;

            import android.content.DialogInterface;
            import android.os.Bundle;
            import android.support.design.widget.FloatingActionButton;
            import android.support.design.widget.Snackbar;
            import android.support.v7.app.AppCompatActivity;
            import android.support.v7.widget.Toolbar;
            import android.view.Menu;
            import android.view.MenuItem;
            import android.view.View;
            import android.widget.Button;

            import com.google.android.gms.appindexing.Action;
            import com.google.android.gms.appindexing.AppIndex;
            import com.google.android.gms.common.api.GoogleApiClient;

            import static android.R.attr.button;

            public class MainActivity extends AppCompatActivity implements View.OnClickListener {

                Button[] buttons = new Button[10];

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                    setSupportActionBar(toolbar);

                    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
                    //fab.setOnClickListener();

                    buttons[1] = (Button) findViewById(R.id.one);
                    buttons[1].setOnClickListener(this);
                    buttons[2] = (Button) findViewById(R.id.two);
                    buttons[2].setOnClickListener(this);
                    buttons[3] = (Button) findViewById(R.id.three);
                    buttons[3].setOnClickListener(this);
                    buttons[4] = (Button) findViewById(R.id.four);
                    buttons[4].setOnClickListener(this);
                    buttons[5] = (Button) findViewById(R.id.five);
                    buttons[5].setOnClickListener(this);
                    buttons[6] = (Button) findViewById(R.id.six);
                    buttons[6].setOnClickListener(this);
                    buttons[7] = (Button) findViewById(R.id.seven);
                    buttons[7].setOnClickListener(this);
                    buttons[8] = (Button) findViewById(R.id.eight);
                    buttons[8].setOnClickListener(this);
                    buttons[9] = (Button) findViewById(R.id.nine);
                    buttons[9].setOnClickListener(this);

                    buttons[1].setText("");
                    buttons[1].setEnabled(true);

                    Button NewGame = (Button) findViewById(R.id.new_game);
                    Button redo = (Button) findViewById(R.id.redo_move);
                    Button undo = (Button) findViewById(R.id.undo_move);
                }


                public void makeMove(int i) {
                    buttons[i].setText("X");
                    buttons[i].setOnClickListener(this);

                }

                //Respond 1-5 methods are computer moves based on the players last move.
                public void respond() {
                    for (int i = 5; i <= 10; i++) {
                        if (buttons[i].isEnabled()) {
                            buttons[i].setText("O");
                            buttons[i].setEnabled(false);
                            break;
                        }else {
                            buttons[1].setText("O");
                            buttons[1].setEnabled(false);
                            break;
                        }
                    }
                }

                public void respond2() {
                    for (int i = 3; i <= 10; i++) {
                        if (buttons[i].isEnabled()) {
                            buttons[3].setText("O");
                            buttons[3].setEnabled(false);
                            break;
                        }else {
                            buttons[2].setText("O");
                            buttons[2].setEnabled(false);
                            break;
                        }
                    }
                }

                public void respond3() {
                    for (int i = 4; i <= 10; i++) {
                        if (buttons[i].isEnabled()) {
                            buttons[4].setText("O");
                            buttons[4].setEnabled(false);
                            break;
                        }else {
                            buttons[8].setText("O");
                            buttons[8].setEnabled(false);
                            break;
                        }
                    }
                }

                public void respond4() {
                    for (int i = 6; i <= 10; i++) {
                        if (buttons[i].isEnabled()) {
                            buttons[6].setText("O");
                            buttons[6].setEnabled(false);
                            break;
                        }else {
                            buttons[7].setText("O");
                            buttons[7].setEnabled(false);
                            break;
                        }
                    }
                }

                public void respond5() {
                    for (int i = 9; i <= 10; i++) {
                        if (buttons[i].isEnabled()) {
                            buttons[9].setText("O");
                            buttons[9].setEnabled(false);
                            break;
                        }else {
                            respond();
                            break;
                        }
                    }
                }

                //This is where the move is blocked.
                public void PlayerMoveBlocker() {
                    if (buttons[1].isEnabled() * buttons[5].isEnabled()) {
                        buttons[9].setText("O");
                        buttons[9].setEnabled(false);
                        break;
                    }else {
                        break;
                    }
                }


                public void clearGrid() {
                    for (int i = 1; i < 10; i++) {
                        buttons[i].setOnClickListener(this);
                    }
                }

                @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                    // Inflate the menu; this adds items to the action bar if it is present.
                    getMenuInflater().inflate(R.menu.menu_main, menu);
                    return true;
                }

                @Override
                public boolean onOptionsItemSelected(MenuItem item) {
                    // Handle action bar item clicks here. The action bar will
                    // automatically handle clicks on the Home/Up button, so long
                    // as you specify a parent activity in AndroidManifest.xml.
                    int id = item.getItemId();

                    //noinspection SimplifiableIfStatement
                    if (id == R.id.action_settings) {
                        return true;
                    }

                    return super.onOptionsItemSelected(item);
                }

                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.one:
                            buttons[1].setText("X");
                            buttons[1].setEnabled(false);
                            respond2();
                            break;
                        case R.id.two:
                            buttons[2].setText("X");
                            buttons[2].setEnabled(false);
                            respond();
                            break;
                        case R.id.three:
                            buttons[3].setText("X");
                            buttons[3].setEnabled(false);
                            respond();
                            break;
                        case R.id.four:
                            buttons[4].setText("X");
                            buttons[4].setEnabled(false);
                            respond3();
                            break;
                        case R.id.five:
                            buttons[5].setText("X");
                            buttons[5].setEnabled(false);
                            respond2();
                            break;
                        case R.id.six:
                            buttons[6].setText("X");
                            buttons[6].setEnabled(false);
                            respond4();
                            break;
                        case R.id.seven:
                            buttons[7].setText("X");
                            buttons[7].setEnabled(false);
                            respond4();
                            break;
                        case R.id.eight:
                            buttons[8].setText("X");
                            buttons[8].setEnabled(false);
                            respond3();
                            break;
                        case R.id.nine:
                            buttons[9].setText("X");
                            buttons[9].setEnabled(false);
                            respond5();
                            break;
                        }
                    }

                public void clearGame() {
                    clearGrid();
                }

                private void setOnClickListener(DialogInterface.OnClickListener onClickListener) {
                    for (int i = 1; i < 10; i++) {
                        buttons[i].setOnClickListener(this);
                    }
                }
            }