Results 1 to 10 of 10

Thread: TicTacToe game

  1. #1
    pinotje's Avatar
    pinotje is offline Member
    Join Date
    Dec 2010
    Location
    Belgium
    Posts
    14
    Rep Power
    0

    Default TicTacToe game

    Hi all,

    I'm new to Java!
    At the moment I'm trying to make a Dutch game called "Boter, Kaas, Eieren" (English: Tic Tac Toe) game in Java....

    Java Code:
    public class Main {
      public static void main(String[] arg) {
        MyFrame mf = new MyFrame();
      }
    }
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MyFrame extends JFrame implements ActionListener {
    	private JButton b1,b2,b3;
    	private JButton b4, b5,b6;
    	private JButton b7,b8,b9;
    	private int aantal;
    	private String letter;
    	private boolean winnen = false;
    	private boolean setter = false;
    
    	public MyFrame() {
    		JPanel spel = new JPanel();
    		spel.setLayout(new GridLayout(3,3));
    		spel.setPreferredSize(new Dimension(300, 300));
    		add(spel);
    		b1 = new JButton(""); spel.add(b1);
    		b1.addActionListener(this);
    		b2 = new JButton(""); spel.add(b2);
    		b2.addActionListener(this);
    		b3 = new JButton(""); spel.add(b3);
    		b3.addActionListener(this);
    		b4 = new JButton(""); spel.add(b4);
    		b4.addActionListener(this);
    		b5 = new JButton(""); spel.add(b5);
    		b5.addActionListener(this);
    		b6 = new JButton(""); spel.add(b6);
    		b6.addActionListener(this);
    		b7 = new JButton(""); spel.add(b7);
    		b7.addActionListener(this);
    		b8 = new JButton(""); spel.add(b8);
    		b8.addActionListener(this);
    		b9 = new JButton(""); spel.add(b9);
    		b9.addActionListener(this);
    		pack();
    		setTitle("Boter Kaas Eiren");
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		}
    
    	public void actionPerformed(ActionEvent a) {
    		aantal++;
    		if(aantal%2==0) { letter="O"; }
    		else { letter="X"; }
    		JButton ingedrukteKnop = (JButton)a.getSource();
    		ingedrukteKnop.setText(letter);
    		ingedrukteKnop.setEnabled(false);
    
    		// Horizontaal
    		if(b1.getText().equals(b2.getText()) && b2.getText().equals(b3.getText()) && !b1.getText().equals("")) { winnen = true; }
    		else if(b4.getText().equals(b5.getText()) && b5.getText().equals(b6.getText()) && !b4.getText().equals("")) { winnen = true; }
    		else if(b7.getText().equals(b8.getText())&& b8.getText().equals(b9.getText()) && !b7.getText().equals("")) { winnen = true; }
    
    		// Verticaal
    		if(b1.getText().equals(b4.getText()) && b4.getText().equals (b7.getText()) && !b1.getText().equals("")) { winnen = true; }
    		else if(b2.getText().equals(b5.getText()) && b5.getText().equals( b8.getText()) && !b2.getText().equals( "")) { winnen = true; }
    		else if(b3.getText().equals(b6.getText())&& b6.getText().equals( b9.getText()) && !b3.getText().equals("")) { winnen = true; }
    
    		// Diagonaal
    		if(b1.getText().equals(b5.getText()) && b5.getText().equals (b9.getText()) && !b1.getText().equals("")) { winnen = true; }
    		else if(b3.getText().equals(b5.getText()) && b5.getText().equals( b7.getText()) && !b3.getText().equals("")) { winnen = true; }
    		else { winnen = false; }
    
    		// Result
    		if(aantal==9) {
    			if(!winnen) {
    				JOptionPane.showMessageDialog(null, "Draw!");
    				setter = true;
    				aantal = 0;
    			}
    			else {
    				JOptionPane.showMessageDialog(null, letter + " Wins!");
    				setter = true;
    				aantal = 0;
    			}
    		}
    
    		// Setter
    		if(setter) {
    			setter = false;
    			b1.setText("");
    			b1.setEnabled(true);
    			b2.setText("");
    			b2.setEnabled(true);
    			b3.setText("");
    			b3.setEnabled(true);
    			b4.setText("");
    			b4.setEnabled(true);
    			b5.setText("");
    			b5.setEnabled(true);
    			b6.setText("");
    			b6.setEnabled(true);
    			b7.setText("");
    			b7.setEnabled(true);
    			b8.setText("");
    			b8.setEnabled(true);
    			b9.setText("");
    			b9.setEnabled(true);
    			}
    	}
    }
    So far, it pretty works...
    But the problem is, the Result (show draw or win, and then clear all the buttons) will only come after 9 attempts.

    How can I make the Result shown directly after the person has 3 same "X" or "O" in a row? And btw is this game scripted as it should?

    Any help is welcome!
    Thx!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    Step through your program with a debugger, or add some print statements to figure out what's going on.

    Hint- What is the value of your winnen variable each time? Why? When is it set to that value?

  3. #3
    pinotje's Avatar
    pinotje is offline Member
    Join Date
    Dec 2010
    Location
    Belgium
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    Step through your program with a debugger, or add some print statements to figure out what's going on.

    Hint- What is the value of your winnen variable each time? Why? When is it set to that value?

    I'm using TextPad to edit my Java sources.
    And there is no error while compiling this java game.

    The value of winnen variable will be set true of the person has 3 on a row, else the winnen variable will be false....

    Is this what you mean?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    Quote Originally Posted by pinotje View Post
    I'm using TextPad to edit my Java sources.
    And there is no error while compiling this java game.
    I didn't say there was. And I'm not sure what relevancy your editor has to your problem.

    Quote Originally Posted by pinotje View Post
    The value of winnen variable will be set true of the person has 3 on a row, else the winnen variable will be false....
    That's what you think is happening. Use a debugger or print statements to figure out why that's not what's actually happening.[/QUOTE]

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pinotje View Post
    Java Code:
    		// Result
    		if(aantal==9) {
    			if(!winnen) {
    				JOptionPane.showMessageDialog(null, "Draw!");
    				setter = true;
    				aantal = 0;
    			}
    			else {
    				JOptionPane.showMessageDialog(null, letter + " Wins!");
    				setter = true;
    				aantal = 0;
    			}
    		}
    So far, it pretty works...
    But the problem is, the Result (show draw or win, and then clear all the buttons) will only come after 9 attempts.
    Your program logic is wrong here; it should be something like this:

    Java Code:
    if (winnen) {
       JOptionPane.showMessageDialog(null, letter + " Wins!");
       ...
    }
    else if (aantal == 9) {
       JOptionPane.showMessageDialog(null, "Draw!");
       ...
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    pinotje's Avatar
    pinotje is offline Member
    Join Date
    Dec 2010
    Location
    Belgium
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by KevinWorkman View Post
    I didn't say there was. And I'm not sure what relevancy your editor has to your problem.


    That's what you think is happening. Use a debugger or print statements to figure out why that's not what's actually happening.
    [/QUOTE]

    I'm new to Java...

    Could you advice me a Java debugger software?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    I'm new to Java...

    Could you advice me a Java debugger software?[/QUOTE]

    System.out.println( ... ) is the perfect poor man's debugger. Sprinkle those print statement all over your code and print out every value that you suspect to be wrong or interesting or whatever. It really helps, doesn't cost anything and it's easy to use.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    pinotje's Avatar
    pinotje is offline Member
    Join Date
    Dec 2010
    Location
    Belgium
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Your program logic is wrong here; it should be something like this:

    Java Code:
    if (winnen) {
       JOptionPane.showMessageDialog(null, letter + " Wins!");
       ...
    }
    else if (aantal == 9) {
       JOptionPane.showMessageDialog(null, "Draw!");
       ...
    }
    kind regards,

    Jos

    Hoi Jos, (are u Dutch? =P)

    Thanks! Your code is more logic.

    Also, I have added:
    Java Code:
    winnen = false;
    to:
    Java Code:
    if(setter) {
    ......
    }

    Now it works perfectly!

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pinotje View Post
    Hoi Jos, (are u Dutch? =P)

    Thanks! Your code is more logic.

    Also, I have added:
    Java Code:
    winnen = false;
    to:
    Java Code:
    if(setter) {
    ......
    }

    Now it works perfectly!
    Good; and yep I'm Dutch but don't tell anyone I told you ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    pinotje's Avatar
    pinotje is offline Member
    Join Date
    Dec 2010
    Location
    Belgium
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Good; and yep I'm Dutch but don't tell anyone I told you ;-)

    kind regards,

    Jos
    Haha, I won't =P

Similar Threads

  1. Tictactoe game
    By Fowler in forum New To Java
    Replies: 11
    Last Post: 10-31-2010, 09:55 PM
  2. I need feedback on my TicTacToe game
    By kiregad in forum New To Java
    Replies: 4
    Last Post: 03-21-2010, 10:09 PM
  3. Help with tictactoe
    By ximenaacucr in forum New To Java
    Replies: 3
    Last Post: 09-16-2009, 08:30 PM
  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
  •