2017-07-03 1 views
-2

私はアンドロイドを初めて使っています。向きの切り替えやホームボタンを押してアプリに戻るため、現在のゲームを保存してコードを正しくコーディングする方法を見つけ出すのに問題があります。私は設定とonSaveInstanceStateについて読んだことがありますが、私はTic-Tac-Toeアプリケーションで正しく動作するように設定するのに苦労しています。誰かがこれを設定してプロセスを説明するのを手伝ってもらえますか?前もって感謝します。オリエンテーションが切り替えられたり、別のアプリが開いているときに、現在のゲームを保存してリロードする方法は?

public class MainActivity extends Activity implements View.OnClickListener { 

    private Button TopLeft; 
    private Button TopCenter; 
    private Button TopRight; 
    private Button CenterLeft; 
    private Button Center; 
    private Button CenterRight; 
    private Button BottomLeft; 
    private Button BottomCenter; 
    private Button BottomRight; 
    private Button newGameButton; 
    private Button gameGrid[][] = new Button[3][3]; 
    private TextView displayOut; 
    private Random ran = new Random(); 
    private int count = 9; 
    private View v; 
    private boolean p = false; 
    private boolean c = false; 

    // define the SharedPreferences object 
    private SharedPreferences savedValues; 

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

     //Get references to the widgets 
     gameGrid[0][0] = (Button) findViewById(R.id.TopLeft); 
     gameGrid[0][1] = (Button) findViewById(R.id.TopCenter); 
     gameGrid[0][2] = (Button) findViewById(R.id.TopRight); 
     gameGrid[1][0] = (Button) findViewById(R.id.CenterLeft); 
     gameGrid[1][1] = (Button) findViewById(R.id.Center); 
     gameGrid[1][2] = (Button) findViewById(R.id.CenterRight); 
     gameGrid[2][0] = (Button) findViewById(R.id.BottomLeft); 
     gameGrid[2][1] = (Button) findViewById(R.id.BottomCenter); 
     gameGrid[2][2] = (Button) findViewById(R.id.BottomRight); 
     newGameButton = (Button) findViewById(R.id.NewGame); 
     displayOut = (TextView) findViewById(R.id.DisplayOut); 

     // set each button to listen for click 
     for (int x = 0; x < gameGrid.length; x++) { 
      for (int y = 0; y < gameGrid[x].length; y++) { 
       gameGrid[x][y].setOnClickListener(this); 
      } 
      newGameButton.setOnClickListener(this); 
      displayOut.setText("It's Player X's turn."); 
     } 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
    } 

    public void onClick(View v) { 
     boolean check = false; 
     switch (v.getId()) 
     { 
      case R.id.NewGame: 
       newGame(); 
       break; 
      default: 
      { 
       playerTurn(v); 
      } 
     } 
    } 

    private void newGame() { 
     for (int x = 0; x < gameGrid.length; x++) 
     { 
      for (int y = 0; y < gameGrid[x].length; y++) 
      { 
       gameGrid[x][y].setText(""); 
       gameGrid[x][y].setClickable(true); 
       displayOut.setText("It's Player X's turn."); 
      } 
     } 
     count = 9; 
    } 

    private void playerTurn(View v) { 

     for (int x = 0; x < gameGrid.length; x++) 
     { 
      for (int y = 0; y < gameGrid[x].length; y++) 
      { 
       if(gameGrid[x][y].getText() == "O") 
       { 
        gameGrid[x][y].setClickable(false); 
       } 
       if(gameGrid[x][y].getText()== "X") 
       { 
        gameGrid[x][y].setClickable(false); 
       } 
       else 
       { 
        gameGrid[x][y].setEnabled(true); 
       } 
      } 
     } 
     Button b = (Button) v; 

     b.setText("X"); 

     p = checkWinner(); 
     count--; 
     if(p == true) 
     { 
      displayOut.setText("Congrats Player X wins!"); 
      endGame(); 
      return; 
     } 
     if(count ==0 && p == false) 
     { 
      displayOut.setText("Sorry, you tied"); 
      endGame(); 
      return; 
     } 
     else 
      displayOut.setText("It's the computer's turn."); 
      computerTurn(); 
    } 

    private void computerTurn() { 


     final Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      public void run() { 
       if (count == 0) { 
        displayOut.setText("Game Ends"); 
        return; 
       } 
//starts here 
       try { 
        //check to see if win vertically 
        for (int i = 0; i < 2; i++) { 

         if (gameGrid[0][i].getText() == gameGrid[1][i].getText() && 
           gameGrid[0][i].getText() == "O") { 

          if (gameGrid[2][i].getText() == "") { 
           gameGrid[2][i].setText("O"); 


           return; 
          } 

         } 
        } 
        for (int i = 0; i < 2; i++) { 

         if (gameGrid[2][i].getText() == gameGrid[1][i].getText() && 
           gameGrid[2][i].getText() == "O") { 

          if (gameGrid[0][i].getText() == "") { 
           gameGrid[0][i].setText("O"); 


           return; 
          } 

         } 
        } 
        for (int i = 0; i < 2; i++) { 
         if (gameGrid[2][i].getText() == gameGrid[0][i].getText() && 
           gameGrid[2][i].getText() == "O") { 
          if (gameGrid[1][i].getText() == "") { 
           gameGrid[1][i].setText("O"); 


           return; 
          } 
         } 
        } 
        //check to see if win horizontally 
        for (int i = 0; i < 2; i++) { 

         if (gameGrid[i][0].getText() == gameGrid[i][1].getText() 
           && gameGrid[i][0].getText() == "O") { 

          if (gameGrid[i][2].getText() == "") { 
           gameGrid[i][2].setText("O"); 


           return; 
          } 

         } 

        } 

        for (int i = 0; i < 2; i++) { 

         if (gameGrid[i][2].getText() == gameGrid[i][1].getText() && 
           gameGrid[i][2].getText() == "O") { 

          if (gameGrid[i][0].getText() == "") { 
           gameGrid[i][0].setText("O"); 


           return; 
          } 

         } 

        } 
        for (int i = 0; i < 2; i++) { 
         if (gameGrid[i][0].getText() == gameGrid[i][2].getText() && 
           gameGrid[i][0].getText() == "O") { 
          if (gameGrid[i][1].getText() == "") { 
           gameGrid[i][1].setText("O"); 


           return; 
          } 
         } 
        } 
        // check if you can take a win diagonally 


        if (gameGrid[0][0].getText() == gameGrid[1][1].getText() 
          && gameGrid[0][0].getText() == "O") { 

         if (gameGrid[2][2].getText() == "") { 
          gameGrid[2][2].setText("O"); 


          return; 
         } 
        } 

        if (gameGrid[2][2].getText() == gameGrid[1][1].getText() 
          && gameGrid[2][2].getText() == "O") { 

         if (gameGrid[0][0].getText() == "") { 
          gameGrid[0][0].setText("O"); 


          return; 
         } 
        } 
        if (gameGrid[2][2].getText() == gameGrid[0][0].getText() && 
          gameGrid[2][2].getText() == "O") { 
         if (gameGrid[1][1].getText() == "") { 
          gameGrid[1][1].setText("O"); 


          return; 
         } 
        } 

        if (gameGrid[0][2].getText() == gameGrid[1][1].getText() 
          && gameGrid[0][2].getText() == "O") { 

         if (gameGrid[2][0].getText() == "") { 
          gameGrid[2][0].setText("O"); 


          return; 
         } 
        } 

        if (gameGrid[2][0].getText() == gameGrid[1][1].getText() 
          && gameGrid[2][0].getText() == "O") { 

         if (gameGrid[0][2].getText() == "") { 
          gameGrid[0][2].setText("O"); 


          return; 
         } 
        } 
        if (gameGrid[2][0].getText() == gameGrid[0][2].getText() && 
          gameGrid[2][0].getText() == "O") { 
         if (gameGrid[1][1].getText() == "") { 
          gameGrid[1][1].setText("O"); 


          return; 
         } 
        } 

        // BLOCKS!!!! // 

        // check if you can block a win vertically 
        for (int i = 0; i < 2; i++) { 

         if (gameGrid[0][i].getText() == gameGrid[1][i].getText() 
           && gameGrid[0][i].getText() == "X") { 
          if (gameGrid[2][i].getText() == "") { 
           gameGrid[2][i].setText("O"); 

           return; 
          } 

         } 

        } 

        for (int i = 0; i < 2; i++) { 

         if (gameGrid[2][i].getText() == gameGrid[1][i].getText() 
           && gameGrid[1][i].getText() == "X") { 

          if (gameGrid[0][i].getText() == "") { 
           gameGrid[0][i].setText("O"); 

           return; 
          } 

         } 

        } 
        for (int i = 0; i < 2; i++) { 

         if (gameGrid[2][i].getText() == gameGrid[0][i].getText() 
           && gameGrid[0][i].getText() == "X") { 

          if (gameGrid[1][i].getText() == "") { 
           gameGrid[1][i].setText("O"); 

           return; 
          } 

         } 

        } 


        // check if you can block a win horizontally 
        for (int i = 0; i < 2; i++) { 

         if (gameGrid[i][0].getText() == gameGrid[i][1].getText() 
           && gameGrid[i][0].getText() == "X") { 

          if (gameGrid[i][2].getText() == "") { 
           gameGrid[i][2].setText("O"); 

           return; 
          } 

         } 

        } 

        for (int i = 0; i < 2; i++) { 

         if (gameGrid[i][2].getText() == gameGrid[i][1].getText() 
           && gameGrid[i][2].getText() == "X") { 

          if (gameGrid[i][0].getText() == "") { 
           gameGrid[i][0].setText("O"); 

           return; 
          } 

         } 

        } 
        for (int i = 0; i < 2; i++) { 

         if (gameGrid[i][2].getText() == gameGrid[i][0].getText() 
           && gameGrid[i][0].getText() == "X") { 

          if (gameGrid[i][1].getText() == "") { 
           gameGrid[i][1].setText("O"); 

           return; 
          } 

         } 

        } 


        // check if you can block a win diagonally 


        if (gameGrid[0][0].getText() == gameGrid[1][1].getText() 
          && gameGrid[0][0].getText() == "X") { 

         if (gameGrid[2][2].getText() == "") { 
          gameGrid[2][2].setText("O"); 

          return; 
         } 
        } 

        if (gameGrid[2][2].getText() == gameGrid[1][1].getText() 
          && gameGrid[2][2].getText() == "X") { 

         if (gameGrid[0][0].getText() == "") { 
          gameGrid[0][0].setText("O"); 

          return; 
         } 
        } 


        if (gameGrid[0][2].getText() == gameGrid[1][1].getText() 
          && gameGrid[0][2].getText() == "X") { 

         if (gameGrid[2][0].getText() == "") { 
          gameGrid[2][0].setText("O"); 

          return; 
         } 
        } 

        if (gameGrid[2][0].getText() == gameGrid[1][1].getText() 
          && gameGrid[2][0].getText() == "X") { 

         if (gameGrid[0][2].getText() == "") { 
          gameGrid[0][2].setText("O"); 

          return; 
         } 
        } 
        if (gameGrid[2][0].getText() == gameGrid[0][2].getText() && 
          gameGrid[2][0].getText() == "X") { 
         if (gameGrid[1][1].getText() == "") { 
          gameGrid[1][1].setText("O"); 

          return; 
         } 
        } 
        if (gameGrid[0][2].getText() == gameGrid[2][0].getText() && 
          gameGrid[0][2].getText() == "X") { 
         if (gameGrid[1][1].getText() == "") { 
          gameGrid[1][1].setText("O"); 

          return; 
         } 
        } 
        if (gameGrid[0][0].getText() == gameGrid[2][2].getText() && 
          gameGrid[0][0].getText() == "X") { 
         if (gameGrid[1][1].getText() == "") { 
          gameGrid[1][1].setText("O"); 

          return; 
         } 
        } 
        if (gameGrid[2][2].getText() == gameGrid[0][0].getText() && 
          gameGrid[2][2].getText() == "X") { 
         if (gameGrid[1][1].getText() == "") { 
          gameGrid[1][1].setText("O"); 

          return; 
         } 
        } 

        // make random move if above rules dont apply 
        ArrayList random = new ArrayList(); 
        random.add("A"); 
        random.add("B"); 
        random.add("C"); 
        random.add("D"); 
        random.add("E"); 
        random.add("F"); 
        random.add("G"); 
        random.add("H"); 
        random.add("I"); 
        String a = ""; 
        for (int x = 0; x < gameGrid.length; x++) { 
         for (int y = 0; y < gameGrid[x].length; y++) { 
          if (gameGrid[x][y].getText() == "X" || gameGrid[x][y].getText() == "O") { 
           String z = x + "," + y; 

           if (z.equals("0,0")) { 
            a = "A"; 

           } else if (z.equals("0,1")) { 
            a = "B"; 

           } else if (z.equals("0,2")) { 
            a = "C"; 

           } else if (z.equals("1,0")) { 
            a = "D"; 

           } else if (z.equals("1,1")) { 
            a = "E"; 

           } else if (z.equals("1,2")) { 
            a = "F"; 

           } else if (z.equals("2,0")) { 
            a = "G"; 

           } else if (z.equals("2,1")) { 
            a = "H"; 

           } else if (z.equals("2,2")) { 
            a = "I"; 

           } else { 
            displayOut.setText("Error in switch 1"); 
           } 
           for (int i = 0; i < random.size(); i++) { 
            String b = (String) random.get(i); 
            if (b == a) { 
             random.remove(a); 
            } 
           } 
          } 
         } 
        } 
        ArrayList nRand = new ArrayList(); 
        nRand = (ArrayList) random.clone(); 
        if (nRand.size() <= 0) { 
         displayOut.setText("Game ends"); 
         endGame(); 
         return; 
        } 
        int index = ran.nextInt(nRand.size()); 
        String num = (String) nRand.get(index); 

        if (num.equals("A")) { 
         gameGrid[0][0].setText("O"); 
         gameGrid[0][0].setClickable(false); 

        } else if (num.equals("B")) { 
         gameGrid[0][1].setText("O"); 
         gameGrid[0][1].setClickable(false); 

        } else if (num.equals("C")) { 
         gameGrid[0][2].setText("O"); 
         gameGrid[0][2].setClickable(false); 

        } else if (num.equals("D")) { 
         gameGrid[1][0].setText("O"); 
         gameGrid[1][0].setClickable(false); 

        } else if (num.equals("E")) { 
         gameGrid[1][1].setText("O"); 
         gameGrid[1][1].setClickable(false); 

        } else if (num.equals("F")) { 
         gameGrid[1][2].setText("O"); 
         gameGrid[1][2].setClickable(false); 

        } else if (num.equals("G")) { 
         gameGrid[2][0].setText("O"); 
         gameGrid[2][0].setClickable(false); 

        } else if (num.equals("H")) { 
         gameGrid[2][1].setText("O"); 
         gameGrid[2][1].setClickable(false); 

        } else if (num.equals("I")) { 
         gameGrid[2][2].setText("O"); 
         gameGrid[2][2].setClickable(false); 

        } else { 
         displayOut.setText("Error in switch2"); 
        } 
       } finally { 
//ends here 
        c = checkWinner(); 
        count--; 
        if (c == true) { 
         displayOut.setText("Sorry, computer wins"); 
         endGame(); 
        } 
        if (c == false && count == 0) { 
         displayOut.setText("Sorry, game ends in a tie"); 
         endGame(); 
        } 
        for (int x = 0; x < gameGrid.length; x++) { 
         for (int y = 0; y < gameGrid[x].length; y++) { 
          if (gameGrid[x][y].getText() == "X") { 
           gameGrid[x][y].setClickable(false); 
          } 
          if (gameGrid[x][y].getText() == "O") { 
           gameGrid[x][y].setClickable(false); 
          } 
         } 
        } 
        displayOut.setText("It's Player X's turn."); 
       } 
      } 
     }, 2000); 
    } 

    private boolean checkWinner() { 

     for (int i = 0; i < gameGrid.length; i++) { 

      //vertical win 
      if (gameGrid[i][0].getText()==gameGrid[i][1].getText() && 
        gameGrid[i][1].getText()==gameGrid[i][2].getText() && 
        gameGrid[i][1].getText()!= "") 
      { 

       return true; 
      } 
      //horizontal win(may need its own for loop) 
      if (gameGrid[0][i].getText()==gameGrid[1][i].getText() && 
        gameGrid[1][i].getText()==gameGrid[2][i].getText() 
        && gameGrid[1][i].getText() != "") 
      { 

       return true; 
      } 

     } 
     //diagonal win 
     if (gameGrid[1][1].getText()==gameGrid[0][0].getText() && 
       gameGrid[1][1].getText()==gameGrid[2][2].getText() 
       && gameGrid[1][1].getText() != ""|| 

       gameGrid[2][0].getText()==gameGrid[1][1].getText()&& 
         gameGrid[1][1].getText()==gameGrid[0][2].getText() 
         &&gameGrid[1][1].getText()!="") 
     { 

      return true; 
     } 

     else { 
      return false; 
     } 
    } 

    public void endGame() { 
     for (int x = 0; x < gameGrid.length; x++) 
     { 
      for (int y = 0; y < gameGrid[x].length; y++) 
      { 
       gameGrid[x][y].setClickable(false); 
      } 
     } 
    } 

} 
+0

こんにちは言ったようにonSaveInstanceState(バンドルsavedInstanceState) とonRestoreInstanceState(バンドルsavedInstanceState)をオーバーライドすることで、スタックオーバーフローを歓迎する、通過する時間がかかるしてください[ツアーを歓迎]ここであなたのやり方を知るために(そして最初のバッジを獲得するため)、[最小限の、完全で、証明可能な例を作成する方法を読む](https://stackoverflow.com/tour) help/mcve)、また、[How to Ask Questions(How to Ask Good Questions)](https://stackoverflow.com/help/how-to-ask)もチェックして、フィードバックや役に立つ回答を得るチャンスを増やしてください。あまりにも多くのコードを投稿しているので、多くのユーザーがそれをすべて読んで興味を持っているわけではありません。 – DarkCygnus

答えて

0

まず、あなたのような人が尋ねる投稿がたくさんあるので、検索してみてください。

私はあなたがあなたの活動で何かについての情報を保存したい場合、あなたはのonCreateを(呼び出す必要がアクティビティのライフサイクル https://developer.android.com/guide/components/activities/activity-lifecycle.html を参照することをお勧め)、ONSTART()、onResume()、onPause()、onStop() 、onDestroy()はユーザーが行った操作(閉じる、アプリケーションの変更など)によって異なります。

上記の例が表示されていれば、これはまさにあなたが望むものです。

// recovering the instance state 
    if (savedInstanceState != null) { 
     mGameState = savedInstanceState.getString(GAME_STATE_KEY); 
    } 

ですから、onDestroy(状態を保存する必要があります)::のonCreate()の例では、あなたの場合は

// invoked when the activity may be temporarily destroyed, save the instance state here 
@Override 
public void onSaveInstanceState(Bundle outState) { 
    outState.putString(GAME_STATE_KEY, mGameState); 
    outState.putString(TEXT_VIEW_KEY, mTextView.getText()); 

    // call superclass to save any view hierarchy 
    super.onSaveInstanceState(outState); 
} 

あなたはgamegridを保存する必要があります。

その他の方法は、レトマイヤーが Saving Android Activity state using Save Instance State

関連する問題