Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: TicTacToe reset problems

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default TicTacToe reset problems

    hello I'm having a problem with this tic tac toe game that i am making. what is happening is that the game is suppose to reset when ether a person wins or there is a tie. so far it only exits and dose not reset the game. any suggestions on how to fix this problem?
    import java.awt.*;
    import java.awt.event.*;
     
    import javax.swing.*;
     
     
     
     
    public class TicTakToe  extends JFrame
    {
    	private JMenuBar bar = new JMenuBar();
    	private JMenu file = new JMenu("File");
    	private JMenuItem exit = new JMenuItem("exit");
     
    	private static boolean win = false;
    	private String letter = "";
    	private int count = 0;
     
     
    	private JButton a1 = new JButton("");
    	private JButton a2 = new JButton("");
    	private JButton a3 = new JButton("");
    	private JButton b1 = new JButton("");
    	private JButton b2 = new JButton("");
    	private JButton b3 = new JButton("");
    	private JButton c1 = new JButton("");
    	private JButton c2 = new JButton("");
    	private JButton c3 = new JButton("");
     
     
     
     
     
    	public class exit implements ActionListener
    	{
     
     
    		public void actionPerformed(ActionEvent e) 
    		{
    			System.exit(0);
     
    		}
     
    	}
     
     
     
    	public class test implements ActionListener
    	{
     
     
    		public void actionPerformed(ActionEvent e2) 
    		{
     
    			count++;
     
    			if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9)
    			{
    			letter = "X";
    			} 
    			else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10)
    			{
    				letter = "O";
    			}
     
     
    			if(e2.getSource() == a1)
    			{
    				a1.setText(letter);
    				a1.setEnabled(false);
    			} 
    			else if(e2.getSource() == a2)
    			{
    				a2.setText(letter);
    				a2.setEnabled(false);
    			} 
    			else if(e2.getSource() == a3)
    			{
    				a3.setText(letter);
    				a3.setEnabled(false);
    			} 
    			else if(e2.getSource() == b1)
    			{
    				b1.setText(letter);
    				b1.setEnabled(false);
    			}
    			else if(e2.getSource() == b2)
    			{
    				b2.setText(letter);
    				b2.setEnabled(false);
    			} 
    			else if(e2.getSource() ==b3)
    			{
    				b3.setText(letter);
    				b3.setEnabled(false);
    			}
    			else if(e2.getSource() == c1)
    			{
    				c1.setText(letter);
    				c1.setEnabled(false);
    			} 
    			else if(e2.getSource() == c2)
    			{
    				c2.setText(letter);
    				c2.setEnabled(false);
    			} 
    			else if(e2.getSource() == c3)
    			{
    				c3.setText(letter);
    				c3.setEnabled(false);
    			}
    			if( a1.getText() == a2.getText() && a2.getText() == a3.getText() && a1.getText() != "" || 
    				b1.getText() == b2.getText() && b2.getText() == b3.getText() && b1.getText() != ""||
    				c1.getText() == c2.getText() && c2.getText() == c3.getText() && c1.getText() != "")
    			{
    				win = true;
    			}
    			else if(a1.getText() == b1.getText() && b1.getText() == c1.getText() && a1.getText() != "" || 
    					a2.getText() == b2.getText() && b2.getText() == c2.getText() && a2.getText() != ""||
    					a3.getText() == b3.getText() && b3.getText() == c3.getText() && a3.getText() != "")
    			{
    				win = true;
    			}
    			else if(a1.getText() == b2.getText() && b2.getText() == c3.getText() && a1.getText() != "" || 
    					a3.getText() == b2.getText() && b2.getText() == c1.getText() && a3.getText() != "")
    			{
    				win = true;
    			}
    			else
    			{
    				win = false;
    			}
     
    			// these are the lines that should reset the game.
    			 if(win == true)
    		      {
    		          JOptionPane.showMessageDialog(null, letter + " wins the game!");
    		          System.exit(0);
    		     } 
    			 else if(count == 9 && win == false)
    		     {
    			 System.exit(0);
     
    		     }        
     
    			//========================
     
    		}
     
    	}
     
     
     
     
     
    	public TicTakToe()
    	{
    		LayoutManager some_layout = new BorderLayout();
    		setLayout(some_layout);
     
     
    		add(bar, BorderLayout.NORTH);
    		bar.add(file);
    		file.add(exit);
    		exit close = new exit();
    		test c = new test();
     
     
    		exit.addActionListener(close);
     
    		a1.addActionListener(c);
    		a2.addActionListener(c);
    		a3.addActionListener(c);
    		b1.addActionListener(c);
    		b2.addActionListener(c);
    		b3.addActionListener(c);
    		c1.addActionListener(c);
    		c2.addActionListener(c);
    		c3.addActionListener(c);
     
    		JPanel cen_panel = new JPanel();
    		LayoutManager some_layout2 = new GridLayout(3,3);
    		cen_panel.setLayout(some_layout2);
     
    		cen_panel.add(a1);
    		cen_panel.add(a2);
    		cen_panel.add(a3);
    		cen_panel.add(b1);
    		cen_panel.add(b2);
    		cen_panel.add(b3);
    		cen_panel.add(c1);
    		cen_panel.add(c2);
    		cen_panel.add(c3);
     
     
     
     
     
    		add(cen_panel,BorderLayout.CENTER);
     
     
     
     
     
     
    		setTitle("test");
    		setSize(400,400);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
     
    	}
     
    	public static void main(String[] args)
    	{
    		TicTakToe test = new TicTakToe();
    	}
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: TicTacToe reset problems

    any suggestions on how to fix this problem?
    First step would be to remove: System.exit(0); so that the program doesn't exit.
    Next decide what needs to be done to reset the game. What variables need to have their values changed?


    BTW How much of this code did you write yourself?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Tictactoe with out GUI
    By Andile in forum Java Theory & Questions
    Replies: 2
    Last Post: September 15th, 2012, 03:49 PM
  2. TicTacToe problem
    By Kratos in forum Java Theory & Questions
    Replies: 3
    Last Post: March 15th, 2012, 12:13 PM
  3. TicTacToe 2D Array help
    By steel55677 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 9th, 2011, 05:01 PM
  4. TicTacToe
    By Zerro900 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 27th, 2011, 08:29 AM
  5. Tictactoe problem
    By Nostromo in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2011, 03:38 PM