-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndroidTicTacToe.java
More file actions
353 lines (317 loc) · 11.3 KB
/
AndroidTicTacToe.java
File metadata and controls
353 lines (317 loc) · 11.3 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* This project is an android tic tac toe game, in which the user is X and can manually choose their move
and the device makes a move as O
By:Deyvik Bhan
Date: 2/11/19
*/
package com.example.dbhan.androidttt;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button[][] grid = new Button[3][3];
int[][] board = new int[3][3];
final int BLANK = 0;
final int X_MOVE = 1;
final int O_MOVE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Below gives each induvidual button a spot on the grid array
grid[0][0] = (Button)this.findViewById(R.id.button1);
grid[0][1] = (Button)this.findViewById(R.id.button2);
grid[0][2] = (Button)this.findViewById(R.id.button3);
grid[1][0] = (Button)this.findViewById(R.id.button4);
grid[1][1] = (Button)this.findViewById(R.id.button5);
grid[1][2] = (Button)this.findViewById(R.id.button6);
grid[2][0] = (Button)this.findViewById(R.id.button7);
grid[2][1] = (Button)this.findViewById(R.id.button8);
grid[2][2] = (Button)this.findViewById(R.id.button9);
// Below sets on click listener for each button
for(int x = 0; x < 3; x++) {
for(int y = 0; y < 3; y++) {
grid[x][y].setOnClickListener(this);
}
}
}
@Override
public void onClick( View i) {
Button b = (Button) i;
// Button input initialized
// Following code goes through grid array, and puts an x on the grid, if some conditions are met
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
if (b.equals(grid[x][y])) {
if (board[x][y] == BLANK) {
// Sets text, disables button, and records button as X_Move
b.setText("X");
b.setEnabled(false);
board[x][y] = X_MOVE;
// AI move if no one has won or there is no tie
if(!checkTie() && !checkWin(X_MOVE) && !checkWin(O_MOVE)) {
aiMove();
}
}
}
}
}
// If x won
if(checkWin(X_MOVE)) {
Toast.makeText(this,"X Won", Toast.LENGTH_SHORT).show();
clearBoard();
} // If it is a tie
else if(checkTie() && !checkWin(X_MOVE) && !checkWin(O_MOVE)) {
Toast.makeText(this, "Tie", Toast.LENGTH_SHORT).show();
clearBoard();
}
}
// Following function is responsble for ai move
public void aiMove() {
// Try to win
if(checkSingleBlank(O_MOVE)) {
// Tries to win before it tries to block
return;
}
// Try to block
if(checkSingleBlank(X_MOVE)) {
return;
}
//play randomly
// If there is no way to win, and there is nothing to block then just p;aces randomly
ArrayList<Integer> list = new ArrayList<Integer>();
for(int x = 0; x < 3; x++) {
for(int y = 0; y < 3; y++) {
if(board[x][y] == BLANK) {
list.add(x*10+y);
}
}
}
int choice = (int)(Math.random()*list.size());
board[list.get(choice) / 10][list.get(choice)%10] = O_MOVE;
grid[list.get(choice) / 10][list.get(choice)%10].setText("O");
grid[list.get(choice) / 10][list.get(choice)%10].setEnabled(false);
if(checkWin(O_MOVE)) {
// Checks if O won
Toast.makeText(this, "O Won", Toast.LENGTH_SHORT).show();
clearBoard();
} else if(checkTie() && !checkWin(X_MOVE) && !checkWin(O_MOVE)) {
Toast.makeText(this, "Tie", Toast.LENGTH_SHORT).show();
clearBoard();
}
}
// Checks if there is a single blank spot between two O's or two X's
public boolean checkSingleBlank(int player) {
int oCount = 0;
int blankCount = 0;
int blankX = 0;
int blankY = 0;
// Integers which track
// Checks columns wins
for(int x = 0; x < 3; x++) {
oCount = 0;
blankCount = 0;
blankX = 0;
blankY = 0;
for(int y = 0; y < 3; y++) {
if(board[x][y] == BLANK) {
blankCount++;
blankX = x;
blankY = y;
}
if(board[x][y] == player) {
oCount++;
}
}
if(oCount == 2 && blankCount == 1) {
board[blankX][blankY] = O_MOVE;
grid[blankX][blankY].setText("O");
grid[blankX][blankY].setEnabled(false);
if(checkWin(O_MOVE)) {
Toast.makeText(this, "O Won", Toast.LENGTH_SHORT).show();
clearBoard();
} else if(checkTie() && !checkWin(X_MOVE) && !checkWin(O_MOVE)) {
Toast.makeText(this, "Tie", Toast.LENGTH_SHORT).show();
clearBoard();
}
return true;
}
// Checks if there is a single space between two O's or X's in collumns
}
//Checks rows win
for(int y = 0; y < 3; y++) {
oCount = 0;
blankCount = 0;
blankX = 0;
blankY = 0;
for(int x = 0; x < 3; x++) {
if(board[x][y] == BLANK) {
blankCount++;
blankX = x;
blankY = y;
}
if(board[x][y] == player) {
oCount++;
}
}
if(oCount == 2 && blankCount == 1) {
board[blankX][blankY] = O_MOVE;
grid[blankX][blankY].setText("O");
grid[blankX][blankY].setEnabled(false);
if(checkWin(O_MOVE)) {
Toast.makeText(this, "O Won", Toast.LENGTH_SHORT).show();
clearBoard();
} else if(checkTie() && !checkWin(X_MOVE) && !checkWin(O_MOVE)) {
Toast.makeText(this, "Tie", Toast.LENGTH_SHORT).show();
clearBoard();
}
return true;
}
}
// Checks if there is a single space between two O's or X's in collumns
//Check for top left to bottom right diagnol
oCount = 0;
blankCount = 0;
blankX = 0;
blankY = 0;
if(board[0][0] == BLANK) {
blankCount++;
blankX = 0;
blankY = 0;
}
if(board[0][0] == player) {
oCount++;
}
if(board[1][1] == BLANK) {
blankCount++;
blankX = 1;
blankY = 1;
}
if(board[1][1] == player) {
oCount++;
}
if(board[2][2] == BLANK) {
blankCount++;
blankX = 2;
blankY = 2;
}
if(board[2][2] == player) {
oCount++;
}
if(oCount == 2 && blankCount == 1) {
board[blankX][blankY] = O_MOVE;
grid[blankX][blankY].setText("O");
grid[blankX][blankY].setEnabled(false);
if(checkWin(O_MOVE)) {
Toast.makeText(this, "O Won", Toast.LENGTH_SHORT).show();
clearBoard();
} else if(checkTie() && !checkWin(X_MOVE) && !checkWin(O_MOVE)) {
Toast.makeText(this, "Tie", Toast.LENGTH_SHORT).show();
clearBoard();
}
return true;
}
// Checks if there is a single space between two O's or X's in the left to right diagnol
// Top right to the bottom left
// Checks diagnol
oCount = 0;
blankCount = 0;
blankX = 0;
blankY = 0;
if(board[2][0] == BLANK) {
blankCount++;
blankX = 2;
blankY = 0;
}
if(board[2][0] == player) {
oCount++;
}
if(board[1][1] == BLANK) {
blankCount++;
blankX = 1;
blankY = 1;
}
if(board[1][1] == player) {
oCount++;
}
if(board[0][2] == BLANK) {
blankCount++;
blankX = 0;
blankY = 2;
}
if(board[0][2] == player) {
oCount++;
}
if(oCount == 2 && blankCount == 1) {
board[blankX][blankY] = O_MOVE;
grid[blankX][blankY].setText("O");
grid[blankX][blankY].setEnabled(false);
if(checkWin(O_MOVE)) {
Toast.makeText(this, "O Won", Toast.LENGTH_SHORT).show();
clearBoard();
} else if(checkTie() && !checkWin(X_MOVE) && !checkWin(O_MOVE)) {
Toast.makeText(this, "Tie", Toast.LENGTH_SHORT).show();
clearBoard();
}
return true;
}
return false;
}
// Checks if there is a single space between two O's or X's in the right to left diagnol
// Following code checks win
public boolean checkWin(int player) {
// Checks all the possible kinds of wins
if(board[0][0] == player && board[0][1] == player && board[0][2] == player) {
return true;
}
if(board[1][0] == player && board[1][1] == player && board[1][2] == player) {
return true;
}
if(board[2][0] == player && board[2][1] == player && board[2][2] == player) {
return true;
}
if(board[0][0] == player && board[1][0] == player && board[2][0] == player) {
return true;
}
if(board[0][1] == player && board[1][1] == player && board[2][1] == player) {
return true;
}
if(board[0][2] == player && board[1][2] == player && board[2][2] == player) {
return true;
}
if(board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true;
}
if(board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return true;
}
return false;
}
// Following function checks if there is a tie
public boolean checkTie() {
//Checks if all the possible spots are taken then declares it a tie
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
if(board[row][column] == BLANK) {
return false;
}
}
}
return true;
}
// Function below resets board
public void clearBoard() {
for(int a = 0; a < 3; a++) {
for(int c = 0; c < 3; c++) {
Button e = grid[a][c];
e.setText("");
e.setEnabled(true);
board[a][c] = BLANK;
}
}
}
}