Android Studio:更新数组中的分数覆盖

时间:2018-03-18 20:04:20

标签: java android arrays arraylist

我目前正致力于SOS游戏。当您拥有 SOS 序列时,玩家得分会更新。我使用数组来检查是否存在任何 SOS 序列。

当电路板上有2个 SOS 序列时,我的程序会计算之前的SOS,并根据电路板上存在的 SOS 序列的数量更新分数。例如,当玩家1进行 SOS 序列时,p1得分增加为1.当玩家2进行 SOS 序列时,p2得分增加到2而不是1(检查图像< /强>)。

我无法弄清楚,我尝试了数组列表,但我无法解决问题。我的程序会覆盖分数。我该如何处理这个问题?

İmage of the game

这是我的checkSOS方法:

public void checkSOS() {
    String[] btString;
    btString = new String[49];
    Button[] buttons;
    buttons = new Button[49];
    for (int i = 0; i < buttons.length; i++) {            
            String buttonID = "btn" + (i+1);
            int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
            buttons[i] = ((Button) findViewById(resID));
            btString[i] = buttons[i].getText().toString();

    }

    int p1sc = 0;
    int p2sc = 0;
    TextView playersc = (TextView) findViewById(R.id.p1score);
    TextView player2sc = (TextView) findViewById(R.id.p2score);

    for (int i = 0; i < buttons.length; i++) {
           if (i <= 46) {
             if(btString[i].contains("S") 
             && btString[i + 1].contains("O") 
             && btString[i + 2].contains("S") 
             && i != 5 && i != 6 && i != 12 
             && i != 13 && i != 19 && i != 20
             && i != 26 && i != 27 && i != 33
             && i != 34 && i != 40 && i != 41) {
                   if (playerTurn == 1) {
                          p1sc += 1;
                          playersc.setText("" + p1sc);                              
                          Toast.makeText(this,""+a,Toast.LENGTH_SHORT).show();
                   }
                   else  {
                         p2sc += 1;
                         player2sc.setText("" + p2sc);

                  }

            }// if statement for horizontal
     } //For loop
 }

0 个答案:

没有答案