8
8
public class TicTacToe extends JFrame implements ActionListener {
9
9
private JButton [][] buttons = new JButton [3 ][3 ];
10
10
private JLabel statusLabel = new JLabel ("X's turn" , SwingConstants .CENTER );
11
+ private JLabel scoreLabel = new JLabel ("Player 1: 0 | Player 2: 0" , SwingConstants .CENTER );
11
12
private char currentPlayerMark = 'X' ;
13
+ private int player1Score = 0 ;
14
+ private int player2Score = 0 ;
15
+ private boolean isPlayer1X = true ; // Track who is playing 'X'
12
16
private Queue <Point > xMoves = new LinkedList <>();
13
17
private Queue <Point > oMoves = new LinkedList <>();
14
18
@@ -22,8 +26,12 @@ public TicTacToe() {
22
26
initializeButtons (boardPanel );
23
27
add (boardPanel , BorderLayout .CENTER );
24
28
29
+ JPanel statusPanel = new JPanel (new GridLayout (2 , 1 ));
25
30
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 );
27
35
}
28
36
29
37
private void initializeButtons (JPanel boardPanel ) {
@@ -32,6 +40,8 @@ private void initializeButtons(JPanel boardPanel) {
32
40
buttons [i ][j ] = new JButton ();
33
41
buttons [i ][j ].setFont (new Font (Font .SANS_SERIF , Font .BOLD , 100 ));
34
42
buttons [i ][j ].setFocusPainted (false );
43
+ buttons [i ][j ].setBackground (Color .WHITE ); // Set background color
44
+ buttons [i ][j ].setOpaque (true );
35
45
buttons [i ][j ].addActionListener (this );
36
46
boardPanel .add (buttons [i ][j ]);
37
47
}
@@ -42,16 +52,20 @@ public void actionPerformed(ActionEvent e) {
42
52
JButton buttonClicked = (JButton ) e .getSource ();
43
53
if (buttonClicked .getText ().equals ("" )) {
44
54
buttonClicked .setText (String .valueOf (currentPlayerMark ));
55
+ buttonClicked .setForeground (currentPlayerMark == 'X' ? Color .RED : Color .BLUE );
45
56
Point move = findPosition (buttonClicked );
46
57
trackMove (currentPlayerMark , move );
47
58
manageFourMarks (currentPlayerMark );
48
59
49
60
if (checkForWin ()) {
50
61
JOptionPane .showMessageDialog (null , "Player " + currentPlayerMark + " wins!" );
62
+ updateScore (currentPlayerMark );
51
63
resetButtons ();
64
+ switchPlayers ();
52
65
} else if (isBoardFull ()) {
53
66
JOptionPane .showMessageDialog (null , "Draw!" );
54
67
resetButtons ();
68
+ switchPlayers ();
55
69
} else {
56
70
currentPlayerMark = (currentPlayerMark == 'X' ) ? 'O' : 'X' ;
57
71
statusLabel .setText (currentPlayerMark + "'s turn" );
@@ -97,6 +111,19 @@ private boolean isBoardFull() {
97
111
return true ;
98
112
}
99
113
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
+
100
127
private boolean checkForWin () {
101
128
return checkRowsForWin () || checkColumnsForWin () || checkDiagonalsForWin ();
102
129
}
@@ -133,6 +160,8 @@ private void resetButtons() {
133
160
buttons [i ][j ].setText ("" );
134
161
}
135
162
}
163
+ currentPlayerMark = 'X' ; // 'X' luôn là người chơi đầu tiên
164
+ statusLabel .setText (currentPlayerMark + "'s turn" );
136
165
xMoves .clear ();
137
166
oMoves .clear ();
138
167
}
0 commit comments