NoughtsAndCrossesGame play button doesn't work

Ads
 

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

Ads









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
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
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
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
profile doesn't match application identifier
profile doesn't match application identifier  profile doesn't match application identifier
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
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
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
wnidows button
wnidows button   How to handle windows back and forward button in struts applications, without using java script
radio button
radio button  On selecting a radio button, it should open a aspx page
html button
html button  when i click the submit button the entered data will be displayed below the submit button please help me
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
collection frame work
collection frame work  explain all the concept in collection frame work
coding of button
coding of button  What is code of fetching the value of button in a textbox using javascript
flogin button
flogin button  how to pass redirctt url for flogin button...and to retrieve parameters that are passed in that url..to store in our database
create a button
create a button  i have created button inhtml but my jsp is not working,so can u guide me
Back button
Back button  how to redirect to a home page when a back button is pressed in jsp instead of displaying the previous page
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
Print Button
Print Button  i want the source code for printing details of jform.. so plz help me for source code of print button
uikeyboard done button/return button
uikeyboard done button/return button  Hi everyone!!! Just worried about ..how to return the UIKeyboard on done or return button click in my iPhone... to return the uikeyboard on done button click. Do not forget to call <
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 display button as a link in vaadin?
How to display button as a link in vaadin?  Hello, I am working on a Vaadin based web application. I want the button to be displayed like link but it should work like a button. I would like to attach the event just like
button tag
button tag  how do i change button color to black. meant to say i want black button with white text on it   Hi Friend, Try the following... Message: <input type="text" id="msg"><br> <input type="button" value

Ads