-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.java
More file actions
207 lines (184 loc) · 6.81 KB
/
tictactoe.java
File metadata and controls
207 lines (184 loc) · 6.81 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
package com.example.findme_technovation;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
import java.util.ArrayList;
import java.util.List;
/**
* THE USER WILL ALWAYS BE "X," AND THE COMPUTER WILL ALWAYS BE "O"
* Computer iwll randomly come up with moves based on math.Random
*/
public class tictactoe extends GameActivity{
Button back; // back button
Button b1; // all the possible buttons
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
Button b9;
boolean userTurn; // boolean checking if its currentlty the users turns
boolean computerTurn; // literally the opposite
int enabledCount; // count ot check if the board is full
TextView turn; // turn
List<Button> buttons; // List to hold buttons
int count;
public tictactoe(){ // initalize all of the values
userTurn = true;
computerTurn = false;
enabledCount = 0;
}
@SuppressLint("MissingInflatedId")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tictactoe);
back = findViewById(R.id.backButtonT);
b1 = findViewById(R.id.b1);// initalize all of hte ubtons
b2 = findViewById(R.id.b2);
b3 = findViewById(R.id.b3);
b4 = findViewById(R.id.b4);
b5 = findViewById(R.id.b5);
b6 = findViewById(R.id.b6);
b7 = findViewById(R.id.b7);
b8 = findViewById(R.id.b8);
b9 = findViewById(R.id.b9);
turn = findViewById(R.id.turn);
buttons = new ArrayList<>(); // add em to the arrayList
buttons.add(b1);
buttons.add(b2);
buttons.add(b3);
buttons.add(b4);
buttons.add(b5);
buttons.add(b6);
buttons.add(b7);
buttons.add(b8);
buttons.add(b9);
back = findViewById(R.id.backButtonT);
back.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
startActivity(new Intent(tictactoe.this, GameActivity.class));
}
});
for (Button button : buttons) {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (userTurn) {
System.out.println("ENTERED");
// hand the user move and set the text of button to x or o
// gotta come up with lgoic
button.setText("X");
System.out.println("your move disabled");
button.setEnabled(false); // disalbe the button
enabledCount ++;
// check if user wins or if board is full and if its not
// then its the pcs turns
if (!checkForWin() && !isBoardFull()) {
computerTurn = true;
userTurn = false;
makeComputerMove();
}
}
}
});
}
}
public void makeComputerMove() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Random random = new Random();
// choose a random button
Button randomButton;
do {
int index = random.nextInt(buttons.size()); // 0 to list size
randomButton = buttons.get(index); // get button from that spot
} while (!randomButton.getText().toString().isEmpty()); // keep going until empty found
// set text to x or o based off logic
randomButton.setText("O");
randomButton.setEnabled(false); // disable it
System.out.println("pc disabled");
enabledCount++;
// check if computer wins or if board is full and if it's not then it's the user's turn
if (!checkForWin() && !isBoardFull()) {
computerTurn = false;
userTurn = true;
}
}
}, 700); // 1000 milliseconds = 1 second delay
}
public boolean checkForWin() {
int[][] winCombinations = {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, // teh rwos
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, // the cols
{0, 4, 8}, {2, 4, 6} // the diaonls
};
// go through each combo in the win combinations
for (int[] combination : winCombinations) {
Button b1 = buttons.get(combination[0]); // get the ghree buttons from teh combos
Button b2 = buttons.get(combination[1]);
Button b3 = buttons.get(combination[2]);
if (!b1.getText().toString().isEmpty() && b1.getText().equals(b2.getText()) && b2.getText().equals(b3.getText())) {
if(b1.getText().equals("X")){
updatePlayerWinsCount();
}
else{
System.out.println("pc wins");
}
makeRed(b1, b2, b3);
resetBoardWithDelay();
return true; // return true
}
}
return false; // Return false if no winning combination is found
}
// Optional method to highlight the winning combination
public void makeRed(Button b1, Button b2, Button b3) {
b1.setTextColor(Color.RED);
b2.setTextColor(Color.RED);
b3.setTextColor(Color.RED);
}
public boolean isBoardFull() {
// do this too idk
if(enabledCount >= 9){
System.out.println("tie");
resetBoardWithDelay();
return true;
}
return false;
}
public void resetBoard(){
for (Button button : buttons) {
System.out.println("dog");
button.setText("");
button.setEnabled(true);
System.out.println("has it been");
button.setTextColor(Color.WHITE);
userTurn = true;
computerTurn = false;
}
enabledCount = 0;
}
public void resetBoardWithDelay() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
resetBoard(); // Reset the board
}
}, 700); // Delay in milliseconds (2000 milliseconds = 2 seconds)
}
public void updatePlayerWinsCount() {
count++;
turn.setText("Your wins: " + count);
}
}