@@ -40,6 +40,7 @@ public void runGame() {
4040 columnButtons = new JButton [COLUMNS ];
4141
4242 createDropButtons ();
43+ addResetButton ();
4344
4445 // set each tile as an unclickable button with a white background to just add circles on top
4546 for (int row = 0 ; row < ROWS ; row ++) {
@@ -80,17 +81,39 @@ public void actionPerformed(ActionEvent e) {
8081 if (checkWin (WHITE )) {
8182 JOptionPane .showMessageDialog (Connect4 .this , "WHITE wins!" );
8283 gameOver = true ;
84+
8385 } else if (checkWin (BLACK )) {
8486 JOptionPane .showMessageDialog (Connect4 .this , "BLACK wins!" );
8587 gameOver = true ;
88+
8689 } else if (checkDraw ()) {
8790 JOptionPane .showMessageDialog (Connect4 .this , "Draw!" );
8891 gameOver = true ;
92+
8993 }
9094 }
9195 }
9296 }
9397
98+ private void addResetButton () {
99+ JPanel resetPanel = new JPanel ();
100+ JButton resetButton = new JButton ("Reset" );
101+ resetButton .addActionListener (new ActionListener () {
102+ public void actionPerformed (ActionEvent e ) {
103+ // Reset the game
104+
105+ isWhiteTurn = true ;
106+ gameOver = false ;
107+ runGame ();
108+ }
109+ });
110+ resetPanel .add (resetButton );
111+ add (resetPanel , BorderLayout .SOUTH );
112+ pack ();
113+ setVisible (true );
114+ }
115+
116+
94117 public void setGridIcon () {
95118 Component [] components = boardPanel .getComponents ();
96119 int index = COLUMNS ; // start at the bottom left of the board by reducing the indexes of drop buttons
@@ -134,7 +157,11 @@ public void makeUserMove(int column) {
134157 }
135158 }
136159 }
137- switchTurn ();
160+
161+ // switch turns
162+ if (moveMade ){
163+ switchTurn ();
164+ }
138165 }
139166
140167 // Switch turns
0 commit comments