2016-12-23 3 views
-1

Я делаю приложение Tic Tac Toe. Я уже сделал это с заявлением «выиграть», но я застрял в заявлении о розыгрыше.Tic-Tac-Toe Draw для Android Studio

Ниже приведены мои коды.

public class MainActivity extends AppCompatActivity { 

    int activePlayer = 0; // 0 for red 
    int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2}; // 2 means unplayed. 
    int[][] winningLocations = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {0, 4, 8}, 
      {2, 4, 6}}; 
    boolean gameOver = false; 

    public void gameLogic(View view) { 

     ImageView tappedView = (ImageView) view; 

     int tappedLocation = Integer.parseInt(view.getTag().toString()); 

     if (gameState[tappedLocation] == 2 && !gameOver) { 

      gameState[tappedLocation] = activePlayer; 

      tappedView.setTranslationY(-3000f); 

      if (activePlayer == 0) { 

       tappedView.setImageResource(R.drawable.red); 
       activePlayer = 1; 

      } else if (activePlayer == 1) { 

       tappedView.setImageResource(R.drawable.yellow); 
       activePlayer = 0; 

      } 


     tappedView.animate().translationYBy(3000f).setDuration(500); 

     String msg = ""; 

     for (int[] winningPosition : winningLocations) { 

      if (gameState[winningPosition[0]] == gameState[winningPosition[1]] 
        && gameState[winningPosition[1]] == gameState[winningPosition[2]] 
        && gameState[winningPosition[0]] != 2) { 

       if (activePlayer == 0) 
        msg = "Yellow is Winner!"; 

       if (activePlayer == 1) 
        msg = "Red is Winner!"; 


       LinearLayout winnerLayout = (LinearLayout) findViewById(R.id.winnerLayout); 
       winnerLayout.setVisibility(View.VISIBLE); 

       TextView winnerMsg = (TextView) findViewById(R.id.textView); 
       winnerMsg.setText(msg); 

       gameOver = true; 
      } 
     } 
    } 

} 

    public void playAgain(View view){ 

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

     gameOver = false; 
     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); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

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

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
    } 
} 
+0

Проверить эту ссылку: HTTP: //www.sourcecodester.com/tutorials/java/7875/creating-simple-tic-tac-toe-game-android.html – HsRaja

+0

является GameLogic() вызывается через атрибут OnClick в ваш xml? – dumazy

ответ

1

Предполагая ничью в крестики-нолики это все 9 квадратов, заполненные строки не с 3, я бы поставил что-то вроде этого после for цикла, который проверяет наличие выигрышной условия:

boolean emptySquare = false; 
for (int squareState : gameState) { 
    if (squareState == 2) { 
    emptySquare = true; 
    break; 
    } 
} 

if (!emptySquare && !gameOver) { 
    // Game is a draw 
    gameOver = true; 
    // Set draw message here... 
}