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

    Default

    Removing those actionListeners only causes the player not to go another turn. I do not believe it will matter after you clear the board everything should reset anyhow. Which I cannot figure out.

  2. #42
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.font.*;
    import java.util.ArrayList;
    import javax.swing.*;
    //import java.text.*;
    
    public class tictactoe extends Frame {
    
    //Color red = new Color(153,255,255);;
        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>();
     [COLOR="Red"]   public static ArrayList<tictactoe> allGames = new ArrayList<tictactoe>();[/COLOR]
    
        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);
    [COLOR="Red"]        allGames.add(this);[/COLOR]
        }
    
       public 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 (int i = 0, count = 0; i < 3; i++) {
    //		button[i] = new TicTacToeButton[3];
    //		for (int j = 0; j < 3; j++, count++) {
    //		button[i][j].setLabel("");
    //
    //
    //
    //}
    //}
                            for (TicTacToeButton current : allButtons) {
                                current.setLabel(" ");
                            }
    [COLOR="Red"]                        allGames.get(0).reinitialize();
                            allGames.get(0).launchGame();[/COLOR]
    
                        }
    
                    }
                }
            });
    
    
    //clearAction.setActionCommand("Clear");
    //aboutAction.setActionCommand("About");
    //exitAction.setActionCommand("Exit");
    
    
            /* 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);
            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 = "";
    
    //continued on next page
                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")) {
    //setBackground(Color.red);
                        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();
        }
    
    [COLOR="Red"]    public void reinitialize(){
            this.dispose();
            this.board.removeAll();
            allButtons.clear();
    
        }[/COLOR]
      
    }

    I've fixed it, but its a hack on a hack. Now you can continue playing after pressing Clear. I highlighted my new additions in red.
    Thx for the hint on CODE tags, whoever it was. I think I saw you on another gaming forum as well, a fellow game development enthusiast I see :D!
    Last edited by Maximus-EVG; 04-15-2011 at 06:00 AM. Reason: forgot to highlight change in constructor. Also, this whole class can benefit from being a singleton.

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

    Default

    Cool thanks. One hint..when the game restarts I need it to add playerTurn.setText (" ");
    So it says Player 1's Turn when the game shows.

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

    Default

    I basically have it so whoever wins it sets the text to Player " " Wins! and itcarries over to the next board. Do you know how I can fix this so it just says Player 1's Turn

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

    Default

    Change the label that says by using setText("Player 1's turn")....?

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

    Default

    Duhh :p ugghh long night!

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

    Default

    @maximus, please don't spoonfeed. I've seen a couple of posts where you gave the answer. While it may appear helpful, it really doesn't help posters to give them the direct answer. Copy/paste is not learning.

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

    Default

    Im glad he could help. Now I have learned some new code I would have never seen before!

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

    Default

    @Maximus
    Yes that was me on the CODE tags and I'm also on JavaGaming.org - Index ;)

  10. #50
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    Hey sunde887,
    If you look at the first page the guy wasn't able to understand where the import was missing when I first helped him fix the game. How will an absolute beginner who's trying to mod someones game going to understand how to do it without any examples? I don't believe I spoon fed him per say, but I should have included detailed comments outlining why my changes fixed it, as that definitely aids understanding more than just looking at it by yourself. I'm a believer in step-by-step learning :P. Is there a forum rule against guiding people by showing the fix, and explaining how to do it :/?

    Also on the first page, your suggestion to his questions was to format the code properly. This isn't the prettiest code I saw, but, did you even bother to go through it once? That's not the kind of suggestion I would say to my boss on the job man :D.

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

    Default

    I didn't read it entirely. I scanned it, I am not going to waste time going through that much unformatted code. My suggestion was meant to show him a way to receive more help. While I do not know whether it's a rule to not spoonfeed people, I personally(as well as many others, I'm sure) tend to favor helping them figure it out, rather then giving them the answer. Call me lazy, but lack of formatting tends to bother me, I will read through formatted code, but when I come across un formatted code on my phone my first comment tends to ask the user to properly format code.

    Of course he is going to say he understands what he was missing, maybe he does. The thing is, if you helped him come to this realization without simply highlighting and correcting the code perhaps he would also have picked up a deeper understanding of the fix. Spoonfeeding leads to people coming here with every little problem they have. Why figure it out yourself if someone will eventually give you the answer?

    Step by step learning is different then what you did. Perhaps I am nitpicking, and I do occasionally spoon feed a little, but in the long run, I believe it will hurt the user more than it will help.

  12. #52
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Step by step learning is different then what you did. Perhaps I am nitpicking, and I do occasionally spoon feed a little, but in the long run, I believe it will hurt the user more than it will help.
    I somewhat agree with you, but then, in how many steps could I have divided those 3 lines of fix to make him really understand what is happening? Real learning is always up to the individual himself, the onus is on Bundy to experiment with the game/fix and figure it out. The thing is, if he were to get no help at all, or purely hints for 3 pages of the thread, that's pretty disheartening to go through. As for crappily formatted code, man...Im used to it :( ;)

Page 3 of 3 FirstFirst 123

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 05:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  3. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 08:50 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08: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
  •