-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
213 lines (199 loc) · 6.93 KB
/
TicTacToe.java
File metadata and controls
213 lines (199 loc) · 6.93 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
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
/*
* This class creates the board and controls the logic of the game
*/
public class TicTacToe implements ActionListener {
Random turnRandomizer = new Random(); //creates a object to randomize which player goes first
JFrame frame = new JFrame(); //creates a frame for the board
JPanel titlePanel = new JPanel(); //creates the title panel
JPanel infoPanel = new JPanel(); //creates the info panel
JPanel buttonsPanel = new JPanel(); //creates the buttons panel
JLabel textField = new JLabel(); //creates the title text field
JLabel infoTextField = new JLabel(); //creates the info text field
JButton[] buttons = new JButton[10]; //creates buttons for the game and to reset
boolean playerTurn; //controls players turns true=X-turn / false=O-turn
//this constructor calls the method to build the board and set first player turn
TicTacToe (){
//Initializes player turn variable to false
playerTurn = false;
//call to the method to build the board for the game
buildBoard();
//call turn method to find out which player goes first
turn();
}
//this method creates the complete board by initializing and setting all elements above
public void buildBoard() {
//build frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,400);
frame.getContentPane().setBackground(new Color(0, 0, 0));
frame.setLayout(new BorderLayout());
//build title text Field
textField.setBackground(new Color(0, 0, 0));
textField.setForeground(new Color(20, 153, 242));
textField.setFont(new Font("Serif", Font.BOLD, 45));
textField.setHorizontalAlignment(JLabel.CENTER);
textField.setText("Tic Tac Toe");
textField.setOpaque(true);
//build title panel
titlePanel.setLayout(new BorderLayout());
titlePanel.setBounds(0, 0, 400, 100);
titlePanel.add(textField);
//build info text field
infoTextField.setBackground(new Color(0, 0, 0));
infoTextField.setForeground(new Color(20, 153, 242));
infoTextField.setFont(new Font("Serif", Font.BOLD, 45));
infoTextField.setText("START");
infoTextField.setHorizontalAlignment(JLabel.CENTER);
infoTextField.setOpaque(true);
//build info panel
infoPanel.setLayout(new BorderLayout());
infoPanel.setBounds(0, 0, 400, 100);
infoPanel.add(infoTextField, BorderLayout.NORTH);
//build X/O buttons
for(int i=0;i<9;i++) {
buttons[i] = new JButton();
buttonsPanel.add(buttons[i]);
buttons[i].setFont(new Font("Serif", Font.BOLD, 70));
buttons[i].setText("");
buttons[i].setFocusable(false);
buttons[i].addActionListener(this);
}
//build x/o buttons panel
buttonsPanel.setLayout(new GridLayout(3,3));
buttonsPanel.setBackground(new Color(0, 34, 150));
//build reset button
buttons[9] = new JButton();
buttons[9].setFont(new Font("Serif", Font.BOLD, 20));
buttons[9].setFocusable(false);
buttons[9].addActionListener(this);
buttons[9].setForeground(new Color(255, 0, 0));
buttons[9].setText("RESET");
//add all panels and elements to frame
infoPanel.add(buttons[9], BorderLayout.SOUTH);
frame.add(titlePanel, BorderLayout.NORTH);
frame.add(infoPanel, BorderLayout.SOUTH);
frame.add(buttonsPanel);
frame.setVisible(true);
}
//this method controls the what happens after the buttons are clicked
@Override
public void actionPerformed(ActionEvent e) {
int count; //counter to iterate over buttons
//this for loop controls the button clicks for O and X
for(count = 0; count < 9; count++) {
if(e.getSource() == buttons[count]) {
//sets the button with X for player choice
if(playerTurn) {
if(buttons[count].getText() == ""){
buttons[count].setForeground(new Color(255, 0, 0));
buttons[count].setText("X");
playerTurn = false;
infoTextField.setText("O's Turn");
check(); //calls check method to check if player won
}
}
//sets the button with O for player choice
else {
if(buttons[count].getText() == ""){
buttons[count].setForeground(new Color(0, 0, 255));
buttons[count].setText("O");
playerTurn = true;
infoTextField.setText("X's Turn");
check(); //calls check method to check if player won
}
}
}
}
//controls the action for the reset button by reseting the board
if(e.getSource() == buttons[9]) {
for(count = 0; count < 9; count++) {
buttons[count].setBackground(new Color(0, 34, 150));
buttons[count].setOpaque(false);
buttons[count].setText("");
buttons[count].setEnabled(true);
}
count = 0; //resets counter 0 for new game
turn(); //calls method to figure out who goes first
}
}
//method figures out player turn at random
public void turn() {
//delays the start of the game by 1 second to display "start" in info panel then the player turn
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//sets info field and player turn based on random generated number
if(turnRandomizer.nextInt(2) == 0) {
playerTurn = true;
infoTextField.setText("X's Turn");
}
else {
playerTurn = false;
infoTextField.setText("O's Turn");
}
}
//method is used to create a reference point for the aspect point cut
public boolean check() {
return true;
}
//this method updates the board if there is a draw
public void draw() {
int count = 0; //counter to iterate buttons
boolean filled = false; //checks if all buttons are clicked
//loop iterates to check button status
for(count = 0; count < 9; count++) {
if(buttons[count].getText() == "") {
filled = false;
count = 9;
}
else {
filled = true;
}
}
//if all buttons are pressed and no win then draw is declared and game is reset
if(infoTextField.getText() != "X WINS" && infoTextField.getText() != "O WINS" && filled) {
for(int i = 0; i < 9; i++) {
buttons[i].setEnabled(false);
}
infoTextField.setText("DRAW!");
}
}
//method updates board if x wins
public void xWins(int x, int y, int z) {
//sets winning buttons background color to green
buttons[x].setBackground(new Color(0, 255, 0));
buttons[y].setBackground(new Color(0, 255, 0));
buttons[z].setBackground(new Color(0, 255, 0));
buttons[x].setOpaque(true);
buttons[y].setOpaque(true);
buttons[z].setOpaque(true);
frame.setVisible(true);
//sets buttons to inactive
for(int i = 0; i < 9; i++) {
buttons[i].setEnabled(false);
}
infoTextField.setText("X WINS");
}
//method updates board if O wins
public void oWins(int x, int y, int z) {
//sets winning buttons background color to green
buttons[x].setBackground(new Color(0, 255, 0));
buttons[y].setBackground(new Color(0, 255, 0));
buttons[z].setBackground(new Color(0, 255, 0));
buttons[x].setOpaque(true);
buttons[y].setOpaque(true);
buttons[z].setOpaque(true);
frame.setVisible(true);
//sets buttons to inactive
for(int i = 0; i < 9; i++) {
buttons[i].setEnabled(false);
}
infoTextField.setText("O WINS");
}
}