Browse Source

Tic Tac Toe created method to restart or exit game, if game over

remotes/origin/develop
fdai6040 2 years ago
parent
commit
fbf374f600
  1. 28
      src/main/java/src/TicTacToeGame.java

28
src/main/java/src/TicTacToeGame.java

@ -27,7 +27,7 @@ public class TicTacToeGame implements ActionListener {
frame.setVisible(true); frame.setVisible(true);
textfield.setBackground(new Color(0, 0, 250)); 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.setFont(new Font("Serif", Font.BOLD, 25));
textfield.setHorizontalAlignment(JLabel.CENTER); textfield.setHorizontalAlignment(JLabel.CENTER);
textfield.setText("Tic Tac Toe"); textfield.setText("Tic Tac Toe");
@ -49,7 +49,7 @@ public class TicTacToeGame implements ActionListener {
} }
t_panel.add(textfield); t_panel.add(textfield);
frame.add(t_panel, BorderLayout.NORTH);
frame.add(t_panel, BorderLayout.SOUTH);
frame.add(bt_panel); frame.add(bt_panel);
startGame(); 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 //creating method for checking winner
public void chek(){ public void chek(){
if ((bton[0].getText() == "X") && (bton[1].getText() == "X") && (bton[2].getText() == "X")) 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); oWin(6, 7, 8);
} else if (chance_flag==9) { } else if (chance_flag==9) {
textfield.setText("Game Draw :)"); textfield.setText("Game Draw :)");
gameOver("Game Draw");
} else if ((bton[3].getText()== "O") && (bton[4].getText() == "O") && (bton[5].getText() == "O")) } else if ((bton[3].getText()== "O") && (bton[4].getText() == "O") && (bton[5].getText() == "O"))
oWin(3,4,5); oWin(3,4,5);
} }
@ -163,7 +181,8 @@ public class TicTacToeGame implements ActionListener {
{ {
bton[j].setEnabled(false); bton[j].setEnabled(false);
} }
textfield.setText("Player O Wins");
textfield.setText("Player O wins :)");
gameOver("Player O wins");
} }
//method if player X wins //method if player X wins
@ -176,7 +195,8 @@ public class TicTacToeGame implements ActionListener {
{ {
bton[j].setEnabled(false); bton[j].setEnabled(false);
} }
textfield.setText("Player X wins");
textfield.setText("Player X wins :)");
gameOver("Player X wins");
} }

Loading…
Cancel
Save