-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTic_Tac_Toe.java
More file actions
166 lines (149 loc) · 4.23 KB
/
Tic_Tac_Toe.java
File metadata and controls
166 lines (149 loc) · 4.23 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
package Game_development;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TicTacToe extends JFrame implements ActionListener {
public static int BOARD_SIZE = 3;
public static enum GameStatus {
Incomplete, XWins, ZWins, Tie
}
private JButton[][] buttons = new JButton[BOARD_SIZE][BOARD_SIZE];
boolean crossTurn = true;
public TicTacToe() {
super.setTitle("TicTacToe");
super.setSize(700, 700);
GridLayout gridLayout = new GridLayout(BOARD_SIZE, BOARD_SIZE);
super.setLayout(gridLayout);
Font font = new Font("Comic Sans", 1, 150);
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
JButton button = new JButton("");
buttons[row][col] = button;
button.setFont(font);
button.addActionListener(this);
super.add(button);
}
}
super.setResizable(false);
super.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton clickedButton = (JButton) e.getSource();
makeMove(clickedButton);
GameStatus gs = this.getGameStatus();
if (gs == GameStatus.Incomplete) {
return;
}
declareWinner(gs);
int choice = JOptionPane.showConfirmDialog(this, "Do you want to restart the Game?");
if (choice == JOptionPane.YES_OPTION) {
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
buttons[row][col].setText("");
}
}
crossTurn = true;
} else
super.dispose();
}
private void makeMove(JButton clickedButton) {
String btntext = clickedButton.getText();
if (btntext.length() > 0) {
JOptionPane.showMessageDialog(this, "Invalid Move");
} else {
if (crossTurn) {
clickedButton.setText("X");
} else {
clickedButton.setText("0");
}
crossTurn = !crossTurn;
}
}
private GameStatus getGameStatus() {
String text1 = "", text2 = "";
int row = 0, col = 0;
// text inside row
for (row = 0; row < BOARD_SIZE; row++) {
for (col = 0; col < BOARD_SIZE - 1; col++) {
text1 = buttons[row][col].getText();
text2 = buttons[row][col + 1].getText();
if (!text1.equals(text2) || text1.length() == 0) {
break;
}
}
if (col == BOARD_SIZE - 1) {
if (text1.equals("X"))
return GameStatus.XWins;
else
return GameStatus.ZWins;
}
}
// text inside col
for (col = 0; col < BOARD_SIZE; col++) {
for (row = 0; row < BOARD_SIZE - 1; row++) {
text1 = buttons[row][col].getText();
text2 = buttons[row + 1][col].getText();
if (!text1.equals(text2) || text1.length() == 0) {
break;
}
}
if (row == BOARD_SIZE - 1) {
if (text1.equals("X"))
return GameStatus.XWins;
else
return GameStatus.ZWins;
}
}
// text inside first diagonal
for (row = 0, col = 0; row < BOARD_SIZE - 1; row++, col++) {
text1 = buttons[row][col].getText();
text2 = buttons[row + 1][col + 1].getText();
if (!text1.equals(text2) || text1.length() == 0) {
break;
}
}
if (row == BOARD_SIZE - 1) {
if (text1.equals("X"))
return GameStatus.XWins;
else
return GameStatus.ZWins;
}
// text inside second diagonal
for (row = BOARD_SIZE - 1, col = 0; row > 0; row--, col++) {
text1 = buttons[row][col].getText();
text2 = buttons[row - 1][col + 1].getText();
if (!text1.equals(text2) || text1.length() == 0) {
break;
}
}
if (row == 0) {
if (text1.equals("X"))
return GameStatus.XWins;
else
return GameStatus.ZWins;
}
String text = "";
for (row = 0; row < BOARD_SIZE; row++) {
for (col = 0; col < BOARD_SIZE; col++) {
text = buttons[row][col].getText();
if (text.length() == 0) {
return GameStatus.Incomplete;
}
}
}
return GameStatus.Tie;
}
private void declareWinner(GameStatus gs) {
if (gs == GameStatus.XWins)
JOptionPane.showMessageDialog(this, "X Wins!");
else if (gs == GameStatus.ZWins)
JOptionPane.showMessageDialog(this, "0 Wins!");
else
JOptionPane.showMessageDialog(this, "It's a Tie!");
}
}