Advertisement
Guest User

TicTacToe

a guest
Feb 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. int activePlayer = 0;
  4. boolean gameIsActive = true;
  5. int[] gameState ={2,2,2,2,2,2,2,2,2};
  6.  
  7. 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}};
  8.  
  9.  
  10.  
  11. private GoogleApiClient client;
  12.  
  13. public void dropIn(View view) {
  14.  
  15.  
  16. ImageView counter = (ImageView) view;
  17.  
  18. System.out.println(counter.getTag().toString());
  19.  
  20. int tappeCounter = Integer.parseInt(counter.getTag().toString());
  21. if (gameState[tappeCounter] == 2 && gameIsActive) {
  22. boolean gameIsOver = true;
  23. for (int counterState : gameState) {
  24. if (counterState == 2) gameIsOver = false;
  25. }
  26. gameState[tappeCounter] = activePlayer;
  27. counter.setTranslationY(-1000f);
  28.  
  29.  
  30. if (activePlayer == 0) {
  31. counter.setImageResource(R.drawable.off);
  32. activePlayer = 1;
  33.  
  34. } else {
  35. counter.setImageResource(R.drawable.deff);
  36. activePlayer = 0;
  37.  
  38. }
  39. counter.animate().translationYBy(1000).rotation(3600).setDuration(400);
  40.  
  41. for (int[] winningPosition : winningPositions) {
  42. if (gameState[winningPosition[0]] == gameState[winningPosition[1]]
  43. && gameState[winningPosition[1]] == gameState[winningPosition[2]]
  44. && gameState[winningPosition[0]] != 2) {
  45.  
  46. String winner = "Deff´s";
  47. if (gameState[winningPosition[0]] == 0) {
  48. winner = "Off´s";
  49. }
  50. TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
  51. winnerMessage.setText(winner + " haben gewonnen!");
  52. System.out.println(gameState[winningPosition[0]]);
  53.  
  54.  
  55. LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
  56. layout.setVisibility(View.VISIBLE);
  57.  
  58. gameIsActive = false;
  59.  
  60.  
  61. } else {
  62.  
  63. if (gameIsOver) {
  64. TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
  65. winnerMessage.setText("Offenes Tor!");
  66.  
  67. LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
  68. layout.setVisibility(View.VISIBLE);
  69.  
  70. }
  71. }
  72. }
  73. }
  74. }
  75. public void playAgain(View view) {
  76. gameIsActive = true;
  77. LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
  78. layout.setVisibility(View.INVISIBLE);
  79.  
  80. activePlayer = 0;
  81. for (int i = 0; i < gameState.length; i++) {
  82. gameState[i] = 2;
  83. }
  84. GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout);
  85.  
  86. for(int i = 0; i<gridLayout.getChildCount(); i++){
  87. ((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement