Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 52
  1. #21
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    I understand that but haven't enough time right now. I do not know how to set the variables back to the start after I hit clear. It is why I am here asking where to start.

  2. #22
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Do you see which code block runs when you hit clear?

  3. #23
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Java Code:
    	clearAction.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent g){
    	String arg= g.getActionCommand();{
    	if (arg.equals("Clear")){
    	for (TicTacToeButton current : allButtons){
    	current.setLabel(" ");
    	playerTurn.setText("");
    
    				}
    			}
    		}
    	}
    });

  4. #24
    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 Big Bundy View Post
    I understand that but haven't enough time right now. I do not know how to set the variables back to the start after I hit clear. It is why I am here asking where to start.
    "I haven't enough time"?

    Sorry, but this isn't a do my homework for me because I don't have time, sort of site. If you're willing to put in the work to learn, we'll be more than happy to help, but if you're mainly looking for some place to get your homework done quickly, you're best off going some place else.

  5. #25
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Sorry but this isn't any homework. And I've gotten help already that I appreciate it very much. I like my code written the way it is. If you can't help because you can't "read" it then I can't help you either.

  6. #26
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Yeah, I agree with Fubarable.

    But concerning that code block, just simply add the required commands to reset the variables.

    EDIT: Actually it's the other way around, you help us "read" first for us to help you!

  7. #27
    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 Big Bundy View Post
    Sorry but this isn't any homework. And I've gotten help already that I appreciate it very much. I like my code written the way it is. If you can't help because you can't "read" it then I can't help you either.
    Why can't you take the time to format your code according Java standards? Doing so is usually appreciated by all who read your code and posts.

  8. #28
    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 ra4king View Post
    EDIT: Actually it's the other way around, you help us "read" first for us to help you!
    Amen! It's the person asking for free advice that needs to put in the effort to help others help him.

  9. #29
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    So where do I begin. Is there some sort of reset method in java? And what variables should I reset?

    Quote Originally Posted by ra4king View Post
    Yeah, I agree with Fubarable.

    But concerning that code block, just simply add the required commands to reset the variables.

  10. #30
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    And Fubarable; whats your ways to fixing my code? What do I need to "indent" so people can read my code. I guess Im not understanding what I need to "fix".

  11. #31
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Indenting is adding spacing and beautifying the way your code looks, not the actual code itself....
    This makes it easier for our eyes to automatically see blocks of code.

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

  13. #33
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Ok Sorry but is this a little better? :/

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.font.*;
    import javax.swing.*;
    import java.util.ArrayList;
    //import java.text.*;
    
    public class tictactoe extends Frame{
    
    
    Font font = new Font("Dialog", Font.BOLD, 18);
    Font font2 = new Font("Dialog", Font.ITALIC, 20);
    String turn = "O"; //value is either "O" or "X"
    Panel board;
    TicTacToeButton button[][];
    Label playerTurn;
    ButtonHandler bH = new ButtonHandler();
    int usedCells = 0; //number of cells in use
    public static ArrayList<TicTacToeButton> allButtons= new ArrayList<TicTacToeButton>();
    
    
    
    tictactoe(String title) {
    super(title);
    board = new Panel();
    button = new TicTacToeButton[3][];
    playerTurn = new Label("Player 1's Turn");
    playerTurn.setFont(font2);
    playerTurn.setForeground(Color.white);
    }
    
    void launchGame() {
    setBackground(Color.red);
    board.setLayout(new GridLayout(3, 3, 6, 6));
    
    
    //setMenuBar//
    MenuBar menuBar = new MenuBar();
    setMenuBar(menuBar);
    
    Menu fileMenu = new Menu("File");
    menuBar.add(fileMenu);
    
    MenuItem clearAction = new MenuItem("Clear");
    MenuItem aboutAction = new MenuItem("About");
    MenuItem exitAction = new MenuItem("Exit");
    
    fileMenu.add(clearAction);
    fileMenu.add(aboutAction);
    fileMenu.add(exitAction);
    
    
    
    	exitAction.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent e){
    	String arg = e.getActionCommand();{
    	if (arg.equals("Exit")){
    	System.exit(0);
    			}
     		}
    	}
    });
    
    	aboutAction.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent f){
    	String arg= f.getActionCommand();{
    	if (arg.equals("About")){
    	String message = " Shawn's TicTacToe \n Structured Programming Java";
    	JOptionPane.showMessageDialog(null,message,
    	"About Tic Tac Toe", JOptionPane.INFORMATION_MESSAGE);
    			}
    		}
    	}
    });
    
    	clearAction.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent g){
    	String arg= g.getActionCommand();{
    	if (arg.equals("Clear")){
    	for (TicTacToeButton current : allButtons){
    	current.setLabel(" ");
    	playerTurn.setText("");
    				}
    			}
    		}
    	}
    });
    
    
    /* initialize buttons */
    	for (int i = 0, count = 0; i < 3; i++) {
    	button[i] = new TicTacToeButton[3];
    	for (int j = 0; j < 3; j++, count++) {
    	button[i][j] = new TicTacToeButton();
    	allButtons.add(button[i][j]);
    
    
    
    /* inialize button values to 0,1, 2,...,8 */
    	button[i][j].value = (new Integer(count)).toString();
    	board.add(button[i][j]);
    
    
    
    /* add listeners to the buttons */
    	button[i][j].addActionListener(bH);
    	button[i][j].setFont(font);
    	button[i][j].setForeground(Color.green);
    	button[i][j].setBackground(Color.black);
    
    	}
    }
    
    /*add board*/
    	add(board);
    	board.setSize(300,300);
    	add(playerTurn, BorderLayout.SOUTH);
    
    
    
    /* add listeners for closing the frame */
    	addWindowListener( new WindowAdapter() {
    	public void windowClosing( WindowEvent event ) {
    	System.exit( 0 );
    	}
    });
    
    
    /* display */
    	pack();
    	setVisible(true);
    }
    
    class ButtonHandler implements ActionListener {
    void changeButtonLabel(TicTacToeButton b) {
    b.setLabel(turn);
    b.value = turn;
    
    
    /* remove listener */
    	b.removeActionListener(bH);
    	usedCells++;
    
    }
    
    
    public void actionPerformed(ActionEvent ae) {
    boolean win = false;
    boolean draw = false;
    int player = 0;
    TicTacToeButton b = (TicTacToeButton) ae.getSource();
    
    
    /* change content of button to O or X */
       changeButtonLabel(b);
    
    
    /* check for a win */
    
    /* horizontal */
    	for (int i = 0; i < 3; i++) {
    	if ((button[i][0].value).equals(button[i][1].value) &&
    	(button[i][0].value).equals(button[i][2].value)) {
    	win = true;
    	}
    }
    
    
    /* vertical */
    	for (int i = 0; i < 3; i++) {
    	if ((button[0][i].value).equals(button[1][i].value) &&
    	(button[0][i].value).equals(button[2][i].value)) {
    	win = true;
    	}
    }
    
    
    /* diagonal */
               if ((button[0][0].value).equals(button[1][1].value) &&
    	(button[0][0].value).equals(button[2][2].value)) {
    	win = true;
    
               } else if ((button[0][2].value).equals(button[1][1].value) &&
               (button[0][2].value).equals(button[2][0].value)) {
               win = true;
    }
    
    
    /* check for a draw */
    	if (!win) {
    	if (usedCells==9) {
    	draw = true;
    	}
    }
    
    
    /* Change message */
    	String message = "";
    
    
    if (win) {
    if (turn.equals("O")) {
    player = 1;
    } else {
    player = 2;
    }
    message += "Player " + player + " wins!";
    
    
    /* remove all listeners */
    	for (int i = 0; i < 3; i++) {
    	for (int j = 0; j < 3; j++) {
    	button[i][j].removeActionListener(bH);
    	}
    }
    } else if (draw) {
    message += "It's a draw!";
    for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
    button[i][j].removeActionListener(bH);
    	}
    }
    } else {
    
    
    /* change turn */
    	if (turn.equals("O")){
    	turn = new String("X");
    	player = 2;
    	message += "Player 2's Turn";
    
    	} else {
    	turn = new String("O");
    	player = 1;
    	message += "Player 1's Turn";
    	}
    }
    playerTurn.setText(message);
    	}
    }
    
    class TicTacToeButton extends Button {
    String value;
    }
    
    
    
    public static void main(String args[]) {
    tictactoe game = new tictactoe("Tic-Tac-Toe");
    game.launchGame();
    	}
    }
    Last edited by Big Bundy; 04-15-2011 at 03:23 AM. Reason: code

  14. #34
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Can you lead me on how to reset these variables after the board is cleared?

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

    Default

    Clear's not going to do much because you're removing all of the listeners for your buttons once someone wins or draws:

    Java Code:
                for (int i = 0; i < 3; i++) {
                   for (int j = 0; j < 3; j++) {
                      button[i][j].removeActionListener(bH);
                   }
                }

    I suggest that you don't do this but rather use a win or gameOver boolean variable to decide whether a button will respond or not, and then reset this variable to false in the clear button's action listener.

    Here is one way to format your code:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.ArrayList;
    
    //import java.text.*;
    public class tictactoe extends Frame {
       Font font = new Font("Dialog", Font.BOLD, 18);
       Font font2 = new Font("Dialog", Font.ITALIC, 20);
       String turn = "O"; // value is either "O" or "X"
       Panel board;
       TicTacToeButton button[][];
       Label playerTurn;
       ButtonHandler bH = new ButtonHandler();
       int usedCells = 0; // number of cells in use
       public static ArrayList<TicTacToeButton> allButtons = new ArrayList<TicTacToeButton>();
    
       tictactoe(String title) {
          super(title);
          board = new Panel();
          button = new TicTacToeButton[3][];
          playerTurn = new Label("Player 1's Turn");
          playerTurn.setFont(font2);
          playerTurn.setForeground(Color.white);
       }
    
       void launchGame() {
          setBackground(Color.red);
          board.setLayout(new GridLayout(3, 3, 6, 6));
          // setMenuBar//
          MenuBar menuBar = new MenuBar();
          setMenuBar(menuBar);
          Menu fileMenu = new Menu("File");
          menuBar.add(fileMenu);
          MenuItem clearAction = new MenuItem("Clear");
          MenuItem aboutAction = new MenuItem("About");
          MenuItem exitAction = new MenuItem("Exit");
          fileMenu.add(clearAction);
          fileMenu.add(aboutAction);
          fileMenu.add(exitAction);
          exitAction.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                String arg = e.getActionCommand();
                {
                   if (arg.equals("Exit")) {
                      System.exit(0);
                   }
                }
             }
          });
          aboutAction.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent f) {
                String arg = f.getActionCommand();
                {
                   if (arg.equals("About")) {
                      String message = " Shawn's TicTacToe \n Structured Programming Java";
                      JOptionPane.showMessageDialog(null, message, "About Tic Tac Toe",
                               JOptionPane.INFORMATION_MESSAGE);
                   }
                }
             }
          });
          clearAction.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent g) {
                String arg = g.getActionCommand();
                {
                   if (arg.equals("Clear")) {
                      for (TicTacToeButton current : allButtons) {
                         current.setLabel(" ");
                         playerTurn.setText("");
                      }
                   }
                }
                turn = "O";
             }
          });
          /* initialize buttons */
          for (int i = 0, count = 0; i < 3; i++) {
             button[i] = new TicTacToeButton[3];
             for (int j = 0; j < 3; j++, count++) {
                button[i][j] = new TicTacToeButton();
                allButtons.add(button[i][j]);
                /* inialize button values to 0,1, 2,...,8 */
                button[i][j].value = (new Integer(count)).toString();
                board.add(button[i][j]);
                /* add listeners to the buttons */
                button[i][j].addActionListener(bH);
                button[i][j].setFont(font);
                button[i][j].setForeground(Color.green);
                button[i][j].setBackground(Color.black);
             }
          }
          /* add board */
          add(board);
          board.setSize(300, 300);
          add(playerTurn, BorderLayout.SOUTH);
          /* add listeners for closing the frame */
          addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent event) {
                System.exit(0);
             }
          });
          /* display */
          pack();
          setVisible(true);
       }
    
       class ButtonHandler implements ActionListener {
          void changeButtonLabel(TicTacToeButton b) {
             b.setLabel(turn);
             b.value = turn;
             /* remove listener */
             b.removeActionListener(bH);
             usedCells++;
          }
    
          public void actionPerformed(ActionEvent ae) {
             System.out.println("here");
             boolean win = false;
             boolean draw = false;
             int player = 0;
             TicTacToeButton b = (TicTacToeButton) ae.getSource();
             /* change content of button to O or X */
             changeButtonLabel(b);
             /* check for a win */
             /* horizontal */
             for (int i = 0; i < 3; i++) {
                if ((button[i][0].value).equals(button[i][1].value)
                         && (button[i][0].value).equals(button[i][2].value)) {
                   win = true;
                }
             }
             /* vertical */
             for (int i = 0; i < 3; i++) {
                if ((button[0][i].value).equals(button[1][i].value)
                         && (button[0][i].value).equals(button[2][i].value)) {
                   win = true;
                }
             }
             /* diagonal */
             if ((button[0][0].value).equals(button[1][1].value)
                      && (button[0][0].value).equals(button[2][2].value)) {
                win = true;
             } else if ((button[0][2].value).equals(button[1][1].value)
                      && (button[0][2].value).equals(button[2][0].value)) {
                win = true;
             }
             /* check for a draw */
             if (!win) {
                if (usedCells == 9) {
                   draw = true;
                }
             }
             /* Change message */
             String message = "";
             if (win) {
                if (turn.equals("O")) {
                   player = 1;
                } else {
                   player = 2;
                }
                message += "Player " + player + " wins!";
                /* remove all listeners */
                for (int i = 0; i < 3; i++) {
                   for (int j = 0; j < 3; j++) {
                      button[i][j].removeActionListener(bH);
                   }
                }
             } else if (draw) {
                message += "It's a draw!";
                for (int i = 0; i < 3; i++) {
                   for (int j = 0; j < 3; j++) {
                      button[i][j].removeActionListener(bH);
                   }
                }
             } else {
                /* change turn */
                if (turn.equals("O")) {
                   turn = new String("X");
                   player = 2;
                   message += "Player 2's Turn";
                } else {
                   turn = new String("O");
                   player = 1;
                   message += "Player 1's Turn";
                }
             }
             playerTurn.setText(message);
          }
       }
    
       class TicTacToeButton extends Button {
          String value;
       }
    
       public static void main(String args[]) {
          tictactoe game = new tictactoe("Tic-Tac-Toe");
          game.launchGame();
       }
    }

    Notice how code in the same block is indented the same amount so that it's easy to see when one block begins and when it ends and when one block is nested in another. Does this seem more readable to you?

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

    Default

    Also, why code this in AWT which is out of date by over 12 years rather than Swing which is only out of date by 4 or so years? Swing is much more flexible and robust.

  17. #37
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Ok thanks for the advice. So you suggest I should take out the Clear all together. Can you lead me into syntax for adding the boolean variable?

  18. #38
    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 Big Bundy View Post
    Ok thanks for the advice. So you suggest I should take out the Clear all together.

    No, I suggest the opposite. I suggest that you leave in clear, but that you don't remove the listeners from all of your buttons when someone wins or ties. If you do this, your buttons all become unresponsive and the game won't play.


    Can you lead me into syntax for adding the boolean variable?
    No! Asking for someone to "lead me into syntax" is not how you learn. Please at least try to do this on your own first. A little initiative goes a long way.

  19. #39
    Big Bundy is offline Member
    Join Date
    Mar 2011
    Posts
    42
    Rep Power
    0

    Default

    Ok so I need to remove the following? Am I correct

    Java Code:
    /* remove all listeners */
    	for (int i = 0; i < 3; i++) {
    	for (int j = 0; j < 3; j++) {
    	button[i][j].removeActionListener(bH);
    	}
    Java Code:
    } else if (draw) {
    message += "It's a draw!";
    for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
    button[i][j].removeActionListener(bH);//////////remove
    	}

  20. #40
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Maybe. You need to figure stuff out yourself. Asking for help is fine, but expecting step by step help is only going to hurt you. I understand it is challenging but you need to push through the challenge and keep working to figure it out.

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  3. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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