如何在Tic Tac Toe游戏中阻止玩家?

时间:2017-02-06 02:40:34

标签: java android android-studio tic-tac-toe

我的Tic Tac Toe表:

| 1 | 2 | 3 |

| 4 | 5 | 6 |

| 7 | 8 | 9 |

每个号码都是一个可以点击的按钮。

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

阻止方法:

                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);
                    }
                }
            }

0 个答案:

没有答案