Question: Hello! This is Java assignment applet. Can someone help me writing a code how to exit the game when I press no in question( would
Hello!
This is Java assignment applet.
Can someone help me writing a code how to exit the game when I press no in question( would you like to play again?). Also if no one wins or draws i would like to pop up the question ( would you like to play again?). I have the code below i would like someone to add it
Thanks in advance!
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.applet.*;
public class TicTacToe extends Applet implements ActionListener{
// Array to store buttons JButton button[][] = new JButton[3][3]; boolean isNewGame =false; // To switch player either X or O boolean isZero = true; //first turn is always 0 // Setup size // Set layout to grid // Create buttons through loop // Add buttons to layout // Add listener public void init(){ setSize(500, 500); setLayout(new GridLayout(3,3)); // if it is not a new game if(isNewGame==false) { // Adding buttons to layout //initialize the Grid and add ActionListener to each Button for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ // init jButton button[i][j] = new JButton("");
// Setup Font and Size here button[i][j].setFont(new Font("Lucida Grande", Font.PLAIN, 28));
// Add is a method from applet to add buttons in layout add(button[i][j]); // Listener button[i][j].addActionListener(this); } } } else { // If we need to restart the game, then we only set text to empty // Back to black color // add listener for(int i = 0; i < 3; i++){ for(int j = 0; j < 3; j++){ button[i][j].setText(""); button[i][j].setForeground(Color.BLACK); button[i][j].removeActionListener(this); button[i][j].addActionListener(this); } } } } // Go all the buttons in arrsy // Check the event with button in array // if player O is true, set button text O. Otherwise. X // Remove Listerner. Why? only once you can click // Change player by isZero // Check for winner public void actionPerformed(ActionEvent event){ for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++){ if((event.getSource() == button[i][j])){ if(isZero){ button[i][j].setText("O"); } else{ button[i][j].setText("X"); } button[i][j].removeActionListener(this); //you cannot click on one tile more than once isZero = !isZero; checkForWinners(); //check if any winner after each move } } } public void checkForWinners(){ //3 horizontal lines for(int i = 0; i < 3; i++){ if(button[i][0].getText().equals(button[i][1].getText()) && button[i][1].getText().equals(button[i][2].getText()) && !button[i][0].getText().equals("")){ String player = button[i][0].getText(); button[i][0].setForeground(Color.RED); button[i][1].setForeground(Color.RED); button[i][2].setForeground(Color.RED); int reply =JOptionPane.showConfirmDialog(null, player + " wins!"+" Would like to play again?","Game over",JOptionPane.YES_NO_OPTION); if (reply == JOptionPane.YES_OPTION) { isNewGame = true; init(); return; } else { System.exit(0); return; } } } //3 vertical lines for(int i = 0; i < 3; i++){ if(button[0][i].getText().equals(button[1][i].getText()) && button[0][i].getText().equals(button[2][i].getText()) && !button[0][i].getText().equals("")){ String player = button[0][i].getText(); button[0][i].setForeground(Color.RED); button[1][i].setForeground(Color.RED); button[2][i].setForeground(Color.RED); int reply =JOptionPane.showConfirmDialog(null, player + " wins!"+" Would like to play again?","Game over",JOptionPane.YES_NO_OPTION); if (reply == JOptionPane.YES_OPTION) { isNewGame = true; init(); return; } else { System.exit(0); return; } } } //top to bottom (rightwards) diagonal if(button[0][0].getText().equals(button[1][1].getText()) && button[1][1].getText().equals(button[2][2].getText()) && !button[0][0].getText().equals("")){ button[0][0].setForeground(Color.RED); button[1][1].setForeground(Color.RED); button[2][2].setForeground(Color.RED); int reply =JOptionPane.showConfirmDialog(null, button[0][0].getText() + " wins!"+" Would like to play again?","Game over",JOptionPane.YES_NO_OPTION); if (reply == JOptionPane.YES_OPTION) { isNewGame = true; init(); return; } else { System.exit(0); return; } } //bottom to top (leftwards) diagonal if(button[0][2].getText().equals(button[1][1].getText()) && button[1][1].getText().equals(button[2][0].getText()) && !button[2][0].getText().equals("")){ button[0][2].setForeground(Color.RED); button[1][1].setForeground(Color.RED); button[2][0].setForeground(Color.RED); int reply =JOptionPane.showConfirmDialog(null,button[0][2].getText() + " wins!"+" Would like to play again?","Game over",JOptionPane.YES_NO_OPTION); if (reply == JOptionPane.YES_OPTION) { isNewGame = true; init(); return; } else { System.exit(0); return; } }
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
