-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
164 lines (140 loc) · 5.55 KB
/
Copy pathTicTacToe.java
File metadata and controls
164 lines (140 loc) · 5.55 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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TicTacToe {
int boardWidth = 600;
int boardHeight = 700;
JFrame frame = new JFrame("Tic-Tac-Toe ");
JLabel textLabel = new JLabel();
JPanel textPanel = new JPanel();
JPanel boardPanel = new JPanel();
JPanel buttonPanel = new JPanel();
JButton[][] board = new JButton[3][3];
JButton resetButton = new JButton("Reset Game");
String playerX = "X";
String playerO = "O";
String currentPlayer = playerX;
boolean gameOver = false;
int turns = 0;
TicTacToe() {
frame.setVisible(true);
frame.setSize(boardWidth, boardHeight);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
textLabel.setBackground(Color.darkGray);
textLabel.setOpaque(true);
textLabel.setForeground(Color.white);
textLabel.setFont(new Font("Arial", Font.BOLD, 50));
textLabel.setHorizontalAlignment(JLabel.CENTER);
textLabel.setText("TicTacToe");
textPanel.setLayout(new BorderLayout());
textPanel.add(textLabel);
frame.add(textPanel, BorderLayout.NORTH);
boardPanel.setLayout(new GridLayout(3, 3));
boardPanel.setBackground(Color.darkGray);
frame.add(boardPanel, BorderLayout.CENTER);
buttonPanel.setLayout(new FlowLayout());
resetButton.setFont(new Font("Arial", Font.BOLD, 30));
resetButton.addActionListener(e -> resetGame());
buttonPanel.add(resetButton);
frame.add(buttonPanel, BorderLayout.SOUTH);
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
JButton tile = new JButton();
board[r][c] = tile;
boardPanel.add(tile);
tile.setBackground(Color.darkGray);
tile.setForeground(Color.white);
tile.setFont(new Font("Arial", Font.BOLD, 120));
tile.setFocusable(false);
tile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (gameOver) return;
JButton tile = (JButton) e.getSource();
if (tile.getText().equals("")) {
tile.setText(currentPlayer);
turns++;
checkWinner();
if (!gameOver) {
currentPlayer = currentPlayer.equals(playerX) ? playerO : playerX;
textLabel.setText(currentPlayer + "'s turn.");
}
}
}
});
}
}
}
void checkWinner() {
for (int r = 0; r < 3; r++) {
if (board[r][0].getText().equals("")) continue;
if (board[r][0].getText().equals(board[r][1].getText()) &&
board[r][1].getText().equals(board[r][2].getText())) {
setWinner(board[r][0], board[r][1], board[r][2]);
return;
}
}
for (int c = 0; c < 3; c++) {
if (board[0][c].getText().equals("")) continue;
if (board[0][c].getText().equals(board[1][c].getText()) &&
board[1][c].getText().equals(board[2][c].getText())) {
setWinner(board[0][c], board[1][c], board[2][c]);
return;
}
}
if (board[0][0].getText().equals(board[1][1].getText()) &&
board[1][1].getText().equals(board[2][2].getText()) &&
!board[0][0].getText().equals("")) {
setWinner(board[0][0], board[1][1], board[2][2]);
return;
}
if (board[0][2].getText().equals(board[1][1].getText()) &&
board[1][1].getText().equals(board[2][0].getText()) &&
!board[0][2].getText().equals("")) {
setWinner(board[0][2], board[1][1], board[2][0]);
return;
}
if (turns == 9) {
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
setTie(board[r][c]);
}
}
gameOver = true;
}
}
void setWinner(JButton tile1, JButton tile2, JButton tile3) {
tile1.setForeground(Color.green);
tile2.setForeground(Color.green);
tile3.setForeground(Color.green);
tile1.setBackground(Color.gray);
tile2.setBackground(Color.gray);
tile3.setBackground(Color.gray);
textLabel.setText(currentPlayer + " is the winner!");
gameOver = true;
}
void setTie(JButton tile) {
tile.setForeground(Color.orange);
tile.setBackground(Color.gray);
textLabel.setText("Tie!");
}
void resetGame() {
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
board[r][c].setText("");
board[r][c].setForeground(Color.white);
board[r][c].setBackground(Color.darkGray);
}
}
currentPlayer = playerX;
textLabel.setText("TicTacToe");
gameOver = false;
turns = 0;
}
public static void main(String[] args) {
new TicTacToe();
}
}