-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToeGame.java
163 lines (140 loc) · 5.21 KB
/
TicTacToeGame.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
154
155
156
157
158
159
160
161
162
163
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TicTacToeGame extends JFrame implements ActionListener {
private JButton[][] buttons = new JButton[3][3];
private char currentPlayer = 'X';
private boolean gameWon = false;
public TicTacToeGame() {
setTitle("Tic-Tac-Toe");
setSize(350, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 3));
initializeButtons();
createMenuBar();
setVisible(true);
}
private void initializeButtons() {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
buttons[row][col] = new JButton("");
buttons[row][col].setFont(new Font("Arial", Font.BOLD, 80));
buttons[row][col].setFocusPainted(false);
buttons[row][col].setBackground(Color.WHITE);
buttons[row][col].addActionListener(this);
add(buttons[row][col]);
}
}
}
private void createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu gameMenu = new JMenu("Game");
JMenuItem newGameItem = new JMenuItem("New Game");
JMenuItem exitItem = new JMenuItem("Exit");
newGameItem.addActionListener(e -> startNewGame());
exitItem.addActionListener(e -> System.exit(0));
gameMenu.add(newGameItem);
gameMenu.add(exitItem);
menuBar.add(gameMenu);
setJMenuBar(menuBar);
}
private void startNewGame() {
currentPlayer = 'X';
gameWon = false;
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
buttons[row][col].setText("");
buttons[row][col].setBackground(Color.WHITE);
buttons[row][col].setEnabled(true);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
JButton buttonClicked = (JButton) e.getSource();
if (!gameWon && buttonClicked.getText().equals("")) {
buttonClicked.setText(String.valueOf(currentPlayer));
if (currentPlayer == 'X') {
buttonClicked.setForeground(Color.RED);
} else {
buttonClicked.setForeground(Color.BLUE);
}
buttonClicked.setEnabled(false);
if (checkWin()) {
JOptionPane.showMessageDialog(this, "Player " + currentPlayer + " wins!");
highlightWinningLine();
gameWon = true;
} else if (isBoardFull()) {
JOptionPane.showMessageDialog(this, "It's a draw!");
} else {
switchPlayer();
}
}
}
private void switchPlayer() {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}
private boolean isBoardFull() {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (buttons[row][col].getText().equals("")) {
return false;
}
}
}
return true;
}
private boolean checkWin() {
for (int row = 0; row < 3; row++) {
if (checkLine(buttons[row][0], buttons[row][1], buttons[row][2])) {
return true;
}
}
for (int col = 0; col < 3; col++) {
if (checkLine(buttons[0][col], buttons[1][col], buttons[2][col])) {
return true;
}
}
if (checkLine(buttons[0][0], buttons[1][1], buttons[2][2])) {
return true;
}
if (checkLine(buttons[0][2], buttons[1][1], buttons[2][0])) {
return true;
}
return false;
}
private boolean checkLine(JButton b1, JButton b2, JButton b3) {
return !b1.getText().equals("") && b1.getText().equals(b2.getText()) && b2.getText().equals(b3.getText());
}
private void highlightWinningLine() {
for (int row = 0; row < 3; row++) {
if (checkLine(buttons[row][0], buttons[row][1], buttons[row][2])) {
buttons[row][0].setBackground(Color.BLUE);
buttons[row][1].setBackground(Color.BLUE);
buttons[row][2].setBackground(Color.BLUE);
}
}
for (int col = 0; col < 3; col++) {
if (checkLine(buttons[0][col], buttons[1][col], buttons[2][col])) {
buttons[0][col].setBackground(Color.BLUE);
buttons[1][col].setBackground(Color.BLUE);
buttons[2][col].setBackground(Color.BLUE);
}
}
// Check diagonals
if (checkLine(buttons[0][0], buttons[1][1], buttons[2][2])) {
buttons[0][0].setBackground(Color.BLUE);
buttons[1][1].setBackground(Color.BLUE);
buttons[2][2].setBackground(Color.BLUE);
}
if (checkLine(buttons[0][2], buttons[1][1], buttons[2][0])) {
buttons[0][2].setBackground(Color.BLUE);
buttons[1][1].setBackground(Color.BLUE);
buttons[2][0].setBackground(Color.BLUE);
}
}
public static void main(String[] args) {
new TicTacToeGame();
}
}