-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.java
153 lines (122 loc) · 3.81 KB
/
TicTacToe.java
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
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TicTacToe extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 350;
private JLabel status;
private JButton [] squares = new JButton[9];
private int turn;
private boolean gameOver;
public static void main(String [] args) {
TicTacToe run = new TicTacToe();
run.setVisible(true);
}
public TicTacToe() {
turn = 0;
gameOver = false;
setTitle("Tic Tac Toe");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
setLayout(new BorderLayout());
status = new JLabel("X to play");
add(status, BorderLayout.NORTH);
JPanel gameGrid = new JPanel();
gameGrid.setLayout(new GridLayout(3,3));
add(gameGrid, BorderLayout.CENTER);
for (int i = 0; i < 9; i++) {
squares[i] = new JButton();
squares[i].setActionCommand(Integer.toString(i+1)); // sets the action command to something other than the button label
squares[i].addActionListener(this);
gameGrid.add(squares[i]);
}
JPanel bottom = new JPanel();
bottom.setLayout(new FlowLayout());
add(bottom, BorderLayout.SOUTH);
JButton reset = new JButton("Reset");
reset.addActionListener(this);
bottom.add(reset);
}
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if (actionCommand.equals("Reset")) {
for (int i = 0; i < 9; i++) {
squares[i].setActionCommand(Integer.toString(i+1));
squares[i].setText("");
}
turn=0;
gameOver = false;
status.setText("X to play");
return;
}
if (gameOver) return;
if (actionCommand.equals("X")) return;
if (actionCommand.equals("O")) return;
int actionInt = Integer.valueOf(actionCommand);
if(turn == 1) {
squares[actionInt-1].setText("O");
squares[actionInt-1].setActionCommand("O");
turn = 0;
}
else {
squares[actionInt-1].setText("X");
squares[actionInt-1].setActionCommand("X");
turn = 1;
}
int winner = checkWinner();
if (winner == 0) {
if (turn == 0) status.setText("X to play");
else status.setText("O to play");
return;
}
else if (winner == 1) {
status.setText("Draw. Press reset to play again.");
gameOver = true;
return;
}
else if (winner == 2) {
status.setText("X wins! Press reset to play again.");
gameOver = true;
return;
}
else if (winner == 3) {
status.setText("O Wins! Press reset to play again.");
gameOver = true;
return;
}
}
public int checkWinner() {
boolean full = true;
char [] v = new char[9];
for (int i = 0; i < 9; i++){
v[i] = squares[i].getActionCommand().charAt(0);
if(v[i] != 'X' && v[i] != 'O')
full = false;
}
if(v[0] == 'X' && v[1] == 'X' && v[2] == 'X') return 2;
if(v[3] == 'X' && v[4] == 'X' && v[5] == 'X') return 2;
if(v[6] == 'X' && v[7] == 'X' && v[8] == 'X') return 2;
if(v[0] == 'X' && v[3] == 'X' && v[6] == 'X') return 2;
if(v[1] == 'X' && v[4] == 'X' && v[7] == 'X') return 2;
if(v[2] == 'X' && v[5] == 'X' && v[8] == 'X') return 2;
if(v[0] == 'X' && v[4] == 'X' && v[8] == 'X') return 2;
if(v[2] == 'X' && v[4] == 'X' && v[6] == 'X') return 2;
if(v[0] == 'O' && v[1] == 'O' && v[2] == 'O') return 3;
if(v[3] == 'O' && v[4] == 'O' && v[5] == 'O') return 3;
if(v[6] == 'O' && v[7] == 'O' && v[8] == 'O') return 3;
if(v[0] == 'O' && v[3] == 'O' && v[6] == 'O') return 3;
if(v[1] == 'O' && v[4] == 'O' && v[7] == 'O') return 3;
if(v[2] == 'O' && v[5] == 'O' && v[8] == 'O') return 3;
if(v[0] == 'O' && v[4] == 'O' && v[8] == 'O') return 3;
if(v[2] == 'O' && v[4] == 'O' && v[6] == 'O') return 3;
if (full) return 1;
return 0;
}
}