Advertisement
Guest User

Untitled

a guest
May 26th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. package com.jsoft.connectthree;
  2.  
  3. import android.provider.Settings;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.GridLayout;
  8. import android.widget.ImageView;
  9. import android.widget.LinearLayout;
  10. import android.widget.TextView;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14.     // 0 = yellow; 1 = red
  15.      int activePlayer = 0;
  16.  
  17.     //2 = unplayed
  18.     int[] gameState={2, 2, 2, 2, 2, 2, 2, 2, 2};
  19.     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}};
  20.  
  21.     public void dropIn(View view) {
  22.  
  23.         ImageView counter = (ImageView) view;
  24.  
  25.         //System.out.println(counter.getTag().toString());
  26.  
  27.         int tappedCounter = Integer.parseInt(counter.getTag().toString());
  28.  
  29.         if (gameState[tappedCounter] == 2) {
  30.  
  31.             gameState[tappedCounter] = activePlayer;
  32.  
  33.  
  34.             counter.setTranslationY(-1000f);
  35.             if (activePlayer == 0) {
  36.  
  37.                 counter.setImageResource(R.drawable.yellow);
  38.  
  39.                 activePlayer = 1;
  40.             } else {
  41.  
  42.                 counter.setImageResource(R.drawable.red);
  43.  
  44.                 activePlayer = 0;
  45.  
  46.             }
  47.  
  48.  
  49.             counter.animate().translationYBy(1000f).rotation(360f).setDuration(1500);
  50.  
  51.             for(int[]winningPosition : winningPositions){
  52.  
  53.                 if (gameState[winningPosition[0]]== gameState[winningPosition[1]]&&
  54.                         gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
  55.                         gameState[winningPosition[0]] !=2){
  56.  
  57.                     //Someone Wins!
  58.                     String winner = "Red";
  59.  
  60.                     if(gameState[winningPosition[0]]== 0){
  61.                         winner = "Yellow";
  62.  
  63.                     };
  64.  
  65.  
  66.                     TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
  67.                     winnerMessage.setText(winner + " has won!");
  68.  
  69.  
  70.                     LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
  71.                     layout.setVisibility(View.VISIBLE);
  72.                 }
  73.  
  74.  
  75.  
  76.             }
  77.  
  78.         }
  79.     }
  80.  
  81.     public void playAgain(){
  82.  
  83.         LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
  84.  
  85.         layout.setVisibility(View.INVISIBLE);
  86.         // 0 = yellow; 1 = red
  87.          activePlayer = 0;
  88.  
  89.         for(int i =0; i < gameState.length; i++)
  90.  
  91.             gameState[i]=2;
  92.  
  93.     }
  94.  
  95.     GridLayout gridLayout = (GridLayout)findViewById(R.id.grid);
  96.     int count = gridLayout.getChildCount();
  97.     for(int i = 0; i<gridLayout.getChildCount(); i++){
  98.  
  99.         System.out.println("WIi");
  100.     }
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.     @Override
  108.     protected void onCreate(Bundle savedInstanceState) {
  109.         super.onCreate(savedInstanceState);
  110.         setContentView(R.layout.activity_main);
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement