游戏在插入重置游戏的循环后崩溃。

huangapple 未分类评论45阅读模式
英文:

game crash after inserting a loop for reset game

问题

public class MainActivity extends AppCompatActivity {
    // 黄色 = 0,红色 = 1
    // 2 = 未落子
    int activePlayer = 0;
    int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2};
    int[][] winningPositions = {
            {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6},
            {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, {2, 4, 6}
    };

    public void dropIn(View view) {
        ImageView counter = (ImageView) view;

        int tappedCounter = Integer.parseInt(counter.getTag().toString());
        if (gameState[tappedCounter] == 2) {
            gameState[tappedCounter] = activePlayer;

            counter.setTranslationY(-1000f);
            if (activePlayer == 0) {
                counter.setImageResource(R.drawable.yellow);
                activePlayer = 1;
            } else {
                counter.setImageResource(R.drawable.red);
                activePlayer = 0;
            }

            counter.animate().translationYBy(1000f).setDuration(300);

            for (int[] winningPosition : winningPositions) {
                if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
                        gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
                        gameState[winningPosition[0]] != 2) {
                    System.out.println(gameState[winningPosition[0]]);

                    String winner = "Red";

                    if (gameState[winningPosition[0]] == 0) {
                        winner = "Yellow";
                    }

                    // 有人获胜
                    TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
                    winnerMessage.setText(winner + " 获胜!");
                    LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
                    layout.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    public void playAgain(View view) {
        LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
        layout.setVisibility(View.INVISIBLE);

        activePlayer = 0;

        for (int i = 0; i < gameState.length; i++) {
            gameState[i] = 2;
        }

        GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);

        for (int i = 0; i < gridLayout.getChildCount(); i++) {
            ((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
        }
    }

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

I'm trying to create a connect style game for an assignment. Everything went well until I added the code for the playAgain layout which is supposed to reset the game to the start, but instead it crashes. I keep getting an error for the gridlayout for some reason.

public class MainActivity extends AppCompatActivity {
    //yellow=0 ,red=1
    //2=not played
    int activePlayer=0;
    int[]gameState={2,2,2,2,2,2,2,2,2,};
    int[][] winningPositions={{0,1,2},{3,4,5},{6,7,8},{0,3,6},
            {1,4,7},{2,5,8},{0,4,8},{2,4,6}};


    public void dropIn (View view){
        ImageView counter=(ImageView) view;


        int tappedCounter=Integer.parseInt(counter.getTag().toString());
        if (gameState[tappedCounter]==2) {
            gameState[tappedCounter]=activePlayer;

            counter.setTranslationY(-1000f);
            if (activePlayer == 0) {
                counter.setImageResource(R.drawable.yellow);
                activePlayer = 1;

            } else {
                counter.setImageResource(R.drawable.red);
                activePlayer = 0;

            }


            counter.animate().translationYBy(1000f).setDuration(300);

            for (int[] winningPositions : winningPositions){
                if (gameState[winningPositions[0]]==gameState[winningPositions[1]]
                        &amp;&amp; gameState[winningPositions[1]]==gameState[winningPositions[2]] &amp;&amp;
                 gameState[winningPositions[0]]!=2){
                    System.out.println(gameState[winningPositions[0]]);

                    String winner =&quot;Red&quot;;

                    if (gameState[winningPositions[0]]==0){

                        winner=&quot;Yellow&quot;;
                    }




                    //someone has won

                    TextView winnerMessage=(TextView) findViewById(R.id.winnerMessage);
                    winnerMessage.setText(winner + &quot; has WON!&quot;);
                    LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
                    layout.setVisibility(View.VISIBLE);
                }



            }

        }





    }

    public void playAgain(View view) {


        LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
        layout.setVisibility(View.INVISIBLE);

        activePlayer=0;

        for (int i= 0;i&lt;gameState.length;i++){

            gameState [i]=2;
        }


             GridLayout gridLayout=(GridLayout)findViewById(R.id.gridLayout);

        for (int i=0;i&lt;gridLayout.getChildCount();i++){
            ((ImageView) gridLayout.getChildAt(i)).setImageResource(0);

        }

    }


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

huangapple
  • 本文由 发表于 2020年4月8日 01:38:37
  • 转载请务必保留本文链接:https://java.coder-hub.com/61086053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定