-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.java
110 lines (95 loc) · 3.59 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.example.connect3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import androidx.gridlayout.widget.GridLayout;;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// 0=yellow, 1=red
int activePlayer = 0;
//game is active or not
boolean gameIsActive=true;
// 2 means unplayed
int[] gameState = {2,2,2,2,2,2,2,2,2};
int [][] winnningPositions={{0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}};
public void dropIn(View view)
{
ImageView counter = (ImageView) view;
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter]==2 && gameIsActive)
{
gameState[tappedCounter]=activePlayer;
counter.setTranslationY(-1000f);
if(activePlayer==0)
{
counter.setImageResource(R.drawable.yellow);
activePlayer=1;
}
else
{
counter.setImageResource(R.drawable.red);
activePlayer=0;
}
counter.animate().translationYBy(1000f).rotation(360).setDuration(300);
for (int[] winningPosition: winnningPositions)
{
if (gameState[winningPosition[0]]==gameState[winningPosition[1]]&&gameState[winningPosition[1]]==gameState[winningPosition[2]]
&&gameState[winningPosition[0]]!=2)
{
//someone has won
gameIsActive=false;
String winner = "Red";
if (gameState[winningPosition[0]]==0)
{
winner="Yellow";
}
TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
winnerMessage.setText(winner + " has won!");
LinearLayout layout = (LinearLayout)findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE);
}
else
{
boolean gameIsOver = true;
for (int counterState : gameState)
{
if (counterState == 2)
{
gameIsOver=false;
}
}
if (gameIsOver)
{
TextView winnerMessage = (TextView) findViewById(R.id.winnerMessage);
winnerMessage.setText("Draw !");
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);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}