1. If you're receiving a message that you are banned from the Current Events or Politics forums, it's not you specifically: those forums have been hidden for all users. For more info, see here.
    Dismiss Notice

My noughts and crosses Java GUI game

Discussion in 'Programming & Software Development' started by unwritt3n, Aug 19, 2007.

  1. unwritt3n

    unwritt3n Member

    Joined:
    Oct 26, 2006
    Messages:
    1,400
    Location:
    Melbourne
    hey guys,

    On the home strech now with my Java GUI game of Noughts and Crosses I've made!!!

    I just got 1 thing I want to do...

    I am lost with how to code the expression in an if statement that will output "game over" in a JOptionPane, in the event that no one wins.
    Everything else works fine in my original code.

    You can see in the code below where I need this if statement to go. I have filled the brackets with question-marks to point it out (it's in red). It's near the bottom:

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JOptionPane;
    
    public class NoughtsAndCrosses2 extends JFrame implements ActionListener {
        
        private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9;
        private Container cn;
        private GridLayout layout;
        private int turnCount = 0;
        private String s = "";   
        
        
        public NoughtsAndCrosses2() { //constructor
            b1 = new JButton("");
            b2 = new JButton("");
            b3 = new JButton("");
            b4 = new JButton("");
            b5 = new JButton("");
            b6 = new JButton("");
            b7 = new JButton("");
            b8 = new JButton("");
            b9 = new JButton("");
            
            cn = this.getContentPane(); 
            layout = new GridLayout(3,3); //create 3x3 grid
            cn.setLayout(layout);
            
            //Start adding the buttons:
            cn.add(b1);
            cn.add(b2);
            cn.add(b3);
            cn.add(b4);
            cn.add(b5);
            cn.add(b6);
            cn.add(b7);
            cn.add(b8);
            cn.add(b9);
            //End adding the buttons
            
            //Start register components with the ActionListener:
            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);
            b4.addActionListener(this);
            b5.addActionListener(this);
            b6.addActionListener(this);
            b7.addActionListener(this);
            b8.addActionListener(this);
            b9.addActionListener(this); 
            //End register components with the ActionListener
            
                                     }
                                     
                                         
                         
        public void actionPerformed(ActionEvent e) {
            
            //Start whose turn it is:
            s = Integer.toString(++turnCount);
            if (turnCount %2 == 0)
                    {
                s = "O";
                    }
            else
                    {
                s = "X";
                    }
            //End whose turn it is                
            
          
          if (e.getSource() == b1) { //If B1 press, set the text to "S"
              b1.setText(s);       }
              
          else if (e.getSource() == b2) { //If B2 press, set the text to "S"
              b2.setText(s);            }
            
          else if (e.getSource() == b3) { //If B3 press, set the text to "S"
              b3.setText(s);            }
                                   
          else if (e.getSource() == b4){ //If B4 press, set the text to "S"
              b4.setText(s);           }                    
                                   
          else if (e.getSource() == b5) { //If B5 press, set the text to "S"
              b5.setText(s);            }             
                                   
          else if (e.getSource() == b6){ //If B6 press, set the text to "S"
              b6.setText(s);           }                              
                                                                            
          else if (e.getSource() == b7) { //If B7 press, set the text to "S"
              b7.setText(s);            }                                                                     
                                                                             
          else if (e.getSource() == b8) { //If B8 press, set the text to "S"
              b8.setText(s);            }                   
                                                  
          else if (e.getSource() == b9){ //If B9 press, set the text to "S"
              b9.setText(s);           }
           
          else
           
            System.out.println("ERROR");
            
            
      if(b1.getText() == b2.getText() && b1.getText() == b3.getText() && ! b1.getText().equals("")) {
          
             JOptionPane.showMessageDialog(null,"Congratulations! Player "  + s + " wins." ,"Winner", JOptionPane.PLAIN_MESSAGE);
             
             b1.setText(""); b2.setText(""); b3.setText(""); b4.setText(""); b5.setText(""); b6.setText(""); b7.setText("");
             b8.setText(""); b9.setText("");
             
                                                                                                    }
                                                                  
      else if (b4.getText() == b5.getText() && b4.getText() == b6.getText() && ! b4.getText().equals("")) {
          
             JOptionPane.showMessageDialog(null,"Congratulations! Player "  + s + " wins." ,"Winner", JOptionPane.PLAIN_MESSAGE);
             
             b1.setText(""); b2.setText(""); b3.setText(""); b4.setText(""); b5.setText(""); b6.setText(""); b7.setText("");
             b8.setText(""); b9.setText("");
              
                                                                                                          } 
                                                                             
      else if (b7.getText() == b8.getText() && b7.getText() == b9.getText() && ! b7.getText().equals("")) {
          
             JOptionPane.showMessageDialog(null,"Congratulations! Player "  + s + " wins." ,"Winner", JOptionPane.PLAIN_MESSAGE);
             
             b1.setText(""); b2.setText(""); b3.setText(""); b4.setText(""); b5.setText(""); b6.setText(""); b7.setText("");
             b8.setText(""); b9.setText("");
                                                                                                          }  
              
                                                                                                          
      else if (b1.getText() == b4.getText() && b1.getText() == b7.getText() && ! b1.getText().equals("")) {
          
             JOptionPane.showMessageDialog(null,"Congratulations! Player "  + s + " wins." ,"Winner", JOptionPane.PLAIN_MESSAGE);
             
             b1.setText(""); b2.setText(""); b3.setText(""); b4.setText(""); b5.setText(""); b6.setText(""); b7.setText("");
             b8.setText(""); b9.setText("");
              
                                                                                                          }  
                                                                                                          
      else if (b2.getText() == b5.getText() && b2.getText() == b8.getText() && ! b2.getText().equals("")) {
          
             JOptionPane.showMessageDialog(null,"Congratulations! Player "  + s + " wins." ,"Winner", JOptionPane.PLAIN_MESSAGE);
             
             b1.setText(""); b2.setText(""); b3.setText(""); b4.setText(""); b5.setText(""); b6.setText(""); b7.setText("");
             b8.setText(""); b9.setText("");
              
                                                                                                          }  
                                                                                                          
      else if (b3.getText() == b6.getText() && b3.getText() == b9.getText() && ! b3.getText().equals("")) {
          
             JOptionPane.showMessageDialog(null,"Congratulations! Player "  + s + " wins." ,"Winner", JOptionPane.PLAIN_MESSAGE);
             
             b1.setText(""); b2.setText(""); b3.setText(""); b4.setText(""); b5.setText(""); b6.setText(""); b7.setText("");
             b8.setText(""); b9.setText("");
                
                                                                                                          }        
    
      else if (b1.getText() == b5.getText() && b1.getText() == b9.getText() && ! b1.getText().equals("")) {
          
             JOptionPane.showMessageDialog(null,"Congratulations! Player "  + s + " wins." ,"Winner", JOptionPane.PLAIN_MESSAGE);
             
             b1.setText(""); b2.setText(""); b3.setText(""); b4.setText(""); b5.setText(""); b6.setText(""); b7.setText("");
             b8.setText(""); b9.setText("");
                 
                                                                                                          }  
                                                                                                          
      else if (b3.getText() == b5.getText() && b3.getText() == b7.getText() && ! b3.getText().equals("")) {
          
             JOptionPane.showMessageDialog(null,"Congratulations! Player "  + s + " wins." ,"Winner", JOptionPane.PLAIN_MESSAGE);
             
             b1.setText(""); b2.setText(""); b3.setText(""); b4.setText(""); b5.setText(""); b6.setText(""); b7.setText("");
             b8.setText(""); b9.setText("");
                                                                                                          }
     
     
      [COLOR="Red"][B]else  if (??????????????????????????) {
                   
            JOptionPane.showMessageDialog(null,"Try again" ,"Game Over", JOptionPane.PLAIN_MESSAGE);
            
            b1.setText(""); b2.setText(""); b3.setText(""); b4.setText(""); b5.setText(""); b6.setText(""); b7.setText("");
            b8.setText(""); b9.setText("");
                                            }                      [/B][/COLOR]                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
    
                                                   }
                                                   
                                                   
        public static void main(String[] args) {
            
            NoughtsAndCrosses2 nac = new NoughtsAndCrosses2();
            nac.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            nac.setTitle("Noughts and Crosses v0.2");
            nac.setSize(600,600);
            nac.setVisible(true);
                                               }
                                                                             }
                                                                             
                                                                                                                                                                                             
                                                                     
                 
     
    Last edited: Aug 19, 2007
  2. mordy

    mordy Member

    Joined:
    Aug 30, 2001
    Messages:
    5,100
    Location:
    melb
    You seem to have coded your java in a very vb way.

    What you are meant to do is have a Game object which holds information about the game, which can then be polled to see if someone has won or not. The GUI should be a seperate class to display things about the game.

    obviously your code can look for lines or horizontal, but the problem is that you have set it up as seperate elements, not a char array, which would be easy to traverse.
     
  3. STIK79

    STIK79 Member

    Joined:
    Jun 21, 2002
    Messages:
    1,056
    Location:
    Adelaide
    Code:
    else if (turncount==9) {
      //reached here in 9 turns with no winner
      //reset all the squares
      turnCount =0;
    }
    
    should've used arrays - so many variables of the same type makes baby jesus cry!

    [ed] PS you've got no protection for if somebody clicks on a square with something already in it which fubars the whole game logic!
    you should also create methods for the many lines of repeated code!

    here's an example I just knocked up following your logic but simplifying things a bit - for reference not stealing lol (it's still a bit ugly but will give you some ideas hopefully)!
    Code:
    package noughtsandcrosses;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    /**
     *
     * @author Mat
     */
    public class NoughtsAndCrosses extends JFrame implements ActionListener {
    
        //Array of the buttons that make up the noughts and crosses board
        private JButton[] buttons;
        //JLabel to display the status of the game
        private JLabel status;
        //Number of turns in the game thus far
        private int turnCount = 0;
        //How many wins to X and O;
        private int winsX = 0;
        private int winsO = 0;
    
        public NoughtsAndCrosses() {
            super("Noughts and Crosses");
            JPanel noughtsAndCrosses = new JPanel(); //Panel to hold the noughts and Crosses
            status = new JLabel(); //Init the status Label
            status.setHorizontalAlignment(JLabel.CENTER);
            noughtsAndCrosses.setLayout(new GridLayout(3, 3));
            buttons = new JButton[9];
            for (int i = 0; i < 9; i++) {
                buttons[i] = new JButton();
                buttons[i].addActionListener(this);
                noughtsAndCrosses.add(buttons[i]);
            }
            getContentPane().add(noughtsAndCrosses, BorderLayout.CENTER);
            getContentPane().add(status, BorderLayout.SOUTH);
            resetBoard();
        }
    
        public void actionPerformed(ActionEvent e) {
            if (turnCount == -1) {
                resetBoard();
            } else {
                for (JButton button : buttons) {
                    if (e.getSource() == button) {
                        if (button.getText().equals("")) {
                            if (turnCount % 2 == 0) {
                                button.setText("X");
                                status.setText("In Progress: O to Play    Turn " + (turnCount + 1) + "       X wins = " + winsX + ", O wins = " + winsO);
                            } else {
                                button.setText("O");
                                status.setText("In Progress: X to Play    Turn " + (turnCount + 1) + "       X wins = " + winsX + ", O wins = " + winsO);
                            }
                            turnCount++;
                            checkVictory();
                        }
                    }
                }
            }
        }
    
        private void checkVictory() {
            //columns
            for (int i = 0; i < 3; i++) {
                if (!buttons[i].getText().equals("") && buttons[i].getText().equals(buttons[i + 3].getText()) && buttons[i].getText().equals(buttons[i + 6].getText())) {
                    displayWinnerMessage(buttons[i].getText());
                    return;
                }
            }
            //rows
            for (int i = 0; i < 9; i = i + 3) {
                if (!buttons[i].getText().equals("") && buttons[i].getText().equals(buttons[i + 1].getText()) && buttons[i].getText().equals(buttons[i + 2].getText())) {
                    displayWinnerMessage(buttons[i].getText());
                    return;
                }
            }
            //diagonals
            if (!buttons[4].getText().equals("") && buttons[4].getText().equals(buttons[0].getText()) && buttons[4].getText().equals(buttons[8].getText())) {
                displayWinnerMessage(buttons[4].getText());
                return;
            }
            if (!buttons[4].getText().equals("") && buttons[4].getText().equals(buttons[2].getText()) && buttons[4].getText().equals(buttons[6].getText())) {
                displayWinnerMessage(buttons[4].getText());
                return;
            }
            //Else draw
            if (turnCount == 9) {
                displayDrawMessage();
            }
        }
    
        private void displayWinnerMessage(String winner) {
            status.setText("Winner: " + winner + " wins!");
            status.setForeground(Color.RED);
            turnCount = -1;
            if (winner.equals("X")) {
                winsX++;
            } else {
                winsO++;
            }
        }
    
        private void displayDrawMessage() {
            status.setText("Draw!!");
            status.setForeground(Color.RED);
            turnCount = -1;
        }
    
        private void resetBoard() {
            for (JButton button : buttons) {
                button.setText("");
            }
            turnCount = 0;
            status.setText("New Game: X to Play            " + " X wins = " + winsX + ", O wins = " + winsO);
            status.setForeground(Color.BLACK);
        }
    
        public static void main(String[] args) {
            NoughtsAndCrosses nac = new NoughtsAndCrosses();
            nac.setSize(600, 600);
            nac.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            nac.setVisible(true);
        }
    }
    
     
    Last edited: Aug 19, 2007

Share This Page

Advertisement: