DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2007
    Posts
    6

    Java Tic Tac Toe Project Help Needed

    Hey, im making tic tac toe as my final project, and im gonna need some help. this is what i have so far:

    Code:
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    
    public class TicTacToe extends JFrame implements ActionListener
    {
    	private JButton[][]buttons;
    	public JButton[][] fillArray()
    	{
    		String names="012345678";
    		JButton [][]buttonArray=new JButton[3][3];
    		int x=0;
    		for(int r=0;r<3;r++)
    		{
    			for(int c=0;c<3;c++)
    			{
    				buttonArray[r][c]=new JButton();
    				buttonArray[r][c].addActionListener(this);
    				buttonArray[r][c].setActionCommand(names.substring(x,x+1));     
    				buttonArray[r][c].setBackground(Color.black);
    				buttonArray[r][c].setForeground(Color.white);
    				Font fontType = new Font("Serif", Font.BOLD, 100);
    				buttonArray[r][c].setFont(fontType);
    				contentPane.add(buttonArray[r][c]);
    				x++;
    			}
    		}
    		return buttonArray;
    	}
    
    	private static Container contentPane;
    	public TicTacToe()
    	{
    		setSize(800, 600);
    		setTitle("Tic Tac Toe");
    		contentPane = getContentPane(); 
    		contentPane.setLayout(new GridLayout(3,3));
    		buttons=fillArray();			
    	}
    
    	public void actionPerformed(ActionEvent event)
    	{
    		Container contentPane = getContentPane();
    
    		int i = 1;
    		if(event.getActionCommand().equals("0"))
    		{
    			if(i%2==0)
    			{
    				buttons[0][0].setText("X");
    			}
    			else
    			{
    				buttons[0][0].setText("O");
    			}
    		}
    		else if(event.getActionCommand().equals("1"))
    		{
    			if(i%2==0)
    			{
    				buttons[0][1].setText("X");
    			}
    			else
    			{
    				buttons[0][1].setText("O");
    			}
    		}
    
    		else if(event.getActionCommand().equals("2"))
    		{
    			if(i%2==0)
    			{
    				buttons[0][2].setText("X");
    
    			}
    			else
    			{
    				buttons[0][2].setText("O");
    
    			}
    		}
    
    
    		else if(event.getActionCommand().equals("3"))
    		{
    			if(i%2==0)
    			{
    				buttons[1][0].setText("X");
    			}
    			else
    			{
    				buttons[1][0].setText("O");
    			}
    		}
    
    		else if( event.getActionCommand().equals("4"))
    		{
    			if(i%2==0)
    			{
    				buttons[1][1].setText("X");
    			}
    			else
    			{
    				buttons[1][1].setText("O");
    			}
    		}
    
    		else if(event.getActionCommand().equals("5"))
    		{
    			if(i%2==0)
    			{
    				buttons[1][2].setText("X");
    			}
    			else
    			{
    				buttons[1][2].setText("O");
    			}
    		}
    
    
    		else if(event.getActionCommand ().equals("6"))
    		{
    			if(i%2==0)
    			{
    				buttons[2][0].setText("X");
    			}
    			else
    			{
    				buttons[2][0].setText("O");
    			}
    		}
    
    
    		else if(event.getActionCommand().equals("7"))
    		{
    			if(i%2==0)
    			{
    				buttons[2][1].setText("X");
    			}
    			else
    			{
    				buttons[2][1].setText("O");
    			}
    		}
    		else if(event.getActionCommand().equals("8"))
    		{
    			if(i%2==0)
    			{
    				buttons[2][2].setText("X");
    			}
    			else
    			{
    				buttons[2][2].setText("O");
    			}
    		}
    
    		else if(event.getActionCommand().equals("Exit"))
    			System.exit(0);
    
    		else System.out.println("Error in button interface.");
    	}
    
    	public static void main(String[] args)
    	{
    		TicTacToe buttonGui= new TicTacToe();
    		buttonGui.setVisible(true);
    	}
    
    }
    ok new problem. i have no clue where the i++ is supposed to go. where is it supposed to go?!?! ive been trying a bunch of places but none have worked. help?
    Last edited by SuperApple; 05-08-2007 at 10:19 PM.

  2. #2
    Join Date
    May 2007
    Posts
    5
    have you ended up completing the program?

    I am working on the same program for school and I need help.

  3. #3
    Join Date
    May 2007
    Posts
    6
    yea its done but im really pissed off at my teacher cuz she doesnt like the way i used my for loop and if statements in the checker method. ill post the code tomorrow so somebody could see if there's a better way to use it. what school to u go to, michael? what grade?

  4. #4
    Join Date
    May 2007
    Posts
    5
    I'm a 9th grader from Churchill High School in Potomac, Maryland.

  5. #5
    Join Date
    May 2007
    Posts
    6
    thats funny, im a 10th grader from churchill high school in potomac, maryland...do u have steelman or monshi?

  6. #6
    Join Date
    May 2007
    Posts
    5
    steelman period 1

  7. #7
    Join Date
    May 2007
    Posts
    6
    um you're in my class...thats really funny. my real name is eddy, do you recognize that name? cuz i dont recognize yours lol

  8. #8
    Join Date
    May 2007
    Posts
    5
    I sit next to josh, lawrence, and brian. Are you next to lawrence? I think I copied most of your program today.

  9. #9
    Join Date
    May 2007
    Posts
    6
    hahaha yeah that was you, yea i sit next to lawrence lol

  10. #10
    Join Date
    May 2007
    Posts
    5
    alright well I need help tomorrow

  11. #11
    Join Date
    May 2007
    Posts
    6
    i hope you took notes cuz steelman's being really annoying

Similar Threads

  1. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  2. Control Arrays in VB.NET
    By Gary Nelson in forum .NET
    Replies: 277
    Last Post: 10-01-2003, 12:00 AM
  3. Good Editorial by Russell Jones
    By Robert G in forum .NET
    Replies: 84
    Last Post: 02-08-2001, 02:38 PM
  4. Replies: 1
    Last Post: 08-24-2000, 09:38 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links