-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTTT.java
More file actions
182 lines (157 loc) · 3.87 KB
/
TTT.java
File metadata and controls
182 lines (157 loc) · 3.87 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
package Game;
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 TTT extends JFrame implements ActionListener {
public static final int BOARD_SIZE = 3;
private boolean move = true;
private static enum Gamestatus {
Xwin, Zwin, Tie, Incomplete;
}
private JButton[][] buttons = new JButton[BOARD_SIZE][BOARD_SIZE];
public TTT() {
super.setTitle("Tick Tack Toe");
super.setSize(800, 800);
GridLayout grid = new GridLayout(BOARD_SIZE, BOARD_SIZE);
super.setLayout(grid);
Font font = new Font("Comic Sans", 3, 150);
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
JButton button = new JButton("");
buttons[i][j] = 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();
this.makeMove(clickedButton);
Gamestatus gs = getgameStatus();
if (gs != Gamestatus.Incomplete) {
this.declareWinner(gs);
int choice = JOptionPane.showConfirmDialog(this, "RESTART?");
if (choice == JOptionPane.YES_OPTION) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
buttons[i][j].setText("");
}
}
move = true;
} else {
super.dispose();
}
}
}
private void makeMove(JButton clickedButton) {
String txt = clickedButton.getText();
if (txt.length() != 0) {
JOptionPane.showMessageDialog(this, "Invalid Move !");
return;
}
if (move) {
clickedButton.setText("X");
} else {
clickedButton.setText("0");
}
move = !move;
}
private Gamestatus getgameStatus() {
String txt1 = "", txt2 = "";
int row = 0, col = 0;
for (row = 0; row < BOARD_SIZE; row++) {
for (col = 0; col < BOARD_SIZE - 1; col++) {
txt1 = buttons[row][col].getText();
txt2 = buttons[row][col + 1].getText();
if (!txt1.equals(txt2) || txt1.length() == 0) {
break;
}
}
if (col == BOARD_SIZE - 1) {
if (txt1.equals("X")) {
return Gamestatus.Xwin;
} else {
return Gamestatus.Zwin;
}
}
}
for (col = 0; col < BOARD_SIZE; col++) {
for (row = 0; row < BOARD_SIZE - 1; row++) {
txt1 = buttons[row][col].getText();
txt2 = buttons[row + 1][col].getText();
if (!txt1.equals(txt2) || txt1.length() == 0) {
break;
}
}
if (row == BOARD_SIZE - 1) {
if (txt1.equals("X")) {
return Gamestatus.Xwin;
} else {
return Gamestatus.Zwin;
}
}
}
int i= 0 ;
for( i = 0 ;i < BOARD_SIZE-1 ;i++ ){
txt1 = buttons[i][i].getText() ;
txt2 = buttons[i+1][i+1].getText() ;
if(!txt1.equals(txt2) || txt1.length() == 0){
break ;
}
}
if(i == BOARD_SIZE-1){
if(txt1.equals("X")){
return Gamestatus.Xwin ;
}
else{
return Gamestatus.Zwin ;
}
}
row = BOARD_SIZE -1 ;
col = 0 ;
while(row > 0){
txt1 = buttons[row][col].getText() ;
txt2 = buttons[row-1][col+1].getText() ;
if(!txt1.equals(txt2) || txt1.length() == 0){
break ;
}
row-- ;
col++ ;
}
if(row == 0){
if(txt1.equals("X")){
return Gamestatus.Xwin ;
}
else{
return Gamestatus.Zwin ;
}
}
String txt = "";
for (i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
txt = buttons[i][j].getText();
if (txt.length() == 0) {
return Gamestatus.Incomplete;
}
}
}
return Gamestatus.Tie;
}
private void declareWinner(Gamestatus gs) {
if (gs == Gamestatus.Xwin) {
JOptionPane.showMessageDialog(this, "X WINS");
} else if (gs == Gamestatus.Zwin) {
JOptionPane.showMessageDialog(this, "O WINS");
} else {
JOptionPane.showMessageDialog(this, " Its a TIE");
}
}
}