Skip to content

Commit d7fe046

Browse files
committed
new
1 parent a5367fe commit d7fe046

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

.projects/TicTacToe.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
public class TicTacToe extends JFrame implements ActionListener {
99
private JButton[][] buttons = new JButton[3][3];
1010
private JLabel statusLabel = new JLabel("X's turn", SwingConstants.CENTER);
11+
private JLabel scoreLabel = new JLabel("Player 1: 0 | Player 2: 0", SwingConstants.CENTER);
1112
private char currentPlayerMark = 'X';
13+
private int player1Score = 0;
14+
private int player2Score = 0;
15+
private boolean isPlayer1X = true; // Track who is playing 'X'
1216
private Queue<Point> xMoves = new LinkedList<>();
1317
private Queue<Point> oMoves = new LinkedList<>();
1418

@@ -22,8 +26,12 @@ public TicTacToe() {
2226
initializeButtons(boardPanel);
2327
add(boardPanel, BorderLayout.CENTER);
2428

29+
JPanel statusPanel = new JPanel(new GridLayout(2, 1));
2530
statusLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
26-
add(statusLabel, BorderLayout.NORTH);
31+
scoreLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
32+
statusPanel.add(statusLabel);
33+
statusPanel.add(scoreLabel);
34+
add(statusPanel, BorderLayout.NORTH);
2735
}
2836

2937
private void initializeButtons(JPanel boardPanel) {
@@ -32,6 +40,8 @@ private void initializeButtons(JPanel boardPanel) {
3240
buttons[i][j] = new JButton();
3341
buttons[i][j].setFont(new Font(Font.SANS_SERIF, Font.BOLD, 100));
3442
buttons[i][j].setFocusPainted(false);
43+
buttons[i][j].setBackground(Color.WHITE); // Set background color
44+
buttons[i][j].setOpaque(true);
3545
buttons[i][j].addActionListener(this);
3646
boardPanel.add(buttons[i][j]);
3747
}
@@ -42,16 +52,20 @@ public void actionPerformed(ActionEvent e) {
4252
JButton buttonClicked = (JButton) e.getSource();
4353
if (buttonClicked.getText().equals("")) {
4454
buttonClicked.setText(String.valueOf(currentPlayerMark));
55+
buttonClicked.setForeground(currentPlayerMark == 'X' ? Color.RED : Color.BLUE);
4556
Point move = findPosition(buttonClicked);
4657
trackMove(currentPlayerMark, move);
4758
manageFourMarks(currentPlayerMark);
4859

4960
if (checkForWin()) {
5061
JOptionPane.showMessageDialog(null, "Player " + currentPlayerMark + " wins!");
62+
updateScore(currentPlayerMark);
5163
resetButtons();
64+
switchPlayers();
5265
} else if (isBoardFull()) {
5366
JOptionPane.showMessageDialog(null, "Draw!");
5467
resetButtons();
68+
switchPlayers();
5569
} else {
5670
currentPlayerMark = (currentPlayerMark == 'X') ? 'O' : 'X';
5771
statusLabel.setText(currentPlayerMark + "'s turn");
@@ -97,6 +111,19 @@ private boolean isBoardFull() {
97111
return true;
98112
}
99113

114+
private void switchPlayers() {
115+
isPlayer1X = !isPlayer1X;
116+
}
117+
118+
private void updateScore(char winner) {
119+
if ((winner == 'X' && isPlayer1X) || (winner == 'O' && !isPlayer1X)) {
120+
player1Score++;
121+
} else {
122+
player2Score++;
123+
}
124+
scoreLabel.setText("Player 1: " + player1Score + " | Player 2: " + player2Score);
125+
}
126+
100127
private boolean checkForWin() {
101128
return checkRowsForWin() || checkColumnsForWin() || checkDiagonalsForWin();
102129
}
@@ -133,6 +160,8 @@ private void resetButtons() {
133160
buttons[i][j].setText("");
134161
}
135162
}
163+
currentPlayerMark = 'X'; // 'X' luôn là người chơi đầu tiên
164+
statusLabel.setText(currentPlayerMark + "'s turn");
136165
xMoves.clear();
137166
oMoves.clear();
138167
}

2. Classes in Java/NestedClass.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
// Lớp chồng nhau (nested class) là một lớp được định nghĩa bên trong một lớp khác.
77

88
// Ví dụ:
9+
// ContactBook là danh bạ điện thoại, bao gồm danh sách các liên hệ và các nhóm.
910
class ContactBook {
1011
public List<Contact> contacts = new ArrayList<>();
1112
public Map<String, Group> groups = new HashMap<>();
1213

14+
// Lớp Contact đại diện cho một liên hệ trong danh bạ, gồm tên và số điện thoại.
1315
class Contact {
1416
private String name;
1517
private String phone;
1618

19+
// Là một lớp, Contact cũng có hàm khởi tạo.
20+
// Dùng this để tham chiếu đến biến của lớp Contact.
1721
public Contact(String name, String phone) {
1822
this.name = name;
1923
this.phone = phone;
@@ -24,6 +28,8 @@ public void displayContact() {
2428
}
2529
}
2630

31+
// Lớp Group đại diện cho một nhóm trong danh bạ, bao gồm tên nhóm và danh sách
32+
// các thành viên.
2733
static class Group {
2834
String name;
2935
List<String> members = new ArrayList<>();

0 commit comments

Comments
 (0)