NoughtsAndCrossesGame play button doesn't work

NoughtsAndCrossesGame play button doesn't work

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author 000621812
 */
public class NoughtsAndCrossesGamev2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        NoughtsAndCrossesGameFrame noughtsAndCrossesGame;

        noughtsAndCrossesGame = new NoughtsAndCrossesGameFrame();
        noughtsAndCrossesGame.setVisible(true);


    }
}

class NoughtsAndCrossesGameFrame extends JFrame {

    NoughtsAndCrossesGamePanel gamePanel;
    NoughtsAndCrossesButtonPanel buttonPanel;
    private ButtonPlayActionHandle playHandler;
    private ButtonExitActionHandler exitHandler;
    private JButton playButton;
    private JButton exitButton;

    public NoughtsAndCrossesGameFrame() {

        gamePanel = new NoughtsAndCrossesGamePanel();
        buttonPanel = new NoughtsAndCrossesButtonPanel();


        this.setLayout(new GridLayout(3, 3));
        this.setLayout(new FlowLayout());


        this.setVisible(true);


        this.setTitle("Noughts and Crosses");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        this.add(playButton );
        this.add(exitButton );

        this.add(gamePanel, FlowLayout.LEFT);
        this.add(buttonPanel,FlowLayout.LEFT);


     this.setSize(145, 145);
    this.setVisible(true);














    }

    /**
     * Asks the panel containing the game to reset the game.
     */
    private void resetGame() {
        //**
//* The button panel



//*/
    }

    class NoughtsAndCrossesButtonPanel extends JPanel {

        /**
         * The game panel
         *
         */
        public NoughtsAndCrossesButtonPanel() {


            playButton = new JButton("Play");
            playButton.addActionListener(playHandler);
            playHandler = new ButtonPlayActionHandle();

             exitButton = new JButton("Exit");
            exitHandler = new ButtonExitActionHandler();
            exitButton.addActionListener(exitHandler);








        }
    }
}

class NoughtsAndCrossesGamePanel extends JPanel {

    private int clickCount = 0;
    private JButton[] btnKeypad = new JButton[9];

    public NoughtsAndCrossesGamePanel() {
        /**
         * Resets the buttons and clicjCount ready for a new game
         */
        HandlePlayerShot shotHandler = new HandlePlayerShot();
        // Set some JFrame details




        // NOTE - In grid layout you provide the number of
        // rows and columns but columns are ignored and always set equal to rows
        // unless the rows are zero.
        //TODO

        this.setLayout(new BorderLayout());
        this.setLayout(new GridLayout(3, 3));
     this.setSize(110, 110);
    this.setVisible(true);









        // Create the buttons and add them  to the JFrame
        for (int i = 0; i < 9; i++) {
            btnKeypad[i] = new JButton(" ");
            this.add(btnKeypad[i]);
            btnKeypad[i].addActionListener(shotHandler);


            //TODO
        }

        // Register the same listener for each button as the action needed is always the same.
        // Note - this could have been done in the above loop but is shown separately here to make
        // it clearer.
        //shotHandler = new HandlePlayerShot();



     this.setSize(200, 200);





    }

    private void resetGame() {
        /**
         * Disables all the buttons when the game ends
         *
         */
        for(int i=0; i<9; i++)
        {
            char ch=(char)('0'+i+1);
            btnKeypad[i].setText(""+ch);


        }






    }

    private void gameFinished() {
        /**
         * Inner class of NoughtsAndCrossesGamePanel to handle a players shot
         * ie when a game button is pressed.
         *
         */
        for (int i = 0; i < 9; i++) {

            btnKeypad[i].setEnabled(false);


        }
        //this.dispose(gamePanel);
    }

    class HandlePlayerShot implements ActionListener { //TODO  implements ????

        /**
         * Inner class of NoughtsAndCrossesGameFrame to respond to the Exit Button being
         * pressed. Dispose of the frame and exits the application
         */
        @Override
        /**
         * Responds to the button press - ie player shot
         */
        public void actionPerformed(ActionEvent e) {
            JButton currentButton;
            String winner;

            clickCount++;
            currentButton = (JButton) (e.getSource());
            // If it is an odd click count (button press count) then it is "X"
            // shot as "X" player always goes first
            if (clickCount % 2 == 1) {
                currentButton.setText("X");
            } else {
                currentButton.setText("0");
            }
            currentButton.setEnabled(false);
            winner = checkForWinner();
            if (winner != null) {
                //Display a message showing who the winner is
                //TODO
                JOptionPane.showMessageDialog(null, "The winner is " + winner);

                //NoughtsAndCrossesGamePanel.this.dispose();
            } else {
                // check that all possible shots have been had
                if (clickCount == 9) {
                    //Display a message saying it is a drawn game
                    //TODO

                    JOptionPane.showMessageDialog(null, "Draw!");

                }
            }
        }

        /**
         * Checks if the button in btnKeypad array in positions i, j, k
         * all have the same text.
         * @param i - button number
         * @param j - button number
         * @param k - button number
         * @return true if button i,j,k
         */
        private boolean isAWinner(int i, int j, int k) {
            return !btnKeypad[i].getText().equals(" ")
                    && btnKeypad[i].getText().equals(btnKeypad[j].getText())
                    && btnKeypad[i].getText().equals(btnKeypad[k].getText());
        }

        /**
         * Checks for all possible winning scenarios
         * @return null if there is no winner or the text on the button of the
         * winner
         */
        public String checkForWinner() {
            /* Assumed mapping of button indexes to position in game is -
             *  012
             *  345
             *  678
             */
            int winner = -1; // -1 value means there was no winner

            if (isAWinner(0, 1, 2)) {// Horizontal row 1
                winner = 0;
            } else if (isAWinner(3, 4, 5)) {    // Horizontal row 2
                winner = 3;
            } else if (isAWinner(6, 7, 8)) {    // Horizontal row 3
                winner = 6;
            } else if (isAWinner(0, 3, 6)) {    // Vertical row 1
                winner = 0;
            } else if (isAWinner(1, 4, 7)) {    // Vertical row 2
                winner = 1;
            } else if (isAWinner(2, 5, 8)) {    // Vertical row 3
                winner = 2;
            } else if (isAWinner(0, 4, 8)) {    // Diagonal 1
                winner = 0;
            } else if (isAWinner(2, 4, 6)) {    // Diagonal 2
                winner = 2;
            }

            if (winner != -1) {
                return btnKeypad[winner].getText();
            } else {
                return null;
            }
        }
    }
}

class ButtonExitActionHandler implements ActionListener {

    /**
     * Inner class of NoughtsAndCrossesGameFrame to respond to the Play Button being
     * pressed. Asks the frame to reset the game by calling the frames resetGame
     * method
     */
    public void actionPerformed(ActionEvent e) {
        //this.dispose(gamePanel);
        System.exit(0);

    }
}

class ButtonPlayActionHandle implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        //resetGame();
    }
}

I have problem with NoughtsAndCrossesGame which the play button doesn't work which i couldn't write the play button function code. The rest works but the play button doesn't work used to restart the button. i will good if someone give me a solution so that i could understand. thank you for kindness

View Answers









Related Tutorials/Questions & Answers:
NoughtsAndCrossesGame play button doesn't work
NoughtsAndCrossesGame play button doesn't work   /* * To change... NoughtsAndCrossesGameFrame noughtsAndCrossesGame; noughtsAndCrossesGame = new...() { //** //* The button panel //*/ } class NoughtsAndCrossesButtonPanel
NoughtsAndCrossesGame play button doesn't work
NoughtsAndCrossesGame play button doesn't work   /* * To change... NoughtsAndCrossesGameFrame noughtsAndCrossesGame; noughtsAndCrossesGame = new...() { //** //* The button panel //*/ } class NoughtsAndCrossesButtonPanel
Advertisements
If statement doesn't work ,(doesn't print alert message when user dont field name and email)
If statement doesn't work ,(doesn't print alert message when user dont field name and email)  I've created some if / else statements to get a download when a user hit click jf he fields name and email but doesn"t work for my site
After Logout Back Button Should not work again
After Logout Back Button Should not work again  Hi deepak,, I am doing Login Page,.. IN That If Logout Then When I Click Logout Button It Should not work.. For That what is the logic And what Concept I have To Use.. Please
Running problem with NoughtsAndCrossesGame in blank
of NoughtsAndCrossesGameFrame to respond to the Play Button being * pressed...Running problem with NoughtsAndCrossesGame in blank  Hi i was having problem created NoughtsAndCrossesGame in end the it works but i runs the gui
JFrame Button click to start play
JFrame Button click to start play  i made a game and and i add a button in new jframe and i want when i click it it start to play...can anyone help me
Button
Button  How to use image for button in JSF
iPhone Button Click Sound
coding to play the sound. In my application i am using a button to give the action to play a sound. You can add and switch the button with action in Interface...... you can play the sound on clicking the button. ADS_TO_REPLACE_6 Download
profile doesn't match application identifier
profile doesn't match application identifier  profile doesn't match application identifier
button
button  can i give multiple commands on click of a button? if yes how can i do that?? multiple commands can we retriving dat from database, capturing data, moving to next page.. etc
button
button  can i give multiple commands on click of a button? if yes how can i do that?? multiple commands can be retriving dat from database, capturing data, moving to next page.. etc
JDBC doesn't provide an EOF method.
JDBC doesn't provide an EOF method.  How can I know when I reach the last record in a table, since JDBC doesn't provide an EOF method
Create a input button inside js function
this but doesn't seems to work. Lets show is the function being called function show... called function. I used something like this but doesn't seems to work. Lets show...Create a input button inside js function  I call my js function from
Why doesn't Java allow overriding of static methods?
Why doesn't Java allow overriding of static methods?  Why doesn't Java allow overriding of static methods
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'jpa.sequence' doesn't exist
' doesn't exist  Hi, I am developing an application using JPA 2.1... 'jpa.sequence' doesn't exist Error Code: 1146 Call: UPDATE SEQUENCE SET SEQ...: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: T able 'jpa.sequence' doesn't exist Error
iPhone play, pause and stop playing music
iPhone play, pause and stop playing music This is the small iPhone SDK example that will show you how to play, pause and stop the music on button... you are going to play on button click. Once you done, just open your
jvm work
jvm work  wht is the work of jvm? deaply.   Hi Friend, Java Virtual Machine or JVM for short is a software execution engine to run the java programs. Java Virtual Machine is also known as "Java Interpreter" which
SQL select statement doesn't return any rows,
SQL select statement doesn't return any rows,  When an SQL select statement doesn't return any rows, is an SQLException thrown
why set doesn't allow duplicate value
why set doesn't allow duplicate value  HI Everyone Any one can help me why set not allowed duplicate value in java. Please post ur comment, its urgent. Thanks in advance
validator framework work in Struts
validator framework work in Struts  How does validator framework work in Struts
Message Resource Bundle work.
Message Resource Bundle work.  How does Value replacement in Message Resource Bundle work
hello .. still doesn't run - Java Beginners
hello .. still doesn't run  Iam still having a prblem in running this problem errors are: can not resolve symbol import.util.Scanner class... stupid alittle bit but this is my home work
ModuleNotFoundError: No module named 'Work'
ModuleNotFoundError: No module named 'Work'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'Work' How to remove the ModuleNotFoundError: No module named 'Work' error
MySQL issue: Table 'data_dictionary.CHARACTER_SETS' doesn't exist
MySQL issue: Table 'data_dictionary.CHARACTER_SETS' doesn't exist  MySQL issue: Table 'datadictionary.CHARACTERSETS' doesn't exist
Hibernate Envers - REVINFO table doesn't exist
Hibernate Envers - REVINFO table doesn't exist  Hi, In my project I have to save the data change log. For this we are using the Hibernate Envers...: Hibernate Envers - REVINFO table doesn't exist How to resolve this? Thanks
how formBackingObject() method will work.
how formBackingObject() method will work.  How the formBackingObject method will behave
collection frame work
collection frame work  explain all the concept in collection frame work
work - JSP-Servlet
JSP Work Directory   What is the absolute path for the JSP working directory
validate() method of ActionForm work
validate() method of ActionForm work  How does validate() method of ActionForm work
collection frame work
collection frame work  explain the hierarchy of one dimensional collection frame work classes
collection frame work
collection frame work  could you please tell me detail the concept of collection frame work
How to work with Ajax in spring
How to work with Ajax in spring  give some sample code for ajax with spring (example like if i select a state from one drop down in another drop down related districts should come
MySQl 5.5 doesn't truncate VARCHARs when they're longer than expected
MySQl 5.5 doesn't truncate VARCHARs when they're longer than expected  MySQl 5.5 doesn't truncate VARCHARs when they're longer than expected
ios work with nsdictionary
ios work with nsdictionary  How to work with NSDictionary in Objective C?   Working with NSDictionary in Objective C Basically, in Objective C NSDictionary is used to create and retrive the Key values for objectecs
sing validator framework work in struts
sing validator framework work in struts  How does client side validation using validator framework work in struts
which tags work on which browsers
which tags work on which browsers  Is there a site that shows which tags work on which browsers
JFreeChart dosn't work
JFreeChart dosn't work  Hello everybody I'm trying to make a line chart from JFreechart, but I just can't get it to work. The graph get its data... it, an error occurs (i tried an example from here, with XY, and it didn't work either) I
spring MVC frame work - Spring
spring MVC frame work  Example of spring MVC frame work
wnidows button
wnidows button   How to handle windows back and forward button in struts applications, without using java script
ModuleNotFoundError: No module named 'har-work'
ModuleNotFoundError: No module named 'har-work'  Hi, My Python...-work' How to remove the ModuleNotFoundError: No module named 'har-work... to install padas library. You can install har-work python with following
ModuleNotFoundError: No module named 'work_h'
ModuleNotFoundError: No module named 'work_h'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'work_h' How to remove the ModuleNotFoundError: No module named 'work_h'
ModuleNotFoundError: No module named 'work_h'
ModuleNotFoundError: No module named 'work_h'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'work_h' How to remove the ModuleNotFoundError: No module named 'work_h'
ModuleNotFoundError: No module named 'work-queue'
ModuleNotFoundError: No module named 'work-queue'  Hi, My Python... 'work-queue' How to remove the ModuleNotFoundError: No module named 'work... have to install padas library. You can install work-queue python
ModuleNotFoundError: No module named 'work-toto'
ModuleNotFoundError: No module named 'work-toto'  Hi, My Python... 'work-toto' How to remove the ModuleNotFoundError: No module named 'work... have to install padas library. You can install work-toto python with following
ModuleNotFoundError: No module named 'yzs-work'
ModuleNotFoundError: No module named 'yzs-work'  Hi, My Python...-work' How to remove the ModuleNotFoundError: No module named 'yzs-work... to install padas library. You can install yzs-work python with following
ModuleNotFoundError: No module named 'har-work'
ModuleNotFoundError: No module named 'har-work'  Hi, My Python...-work' How to remove the ModuleNotFoundError: No module named 'har-work... to install padas library. You can install har-work python with following
ModuleNotFoundError: No module named 'simple_work'
ModuleNotFoundError: No module named 'simple_work'  Hi, My Python... 'simple_work' How to remove the ModuleNotFoundError: No module named 'simple_work' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'work_h'
ModuleNotFoundError: No module named 'work_h'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'work_h' How to remove the ModuleNotFoundError: No module named 'work_h'
ModuleNotFoundError: No module named 'work-queue'
ModuleNotFoundError: No module named 'work-queue'  Hi, My Python... 'work-queue' How to remove the ModuleNotFoundError: No module named 'work... have to install padas library. You can install work-queue python
ModuleNotFoundError: No module named 'work-weixin'
ModuleNotFoundError: No module named 'work-weixin'  Hi, My Python... 'work-weixin' How to remove the ModuleNotFoundError: No module named 'work-weixin' error? Thanks   Hi, In your python environment

Ads