|
| 1 | +package com.robpercival.connect3; |
| 2 | + |
| 3 | +import android.support.v7.app.ActionBarActivity; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.view.Menu; |
| 6 | +import android.view.MenuItem; |
| 7 | +import android.view.View; |
| 8 | +import android.widget.GridLayout; |
| 9 | +import android.widget.ImageView; |
| 10 | +import android.widget.LinearLayout; |
| 11 | +import android.widget.TextView; |
| 12 | + |
| 13 | + |
| 14 | +public class MainActivity extends ActionBarActivity { |
| 15 | + |
| 16 | + // 0 = yellow, 1 = red |
| 17 | + |
| 18 | + int activePlayer = 0; |
| 19 | + |
| 20 | + boolean gameIsActive = true; |
| 21 | + |
| 22 | + // 2 means unplayed |
| 23 | + |
| 24 | + int[] gameState = {2, 2, 2, 2, 2, 2, 2, 2, 2}; |
| 25 | + |
| 26 | + 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}}; |
| 27 | + |
| 28 | + public void dropIn(View view) { |
| 29 | + |
| 30 | + ImageView counter = (ImageView) view; |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + int tappedCounter = Integer.parseInt(counter.getTag().toString()); |
| 35 | + |
| 36 | + if (gameState[tappedCounter] == 2 && gameIsActive) { |
| 37 | + |
| 38 | + gameState[tappedCounter] = activePlayer; |
| 39 | + |
| 40 | + counter.setTranslationY(-1000f); |
| 41 | + |
| 42 | + if (activePlayer == 0) { |
| 43 | + |
| 44 | + counter.setImageResource(R.drawable.yellow); |
| 45 | + |
| 46 | + activePlayer = 1; |
| 47 | + |
| 48 | + } else { |
| 49 | + |
| 50 | + counter.setImageResource(R.drawable.red); |
| 51 | + |
| 52 | + activePlayer = 0; |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + counter.animate().translationYBy(1000f).rotation(360).setDuration(300); |
| 57 | + |
| 58 | + for (int[] winningPosition : winningPositions) { |
| 59 | + |
| 60 | + if (gameState[winningPosition[0]] == gameState[winningPosition[1]] && |
| 61 | + gameState[winningPosition[1]] == gameState[winningPosition[2]] && |
| 62 | + gameState[winningPosition[0]] != 2) { |
| 63 | + |
| 64 | + // Someone has won! |
| 65 | + |
| 66 | + gameIsActive = false; |
| 67 | + |
| 68 | + String winner = "Red"; |
| 69 | + |
| 70 | + if (gameState[winningPosition[0]] == 0) { |
| 71 | + |
| 72 | + winner = "Yellow"; |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage); |
| 77 | + |
| 78 | + winnerMessage.setText(winner + " has won!"); |
| 79 | + |
| 80 | + LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout); |
| 81 | + |
| 82 | + layout.setVisibility(View.VISIBLE); |
| 83 | + |
| 84 | + } else { |
| 85 | + |
| 86 | + boolean gameIsOver = true; |
| 87 | + |
| 88 | + for (int counterState : gameState) { |
| 89 | + |
| 90 | + if (counterState == 2) gameIsOver = false; |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + if (gameIsOver) { |
| 95 | + |
| 96 | + TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage); |
| 97 | + |
| 98 | + winnerMessage.setText("It's a draw"); |
| 99 | + |
| 100 | + LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout); |
| 101 | + |
| 102 | + layout.setVisibility(View.VISIBLE); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + public void playAgain(View view) { |
| 115 | + |
| 116 | + gameIsActive = true; |
| 117 | + |
| 118 | + LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout); |
| 119 | + |
| 120 | + layout.setVisibility(View.INVISIBLE); |
| 121 | + |
| 122 | + activePlayer = 0; |
| 123 | + |
| 124 | + for (int i = 0; i < gameState.length; i++) { |
| 125 | + |
| 126 | + gameState[i] = 2; |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout); |
| 131 | + |
| 132 | + for (int i = 0; i< gridLayout.getChildCount(); i++) { |
| 133 | + |
| 134 | + ((ImageView) gridLayout.getChildAt(i)).setImageResource(0); |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + protected void onCreate(Bundle savedInstanceState) { |
| 142 | + super.onCreate(savedInstanceState); |
| 143 | + setContentView(R.layout.activity_main); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 148 | + // Inflate the menu; this adds items to the action bar if it is present. |
| 149 | + getMenuInflater().inflate(R.menu.menu_main, menu); |
| 150 | + return true; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 155 | + // Handle action bar item clicks here. The action bar will |
| 156 | + // automatically handle clicks on the Home/Up button, so long |
| 157 | + // as you specify a parent activity in AndroidManifest.xml. |
| 158 | + int id = item.getItemId(); |
| 159 | + |
| 160 | + //noinspection SimplifiableIfStatement |
| 161 | + if (id == R.id.action_settings) { |
| 162 | + return true; |
| 163 | + } |
| 164 | + |
| 165 | + return super.onOptionsItemSelected(item); |
| 166 | + } |
| 167 | +} |
0 commit comments