-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttt_part.java
308 lines (244 loc) · 8.45 KB
/
ttt_part.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package com.example.alarmpart;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class ttt_part extends AppCompatActivity implements View.OnClickListener {
private Button[][] buttons = new Button[3][3];
private boolean player1Turn = true;
private int cc=0;
private ArrayList<String> list = new ArrayList<String>(Arrays.asList("00","01","02","10","11","12","20","21","22"));
private int roundCount;
private int player1Points;
private int player2Points;
private TextView textViewPlayer1;
private TextView textViewPlayer2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ttt_part);
textViewPlayer1 = findViewById(R.id.text_view_p1);
textViewPlayer2 = findViewById(R.id.text_view_p2);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
String buttonID = "button_" + i + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = findViewById(resID);
buttons[i][j].setOnClickListener(this);
}
}
Button buttonReset = findViewById(R.id.button_reset);
buttonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resetGame();
}
});
}
@Override
public void onClick(View v) {
Button mute = (Button) findViewById(R.id.mute);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (!((Button) v).getText().toString().equals("")) {
return;
}
if (player1Turn) {
((Button) v).setText("X");
String s = v.getResources().getResourceName(v.getId());
list.remove(s);
cc++;
} else {
//((Button) v).setText("O");
cc++;
fillinempty();
randomize();
}
roundCount++;
if (checkForWin()) {
if (player1Turn) {
player1Wins();
if(player1Points>=3){
mute.setEnabled(true);
mute.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
audioManager.setRingerMode(1);
}
});
}else{
mute.setEnabled(false);
}
} else {
player2Wins();
if(player2Points>=3){
mute.setEnabled(true);
mute.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
audioManager.setRingerMode(1);
}
});
}else{
mute.setEnabled(false);
}
}
} else if (roundCount == 9) {
draw();
} else {
player1Turn = !player1Turn;
}
}
private boolean checkForWin() {
String[][] field = new String[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
field[i][j] = buttons[i][j].getText().toString();
}
}
for (int i = 0; i < 3; i++) {
if (field[i][0].equals(field[i][1])
&& field[i][0].equals(field[i][2])
&& !field[i][0].equals("")) {
return true;
}
}
for (int i = 0; i < 3; i++) {
if (field[0][i].equals(field[1][i])
&& field[0][i].equals(field[2][i])
&& !field[0][i].equals("")) {
return true;
}
}
if (field[0][0].equals(field[1][1])
&& field[0][0].equals(field[2][2])
&& !field[0][0].equals("")) {
return true;
}
if (field[0][2].equals(field[1][1])
&& field[0][2].equals(field[2][0])
&& !field[0][2].equals("")) {
return true;
}
return false;
}
private void player1Wins() {
player1Points++;
Toast.makeText(this, "Player 1 wins!", Toast.LENGTH_SHORT).show();
updatePointsText();
resetBoard();
list.add("00");
list.add("01");
list.add("02");
list.add("10");
list.add("11");
list.add("12");
list.add("20");
list.add("21");
list.add("22");
}
private void player2Wins() {
player2Points++;
Toast.makeText(this, "Player 2 wins!", Toast.LENGTH_SHORT).show();
updatePointsText();
resetBoard();
list.add("00");
list.add("01");
list.add("02");
list.add("10");
list.add("11");
list.add("12");
list.add("20");
list.add("21");
list.add("22");
}
private void draw() {
Toast.makeText(this, "Draw!", Toast.LENGTH_SHORT).show();
resetBoard();
list.add("00");
list.add("01");
list.add("02");
list.add("10");
list.add("11");
list.add("12");
list.add("20");
list.add("21");
list.add("22");
}
private void updatePointsText() {
textViewPlayer1.setText("Player 1: " + player1Points);
textViewPlayer2.setText("Player 2: " + player2Points);
}
private void resetBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].setText("");
}
}
roundCount = 0;
player1Turn = true;
}
private void resetGame(){
player1Points =0;
player2Points =0;
updatePointsText();
list.clear();
resetBoard();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("roundCount",roundCount);
outState.putInt("player1Points",player1Points);
outState.putInt("player2Points",player2Points);
outState.putBoolean("player1Turn",player1Turn);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
roundCount = savedInstanceState.getInt("roundCount");
player1Points = savedInstanceState.getInt("player1Points");
player2Points = savedInstanceState.getInt("player2Points");
}
public void randomize(){
String exit = "";
while(!exit.equals("exit")){
Random r = new Random();
int num1 = r.nextInt(2);
int num2 = r.nextInt(2);
String id = "button_"+num1+num2;
int resID = getResources().getIdentifier(id, "id", getPackageName());
Button b = (Button) findViewById(resID);
if(b.getText().toString().equals("")){
b.setText("O");
exit = "exit";
}
}
}
public void randomize2(){
String exit = "";
while(!exit.equals("exit")){
Random r = new Random();
int num1 = r.nextInt(list.size());
//int num2 = r.nextInt(9);
String s = list.get(num1);
String id = "button_"+s;
int resID = getResources().getIdentifier(id, "id", getPackageName());
Button b = (Button) findViewById(resID);
//if(b.getText().toString().equals("")){
b.setText("O");
list.remove(id);
exit = "exit";
//}
}
}
public void fillinempty(){
}
}