-
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.
-
have you ended up completing the program?
I am working on the same program for school and I need help.
-
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?
-
I'm a 9th grader from Churchill High School in Potomac, Maryland.
-
thats funny, im a 10th grader from churchill high school in potomac, maryland...do u have steelman or monshi?
-
-
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
-
I sit next to josh, lawrence, and brian. Are you next to lawrence? I think I copied most of your program today.
-
hahaha yeah that was you, yea i sit next to lawrence lol
-
alright well I need help tomorrow
-
i hope you took notes cuz steelman's being really annoying
Similar Threads
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By Gary Nelson in forum .NET
Replies: 277
Last Post: 10-01-2003, 12:00 AM
-
By Robert G in forum .NET
Replies: 84
Last Post: 02-08-2001, 02:38 PM
-
By DC Jobs in forum Careers
Replies: 1
Last Post: 08-24-2000, 09:38 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
Forum Rules
|
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
|
Bookmarks