I have to write a program that diaplys a tic tac toe board with random x's and o's. I have having severe issues with the random part. I also dont know what to put for arguments in the JButton methods, but I think my issues are realted. Any and all help would be appreciated.

Here is what I have so far:

import java.awt.*;
import javax.swing.*;
import java.util.Random;

public class TicTacToe extends JFrame {

public TicTacToe() {
super ("Tic Tac Toe");
LayoutManager lm = new GridLayout (3,3);
setLayout(lm);
setSize(400,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

ImageIcon x = new ImageIcon("jon.jpg");
JLabel label = new JLabel("jon.jpg"); 
ImageIcon o = new ImageIcon("kate.jpg");
JLabel label2 = new JLabel("kate.jpg");


JButton box1 = new JButton();
add(box1);
JButton box2 = new JButton();
add(box2); 
JButton box3 = new JButton();
add(box3); 
JButton box4 = new JButton();
add(box4); 
JButton box5 = new JButton();
add(box5); 
JButton box6 = new JButton();
add(box6); 
JButton box7 = new JButton();
add(box7); 
JButton box8 = new JButton();
add(box8); 
JButton box9 = new JButton();
add(box9);

Random r = new Random();

int i = r.nextInt(3);
int count = 0; 
if (count <= i) { 
add(box1);
} 
if (count <= i) { 
add(box2);
}
if (count <= i) { 
add(box3);
}
if (count <= i) { 
add(box4);
}
if (count <= i) { 
add(box5);
}
if (count <= i) { 
add(box6);
}
if (count <= i) { 
add(box7);
}
if (count <= i) { 
add(box8);
}
if (count <= i) { 
add(box9);
}







setVisible(true);




}

public static void main(String[] args){
TicTacToe frame = new TicTacToe();

}
}

Recommended Answers

All 5 Replies

go and check deitel and deitel book on java

commented: useless -1

bump

Here is the newest code. Just changes the random part. Now I am getting an incompatible data type error.

import java.awt.*;
import javax.swing.*;
import java.util.Random;

public class TicTacToe extends JFrame {

public TicTacToe() {
super ("Tic Tac Toe");
LayoutManager lm = new GridLayout (3,3);
setLayout(lm);
setSize(400,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

ImageIcon x = new ImageIcon("jon.jpg");
JLabel label = new JLabel("jon.jpg"); 
ImageIcon o = new ImageIcon("kate.jpg");
JLabel label2 = new JLabel("kate.jpg");


JButton box1 = new JButton();
add(box1);
JButton box2 = new JButton();
add(box2); 
JButton box3 = new JButton();
add(box3); 
JButton box4 = new JButton();
add(box4); 
JButton box5 = new JButton();
add(box5); 
JButton box6 = new JButton();
add(box6); 
JButton box7 = new JButton();
add(box7); 
JButton box8 = new JButton();
add(box8); 
JButton box9 = new JButton();
add(box9);

Random r = new Random();

int i = r.nextInt(3);
if (i == 1)
  r = x;

bump

Paste us the error with the line number it's occurring on. Once you do so it'll make it easier for us to help you. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.