Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- int activePlayer = 0;
- boolean gameIsActive = true;
- 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}};
- private GoogleApiClient client;
- public void dropIn(View view) {
- ImageView counter = (ImageView) view;
- System.out.println(counter.getTag().toString());
- int tappeCounter = Integer.parseInt(counter.getTag().toString());
- if (gameState[tappeCounter] == 2 && gameIsActive) {
- boolean gameIsOver = true;
- for (int counterState : gameState) {
- if (counterState == 2) gameIsOver = false;
- }
- gameState[tappeCounter] = activePlayer;
- counter.setTranslationY(-1000f);
- if (activePlayer == 0) {
- counter.setImageResource(R.drawable.off);
- activePlayer = 1;
- } else {
- counter.setImageResource(R.drawable.deff);
- activePlayer = 0;
- }
- counter.animate().translationYBy(1000).rotation(3600).setDuration(400);
- for (int[] winningPosition : winningPositions) {
- if (gameState[winningPosition[0]] == gameState[winningPosition[1]]
- && gameState[winningPosition[1]] == gameState[winningPosition[2]]
- && gameState[winningPosition[0]] != 2) {
- String winner = "Deff´s";
- if (gameState[winningPosition[0]] == 0) {
- winner = "Off´s";
- }
- TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
- winnerMessage.setText(winner + " haben gewonnen!");
- System.out.println(gameState[winningPosition[0]]);
- LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
- layout.setVisibility(View.VISIBLE);
- gameIsActive = false;
- } else {
- if (gameIsOver) {
- TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
- winnerMessage.setText("Offenes Tor!");
- LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
- layout.setVisibility(View.VISIBLE);
- }
- }
- }
- }
- }
- public void playAgain(View view) {
- gameIsActive = true;
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement