Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24
  1. #21
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here is some code that shows the points I've made above. It was deliberately made incomplete.

    Java Code:
    public class Game extends JFrame implements ActionListener
    {
      // constant String to avoid mistakes
      private static final String CLEAR = "Clear";
      // .... code deleted
    
      // 2 dimension jbutton array
      JButton[][] buttons = new JButton[3][3];
      
      public Game()
      {
        // .... code deleted
    
        ButtonListener btnListener = new ButtonListener();
        for (int row = 0; row < buttons.length; row++)
        {
          for (int col = 0; col < buttons[row].length; col++)
          {
            // create a new button
            buttons[row][col] = new JButton("");
            
            // cheating just a little here.  
            // using component's name to store row and col info
            buttons[row][col].setName("" + row + col);
            buttons[row][col].addActionListener(btnListener);
            window.add(buttons[row][col]);
          }
        }
    
        // ... code deleted
        MenuItem mnuEditClear = new MenuItem(CLEAR);
        mnuEditClear.setActionCommand(CLEAR);
        mnuEdit.add(mnuEditClear);
    
        // ...... code deleted
      }
      
      private class ButtonListener implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          JButton btn = (JButton)e.getSource();
          
          // if the button has not yet been pressed
          if (btn.getText().isEmpty())
          {
            // only increase count if empty button pressed
            count++;
            
            // if count even, letter is "O", else "X"
            letter = (count % 2 == 0) ? "O" : "X";
    
            btn.setText(letter);
            
            // extract row and col numbers from button's name String
            String rowColString = btn.getName();
            int row = Integer.parseInt(rowColString.substring(0, 1));
            int col = Integer.parseInt(rowColString.substring(1, 2));
    
            //this method needs to be created
            checkWin(row, col, letter);
            
            //TODO: delete the lines below
            System.out.println("<debug statement, delete later> row: " + 
              row + ", col: " + col);
          }
        }
      }
    
      // method to check if player won
      public void checkWin(int row, int col, String ltr)
      {
        // TODO Create this
      }
    
      public void actionPerformed(ActionEvent e)
      {
        // .... code deleted
    
        // constant String used to avoid mistakes
        if (arg.equals(CLEAR))
        {
          // .... code deleted
          for (int row = 0; row < buttons.length; row++)
          {
            for (int col = 0; col < buttons[row].length; col++)
            {
              buttons[row][col].setText("");
            }
          }
        }
        // .... code deleted
      }
    
      // .... code deleted
    }

  2. #22
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by valiantofficer View Post
    Please keep in mind I am relatively new to java. What exactly is an array, and which lines specifically should be in one? Thanks for the help, btw. I just never had much instruction on this when I was in school.
    For information about arrays: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    I would strongly advise you to review the other basic sections of the tutorials before venturing too deep into Swing. It will help immensely.

  3. #23
    Join Date
    Apr 2009
    Posts
    11
    Rep Power
    0

    Default

    I'll look into it.

  4. #24
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Another option is to create a nested class that extends JButton, handle the actions within that class, and add instances of that class to your JFrame. This would simplify the code further, and would make the code more recyclable/reusable. Make the nested class extend JButton and implement ActionListener. Then, in the actionPerformed method, just use code such as the example below.

    Java Code:
    private void configureTurn(){
         switch(letter){
              case 'X' : letter = 'X'; break;
              case 'Y' : letter = 'Y'; break;
              default  : letter = 'X'; break;
         }
    }
    
    private class MyButton extends JButton{
         public void actionPerformed(ActionEvent e){
              configureTurn();
              setText(letter);
         }
    }
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  2. Java tictactoe
    By frans121 in forum New To Java
    Replies: 3
    Last Post: 02-19-2009, 03:54 AM
  3. TicTacToe
    By Thomas Covington in forum New To Java
    Replies: 1
    Last Post: 04-10-2008, 04:51 AM
  4. TicTacToe Game
    By Ebtihal in forum New To Java
    Replies: 0
    Last Post: 01-09-2008, 11:01 AM
  5. TicTacToe
    By Joe3161 in forum New To Java
    Replies: 4
    Last Post: 11-25-2007, 06:47 PM

Posting Permissions

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