Java beginner- Noughts and Crosses help
Hi, I'm quite new to Java but I'm trying to program a game of noughts and crosses for a test. I've included my code below, and if you copy/paste and compile it you can see what my problem is. My noughts and crosses board is built from an array of JButtons, each of which has an integer tag (BoxTag[]) connected with it. BoxTag[] is initialised as 0 which indicates the button has not been pressed. If the player presses the button BoxTag[] takes the value 1 and if the computer moves there it takes the value 2. My code then uses the value of BoxTag[] to replace the old JButton with a new one with an "X" or "O" in.
If you run the code you can see what the problem is. The buttons pressed by the player don't change straight away but instead change after the next button is pressed. Also if the mouse is moved over a previously pressed button the text in it vanishes. I can't see any reason for this happening.
Please bear in mind that my program is nowhere near complete so the game doesn't yet work perfectly. Thanks
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("Tic Tac Toe");
frame.add(new TestFrame("Tic Tac Toe"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(375,500);
frame.setVisible(true);
}
}
class TestFrame extends JComponent implements ActionListener {
String theMessage;
int messageX=125, messageY=95; // Coordinates of the message
JButton[] Box = new JButton[9];
int[] xpos = new int[9];
int[] ypos = new int[9];
int[] BoxTag = new int[9];
public TestFrame(String message) {
theMessage = message;
ypos[0] = ypos[1] = ypos[2] = xpos[0] = xpos[3] = xpos[6] = 75;
ypos[3] = ypos[4] = ypos[5] = xpos[1] = xpos[4] = xpos[7] = 150;
ypos[6] = ypos[7] = ypos[8] = xpos[2] = xpos[5] = xpos[8] = 225;
for (int i=0; i<9; i++) {
Box[i] = new JButton();
add(Box[i]);
setLayout(null);
Box[i].setBounds(xpos[i], ypos[i], 75, 75);
Box[i].addActionListener(this);
repaint();
}
Start();
}
public void paintComponent(Graphics g) {
g.drawString(theMessage, messageX, messageY);
}
public void Start() {
for (int i=0; i<9; i++) {
BoxTag[i] = 0;
}
Random generator = new Random(); //Coin toss to determine who starts
int Coin = generator.nextInt(2);
if (Coin==0) {
System.out.println("Computer starts!");
JTextArea text = new JTextArea("Computer starts!");
text.setBounds(20,20,200,50);
add(text);
text.setText(null);
Computer();
}
else if (Coin==1) {
System.out.println("Player starts!");
JTextArea text = new JTextArea("Player starts!");
text.setBounds(20,20,200,50);
add(text);
//actionPerformed();
}
}
public void actionPerformed(ActionEvent e) {
for (int i=0; i<9; i++) {
if (e.getSource() == Box[i]) {
if (BoxTag[i] == 0) {
BoxTag[i] = 1;
Computer();
}
else {}
}
}
}
public void Computer() {
Random CompGenerator = new Random();
int CompRand = CompGenerator.nextInt(9);
if (BoxTag[CompRand] == 0) {
BoxTag[CompRand] = 2;
JTextArea text = new JTextArea("Your go!");
text.setBounds(20,20,200,50);
add(text);
Continue();
}
else {
Computer();
}
}
public void Continue() {
for (int i=0; i<9; i++) {
if (BoxTag[i] == 1) {
Box[i] = new JButton("X");
add(Box[i]);
Box[i].setBounds(xpos[i], ypos[i], 75, 75);
}
else if (BoxTag[i] == 2) {
Box[i] = new JButton("O");
add(Box[i]);
Box[i].setBounds(xpos[i], ypos[i], 75, 75);
}
}
}
}