Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners NoughtsAndCrossesGame play button doesn't work
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Dexter
NoughtsAndCrossesGame play button doesn't work
0 Answer(s)      2 years and 7 months ago
Posted in : Java Beginners


/*
 * 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


Advertisement
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
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
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
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
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
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
iPhone Button Click Sound
the sound. In my application i am using a button to give the action to play...... you can play the sound on clicking the button. Download AVAudioplayer iPhone... iPhone Button Click Sound In this series of iPhone button tutorial, we
Play Lyrics on media player
Play Lyrics on media player  how to play lyrics on media player(MIDlet) while song is playing in background..its bit urgent... Thanks
audio file to play automatically
audio file to play automatically  I want to get an audio file to play automatically when someone visits my site
play videos - Java Beginners
play videos   How to play videos on line.how to embed the video player in the browser.If you can give some basic idea about this i would be thankful to you. Thanks
pause play NSTimer
pause play NSTimer  How to pause or play NSTimer?   NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myMethod:) userInfo:nil repeats:YES]; - (void
play SOund - Java3D
play SOund  I creat a player in j2me game, i use tasktimer to set schedule for it, but when i do this, memory increase continuously, it's about 20 minutes, it's out of memory, please help me  Hi Friend, Please visit
How to play radio station on iPhone
How to play radio station on iPhone  how to play radio station on iphone
Play framework dynamic form generation
Play framework dynamic form generation  I want to generate dynamic forms using play framework can any one give me some idea about dynamic form generation using play framework
profile doesn't match application identifier
profile doesn't match application identifier  profile doesn't match application identifier
Button
Button  How to use image for button in JSF
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
how to play audio track in java applat
ActionListener{ Button play,stop; AudioClip audioClip; public void init(){ play = new Button(" Play in Loop "); add(play); play.addActionListener...how to play audio track in java applat  Sir,i took help of coding
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
Play Audio in Java Applet
Play Audio in Java Applet      ... the sound file. This program will show you how to play a audio clip in your java applet... PlaySoundApplet.java to play sound. There are two buttons to play the sound
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
how to play video in website using jsp
how to play video in website using jsp   please help me friend how to play a video in webiste by using jsp which playres are used and how to play video please give me code
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
Play SWF file using Java - Applet
Play SWF file using Java  Hi I have task to design a screen saver using java. Now, I want to play .SWF (Flash file)in my Applet. Is there any way to play SWF from java class. Any reply will be appreciated. Thanks
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
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
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
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
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
Advertisements
 

 

 

DMCA.com