Results 1 to 10 of 10
  1. #1
    Razorfc is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Noughts And Crosses v2 help adding the buttons for play an exit button be

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






    public NoughtsAndCrossesGameFrame() {


    /**
    * Asks the panel containing the game to reset the game.
    */



    }

    private void resetGame() {


    //**
    //* The button panel
    //*/
    }

    class NoughtsAndCrossesButtonPanel extends JPanel {

    /**
    * The game panel
    */


    }
    }

    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
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CL OSE);
    this.setTitle("Noughts and Crosses");




    // 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(.....);
    this.setLayout(new BorderLayout());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.setLayout(new GridLayout(3, 0));
    this.setSize(200, 200);
    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(250, 250);

    }

    private void resetGame() {
    /**
    * Disables all the buttons when the game ends
    */
    }

    private void gameFinished() {
    /**
    * Inner class of NoughtsAndCrossesGamePanel to handle a players shot
    * ie when a game button is pressed.
    */
    }

    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!");
    //NoughtsAndCrossesGamePanel.this.dispose();
    }
    }
    }

    /**
    * 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)
    {
    System.exit(0);
    }

    }

    class ButtonPlayActionHandle implements ActionListener {
    public void actionPerformed(ActionEvent e)
    {
    //resetGame();
    }
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Noughts And Crosses v2 help adding the buttons for play an exit button be

    Please use [code] tags [/code] when posting code.
    That's unreadable without them.

    And you haven't actually asked a question.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Razorfc is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Noughts And Crosses v2 help adding the buttons for play an exit button be

    I don't know how to add the play and exit button in order this applications works. Try many hours to get it work but it doesn't which i kinda struggle

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Noughts And Crosses v2 help adding the buttons for play an exit button be

    So.
    What exact problems are you having?
    As I said, I can't read that code as it stands, and there is far too much in there in any case.
    You've got a lot of Swing in there, so I can't see how adding a button is going to be a problem?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Razorfc is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Noughts And Crosses v2 help adding the buttons for play an exit button be

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






    public NoughtsAndCrossesGameFrame() {


    /**
    * Asks the panel containing the game to reset the game.
    */



    }

    private void resetGame() {


    //**
    //* The button panel
    //*/
    }

    class NoughtsAndCrossesButtonPanel extends JPanel {

    /**
    * The game panel
    */


    }
    }

    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
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CL OSE);
    this.setTitle("Noughts and Crosses");




    // 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(.....);
    this.setLayout(new BorderLayout());
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.setLayout(new GridLayout(3, 0));
    this.setSize(200, 200);
    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(250, 250);

    }

    private void resetGame() {
    /**
    * Disables all the buttons when the game ends
    */
    }

    private void gameFinished() {
    /**
    * Inner class of NoughtsAndCrossesGamePanel to handle a players shot
    * ie when a game button is pressed.
    */
    }

    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!");
    //NoughtsAndCrossesGamePanel.this.dispose();
    }
    }
    }

    /**
    * 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)
    {
    System.exit(0);
    }

    }

    class ButtonPlayActionHandle implements ActionListener {
    public void actionPerformed(ActionEvent e)
    {
    //resetGame();
    }
    }

    Please i need a solution to work coz i'm very struggle atm oh else i failed java. Doesn't know how to add button with exit and restart with a count

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Noughts And Crosses v2 help adding the buttons for play an exit button be

    You'll go far.
    But if you should choose to calm down a minute, I did ask that you use [code] tags [/code], because that code is still unreadable.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Razorfc is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Noughts And Crosses v2 help adding the buttons for play an exit button be

    Java Code:
    /*
     * 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;
        
        
        
    
        
    
        public NoughtsAndCrossesGameFrame() {
            
            
                    /**
    * Asks the panel containing the game to reset the game.
    */
                   
                    
                    
        }
    
        private void resetGame() {
            
            
            //**
    //* The button panel
    //*/
        }
    
        class NoughtsAndCrossesButtonPanel extends JPanel {
        
          /**
    * The game panel
    */
    
    
        }
        }
    
        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
                this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
               this.setTitle("Noughts and Crosses");
    
                
               
               
                // 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(.....);
                this.setLayout(new BorderLayout());
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                this.setLayout(new GridLayout(3, 0));
                this.setSize(200, 200);
                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(250, 250);
    
            }
    
            private void resetGame() {
               /**
    * Disables all the buttons when the game ends
    */
            }
    
            private void gameFinished() {
                /**
    * Inner class of NoughtsAndCrossesGamePanel to handle a players shot
    * ie when a game button is pressed.
    */
            }
    
            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!");
                            //NoughtsAndCrossesGamePanel.this.dispose();
                        }
                    }
                }
    
                /**
                 * 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)
    		{
    			System.exit(0);
    		}
               
            }
    
            class ButtonPlayActionHandle implements ActionListener {
                public void actionPerformed(ActionEvent e)
    		{
    			//resetGame();
    		}
            }
    Last edited by Razorfc; 03-07-2012 at 06:13 PM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Noughts And Crosses v2 help adding the buttons for play an exit button be

    [] brackets, not <>.
    Anyway, what does this do at the moment?
    Does it compile?
    Does it run?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Razorfc is offline Member
    Join Date
    Mar 2012
    Posts
    27
    Rep Power
    0

    Default Re: Noughts And Crosses v2 help adding the buttons for play an exit button be

    Try to add the play and exit button in the code however i did the previous project the NoughtsAndCrossesGame without adding the button, but now they ask me to add the two buttons one is the play the game and another is the exit button which took me few hours to figure it out. It doesn't work unless the button must be add. DOne few codes atm.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: Noughts And Crosses v2 help adding the buttons for play an exit button be

    I honestly can't see where you're stuck.
    You need two buttons, Play and Exit, so add them to your panel wherever you feel they look OK.
    Assign listeners to them (which you know how to do as well).
    You already have placeholder code for those, though I would personally make them both anonymous listeners as neither does a great deal. The Exit simply calls exit(), and the Play calls resetGame(). But if you haven't done anonymous classes then stick to what you know.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Java beginner- Noughts and Crosses help
    By Trevers89 in forum New To Java
    Replies: 10
    Last Post: 07-05-2011, 02:58 PM
  2. Adding a sqrt button and percentage button to a calculator
    By Josie_Taylor in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 02:16 AM
  3. Replies: 0
    Last Post: 04-28-2010, 02:39 PM
  4. Creating Clear and Exit Buttons
    By flambookey in forum New To Java
    Replies: 2
    Last Post: 03-24-2010, 06:08 PM
  5. Adding EXIT button on MIDlet form
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-22-2007, 11:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •