-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.java
More file actions
146 lines (129 loc) · 4.6 KB
/
code.java
File metadata and controls
146 lines (129 loc) · 4.6 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class adwaith extends JFrame implements ActionListener {
private JButton[][] buttons = new JButton[3][3];
private boolean player1Turn = true;
private JLabel message = new JLabel("Player 1's turn");
private int player1Score = 0;
private int player2Score = 0;
private JLabel scoreLabel = new JLabel("Score: " + player1Score + " - " + player2Score);
public adwaith() {
super("Tic Tac Toe");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel gamePanel = new JPanel();
gamePanel.setLayout(new GridLayout(3, 3));
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
buttons[row][col] = new JButton();
buttons[row][col].addActionListener(this);
buttons[row][col].setFont(new Font("Arial", Font.PLAIN, 80));
gamePanel.add(buttons[row][col]);
}
}
add(gamePanel, BorderLayout.CENTER);
JPanel statusPanel = new JPanel();
statusPanel.setLayout(new GridLayout(1, 3));
statusPanel.add(message);
statusPanel.add(scoreLabel);
JButton newGameButton = new JButton("New Game");
newGameButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetGame();
}
});
statusPanel.add(newGameButton);
add(statusPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
if (button.getText().equals("")) {
if (player1Turn) {
button.setText("X");
message.setText("Player 2's turn");
} else {
button.setText("O");
message.setText("Player 1's turn");
}
player1Turn = !player1Turn;
if (checkForWin()) {
if (player1Turn) {
message.setText("Player 2 wins!");
player2Score++;
} else {
message.setText("Player 1 wins!");
player1Score++;
}
updateScore();
disableButtons();
} else if (checkForDraw()) {
message.setText("It's a draw!");
disableButtons();
}
}
}
private boolean checkForWin() {
String[][] board = new String[3][3];
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
board[row][col] = buttons[row][col].getText();
}
}
// Check rows
for (int row = 0; row < 3; row++) {
if (board[row][0].equals(board[row][1]) && board[row][1].equals(board[row][2]) && !board[row][0].equals("")) {
return true;
}
}
// Check columns
for (int col = 0; col < 3; col++) {
if (board[0][col].equals(board[1][col]) && board[1][col].equals(board[2][col]) && !board[0][col].equals("")) {
return true;
}
}
// Check diagonals
if (board[0][0].equals(board[1][1]) && board[1 ][1].equals(board[2][2]) && !board[0][0].equals("")) {
return true;
}
if (board[0][2].equals(board[1][1]) && board[1][1].equals(board[2][0]) && !board[0][2].equals("")) {
return true;
}
return false;
}
private boolean checkForDraw() {
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 void resetGame() {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
buttons[row][col].setText("");
buttons[row][col].setEnabled(true);
}
}
player1Turn = true;
message.setText("Player 1's turn");
}
private void disableButtons() {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
buttons[row][col].setEnabled(false);
}
}
}
private void updateScore() {
scoreLabel.setText("Score: " + player1Score + " - " + player2Score);
}
public static void main(String[] args) {
new adwaith();
}
}