From fbf374f600b9f4dcca3be784364cd6052d6dd754 Mon Sep 17 00:00:00 2001 From: fdai6040 Date: Tue, 7 Feb 2023 11:54:16 +0100 Subject: [PATCH] Tic Tac Toe created method to restart or exit game, if game over --- src/main/java/src/TicTacToeGame.java | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/src/TicTacToeGame.java b/src/main/java/src/TicTacToeGame.java index 0278fe7..64f04a8 100644 --- a/src/main/java/src/TicTacToeGame.java +++ b/src/main/java/src/TicTacToeGame.java @@ -27,7 +27,7 @@ public class TicTacToeGame implements ActionListener { frame.setVisible(true); textfield.setBackground(new Color(0, 0, 250)); - textfield.setForeground(new Color (100,100,255)); + textfield.setForeground(new Color (255,0,0)); textfield.setFont(new Font("Serif", Font.BOLD, 25)); textfield.setHorizontalAlignment(JLabel.CENTER); textfield.setText("Tic Tac Toe"); @@ -49,7 +49,7 @@ public class TicTacToeGame implements ActionListener { } t_panel.add(textfield); - frame.add(t_panel, BorderLayout.NORTH); + frame.add(t_panel, BorderLayout.SOUTH); frame.add(bt_panel); startGame(); @@ -81,6 +81,23 @@ public class TicTacToeGame implements ActionListener { } } + //method to restart or exit game, if game over + public void gameOver(String s) + { + chance_flag = 0; + Object[] option={"Restart","Exit"}; + int n=JOptionPane.showOptionDialog(frame, "Game Over\n"+s,"Game Over", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, option, option[0]); + if(n==0) + { + frame.dispose(); + new TicTacToeGame(); + } + else + { + frame.dispose(); + } + + } //creating method for checking winner public void chek(){ if ((bton[0].getText() == "X") && (bton[1].getText() == "X") && (bton[2].getText() == "X")) @@ -147,6 +164,7 @@ public class TicTacToeGame implements ActionListener { oWin(6, 7, 8); } else if (chance_flag==9) { textfield.setText("Game Draw :)"); + gameOver("Game Draw"); } else if ((bton[3].getText()== "O") && (bton[4].getText() == "O") && (bton[5].getText() == "O")) oWin(3,4,5); } @@ -163,7 +181,8 @@ public class TicTacToeGame implements ActionListener { { bton[j].setEnabled(false); } - textfield.setText("Player O Wins"); + textfield.setText("Player O wins :)"); + gameOver("Player O wins"); } //method if player X wins @@ -176,7 +195,8 @@ public class TicTacToeGame implements ActionListener { { bton[j].setEnabled(false); } - textfield.setText("Player X wins"); + textfield.setText("Player X wins :)"); + gameOver("Player X wins"); }