Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello! This is Java assignment applet. Can someone help me writing a code how to

ID: 3780897 • Letter: H

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 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;
                   }
       }

   }

}

Explanation / Answer

some useful information regarding various satements to exit the application :

System.exit(1);
That is for abnormal termination of an app. It should not be used here, and not used in an application unless there is a fatal error from which it cannot recover.

System.exit(0);
An applet might share a Java Virtual Machine with other applets. If the applet in the JVM can be seen as a guest in a guesthouse, that is like one of the guests burning down the guesthouse! It is not permitted even in an applet that is trusted.

Applet destroy();
That method is called automatically by the JVM when the JVM thinks it is appropriate to do so. An applet might override the method, but should not explicitly call it.

Applet dispose();
Same deal as destroy(), leave it to the JVM.

code to pop up a dialog box is :

try this
JOptionPane.showMessageDialog(this, "Hello World!")
or
JOptionPane.showMessageDialog(null, "Hello World!")

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote