-
TicTacToe error
I have been in java in school for almost one semester (i have a great teacher but i'm sick right now and i need an answer ASAP) and i was working what i was doing wrong. My program gave me :eek:NE HUNDRED class, interface, or enum expected errors. It's a tictactoe program, just a simple exercise, and i got this huge list of errors. I looked it up and what i found said it had to do with a previous line, but i looked and experimented but could not find it. I googled java forums and found this site and posted. If i did this completely wrong, I'm sorry, i am new to java.
import java.applet.Applet;
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
public class TicTacToe extends Applet implements ActionListener
{
int i = 0;
int r = 0;
int g = 0;
int b = 0;
Random rand = new Random(150);
int counter = 0;
GridLayout playBoard = new GridLayout(4,3,5,5);
boolean playerX = true;
boolean xWinner = false;
boolean oWinner = false;
TextField turnField = new TextField(20);
TextField title = new TextField(20);
TextField winner = new TextField(20);
Button b1 = new Button ("1");
Button b2 = new Button ("2");
Button b3 = new Button ("3");
Button b4 = new Button ("4");
Button b5 = new Button ("5");
Button b6 = new Button ("6");
Button b7 = new Button ("7");
Button b8 = new Button ("8");
Button b9 = new Button ("9");
public void init()
{
turnField.setText("X's Turn");
turnField.setFont(new Font("TimesRoman", Font.PLAIN,36));
title.setFont(new Font("TimesRoman", Font.PLAIN,36));
winner.setFont(new Font("TimesRoman", Font.PLAIN,36));
b1.setFont(new Font("TimesRoman", Font.PLAIN,36));
b2.setFont(new Font("TimesRoman", Font.PLAIN,36));
b3.setFont(new Font("TimesRoman", Font.PLAIN,36));
b4.setFont(new Font("TimesRoman", Font.PLAIN,36));
b5.setFont(new Font("TimesRoman", Font.PLAIN,36));
b6.setFont(new Font("TimesRoman", Font.PLAIN,36));
b7.setFont(new Font("TimesRoman", Font.PLAIN,36));
b8.setFont(new Font("TimesRoman", Font.PLAIN,36));
b9.setFont(new Font("TimesRoman", Font.PLAIN,36));
title.setText("Tic-Tac-Toe");
turnField.setForeground(Color.blue);
setLayout(playBoard);
add (turnField);
add (title);
add (winner);
add (b1);
add (b2);
add (b3);
add (b4);
add (b5);
add (b6);
add (b7);
add (b8);
add (b9);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
}
}
public void actionPerformed(ActionEvent e)
{
//--------------------------------Turn-----------------------------------------------
if(counter % 2 == 0)
{
playerX = true;
}
else
{
playerX = false;
}
if(!playerX)
{
turnField.setForeground(Color.blue);
turnField.setText("X's Turn");
}
if(playerX)
{
turnField.setForeground(Color.red);
turnField.setText("O's Turn");
}
//------------------------------Pushing Buttons-------------------------------------
if (e.getSource() == b1 && b1.getLabel().equals ("1"))
{
if(playerX == true)
{
b1.setForeground(Color.blue);
b1.setLabel("X");
counter++;
}
if(playerX == false)
{
b1.setForeground(Color.red);
b1.setLabel("O");
counter++;
}
}
if (e.getSource() == b2 && b2.getLabel().equals ("2"))
{
if(playerX == true)
{
b2.setForeground(Color.blue);
b2.setLabel("X");
counter++;
}
if(playerX == false)
{
b2.setForeground(Color.red);
b2.setLabel("O");
counter++;
}
}
if (e.getSource() == b3 && b3.getLabel().equals ("3"))
{
if(playerX == true)
{
b3.setForeground(Color.blue);
b3.setLabel("X");
counter++;
}
if(playerX == false)
{
b3.setForeground(Color.red);
b3.setLabel("O");
counter++;
}
}
if (e.getSource() == b4 && b4.getLabel().equals ("4"))
{
if(playerX == true)
{
b4.setForeground(Color.blue);
b4.setLabel("X");
counter++;
}
if(playerX == false)
{
b4.setForeground(Color.red);
b4.setLabel("O");
counter++;
}
}
if (e.getSource() == b5 && b5.getLabel().equals ("5"))
{
if(playerX == true)
{
b5.setForeground(Color.blue);
b5.setLabel("X");
counter++;
}
if(playerX == false)
{
b5.setForeground(Color.red);
b5.setLabel("O");
counter++;
}
}
if (e.getSource() == b6 && b6.getLabel().equals ("6"))
{
if(playerX == true)
{
b6.setForeground(Color.blue);
b6.setLabel("X");
counter++;
}
if(playerX == false)
{
b6.setForeground(Color.red);
b6.setLabel("O");
counter++;
}
}
if (e.getSource() == b7 && b7.getLabel().equals ("7"))
{
if(playerX == true)
{
b7.setForeground(Color.blue);
b7.setLabel("X");
counter++;
}
if(playerX == false)
{
b7.setForeground(Color.red);
b7.setLabel("O");
counter++;
}
}
if (e.getSource() == b8 && b8.getLabel().equals ("8"))
{
if(playerX == true)
{
b8.setForeground(Color.blue);
b8.setLabel("X");
counter++;
}
if(playerX == false)
{
b8.setForeground(Color.red);
b8.setLabel("O");
counter++;
}
}
if (e.getSource() == b9 && b9.getLabel().equals ("9"))
{
if(playerX == true)
{
b9.setForeground(Color.blue);
b9.setLabel("X");
counter++;
}
if(playerX == false)
{
b9.setForeground(Color.red);
b9.setLabel("O");
counter++;
}
}
//------------------------------X WINS--------------------------------------------
if(b1.getLabel().equals("X") && b2.getLabel().equals("X") && b3.getLabel().equals("X"))
{
winner.setForeground(Color.blue);
winner.setText("X WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
if(b4.getLabel().equals("X") && b5.getLabel().equals("X") && b6.getLabel().equals("X"))
{
winner.setForeground(Color.blue);
winner.setText("X WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
if(b7.getLabel().equals("X") && b8.getLabel().equals("X") && b9.getLabel().equals("X"))
{
winner.setForeground(Color.blue);
winner.setText("X WINS!");
turnField.setText(" ");
}
if(b1.getLabel().equals("X") && b5.getLabel().equals("X") && b9.getLabel().equals("X"))
{
winner.setForeground(Color.blue);
winner.setText("X WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
if(b7.getLabel().equals("X") && b5.getLabel().equals("X") && b3.getLabel().equals("X"))
{
winner.setForeground(Color.blue);
winner.setText("X WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
if(b1.getLabel().equals("X") && b4.getLabel().equals("X") && b7.getLabel().equals("X"))
{
winner.setForeground(Color.blue);
winner.setText("X WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
if(b2.getLabel().equals("X") && b5.getLabel().equals("X") && b8.getLabel().equals("X"))
{
winner.setForeground(Color.blue);
winner.setText("X WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
if(b3.getLabel().equals("X") && b6.getLabel().equals("X") && b9.getLabel().equals("X"))
{
winner.setForeground(Color.blue);
winner.setText("X WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
//---------------------------O WINS---------------------------------------
if(b1.getLabel().equals("O") && b2.getLabel().equals("O") && b3.getLabel().equals("O"))
{
winner.setForeground(Color.red);
winner.setText("O WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
if(b4.getLabel().equals("O") && b5.getLabel().equals("O") && b6.getLabel().equals("O"))
{
winner.setForeground(Color.red);
winner.setText("O WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
if(b7.getLabel().equals("O") && b8.getLabel().equals("O") && b9.getLabel().equals("O"))
{
winner.setForeground(Color.red);
winner.setText("O WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
if(b1.getLabel().equals("O") && b5.getLabel().equals("O") && b9.getLabel().equals("O"))
{
winner.setForeground(Color.red);
winner.setText("O WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
if(b7.getLabel().equals("O") && b5.getLabel().equals("O") && b3.getLabel().equals("O"))
{
winner.setForeground(Color.red);
winner.setText("O WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
if(b1.getLabel().equals("O") && b4.getLabel().equals("O") && b7.getLabel().equals("O"))
{
winner.setForeground(Color.red);
winner.setText("O WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
if(b2.getLabel().equals("O") && b5.getLabel().equals("O") && b8.getLabel().equals("O"))
{
winner.setForeground(Color.red);
winner.setText("O WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
if(b3.getLabel().equals("O") && b6.getLabel().equals("O") && b9.getLabel().equals("O"))
{
winner.setForeground(Color.red);
winner.setText("O WINS!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
if(counter == 9 && !xWinner && !oWinner)
{
winner.setForeground(Color.red);
winner.setText("CAT GAME!");
turnField.setText(" ");
for(i = 0; i < 1; i--)
{
r = rand.nextInt(150) + 100;
g = rand.nextInt(150) + 100;
b = rand.nextInt(150) + 100;
Color myColor = new Color(r,g,b);
setBackground(myColor);
}
}
}
}
-
Hint- You have a closing bracket on line 70 (or thereabouts) that closes the class. You do not close your class at the end.
-
Forehead Slap
Thanks for responding so quickly, i deleted the bracket and it worked. Sorry about not putting the line numbers in the post.
-
Line numbers aren't a big deal. You probably should have used the code tags though. Anyway, glad you got it working.