1
\$\begingroup\$

This is the problem:

(Game: display a tic-tac-toe board) Display a frame that contains nine labels. A label may display an image icon for X or and image icon for O. What to display is randomly decided. Use the Math.random() method to generate an integer 0 or 1, which corresponds to displaying an X or O image icon. These images are in the files x.gif and o.gif)

My code seems to do what the problem asks, and the output is practically the same as the example (minus the borders). However, without the solutions for odd-numbered exercises, I can't imagine what an "ideal" implementation of this problem would look like. For example, is it good practice to use arrays in the way that I did?

Here is my solution:

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

public class DisplayTicTacToe extends JFrame {
public DisplayTicTacToe() {
    setLayout(new GridLayout(0, 3));

    ImageIcon[] display = new ImageIcon[3];
    display[0] = new ImageIcon("image/o.gif");
    display[1] = new ImageIcon("image/x.gif");
    display[2] = new ImageIcon("Empty Icon");
    for (int i = 0; i < 9; i++) {
        JLabel jlb = new JLabel(display[(int)(Math.random() * 3)]);
        jlb.setBorder(new LineBorder(Color.BLACK, 1));
        add(jlb);
    }
}

public static void main(String[] args) {
    DisplayTicTacToe frame = new DisplayTicTacToe();
    frame.pack();
    frame.setTitle("Display TicTacToe");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}
\$\endgroup\$
1
\$\begingroup\$

By my personal interpretation of the challenge, I wouldn't accept this solution, because it can generate illegal board configurations.

  • Not respecting turns: $$\begin{array}{|c|c|c|} \hline \textrm{X} & \ & \ \\ \hline \textrm{X} & \ & \textrm{X} \\ \hline \ & \ & \ \\ \hline \end{array}$$
  • Allowing the game to continue after a win: $$\begin{array}{|c|c|c|} \hline \textrm{X} & \ & \textrm{O} \\ \hline \textrm{X} & \ & \textrm{O} \\ \hline \textrm{X} & \ & \textrm{O} \\ \hline \end{array}$$

If you want to use an array, initialize it like this:

ImageIcon[] icons = new ImageIcon[] {
    new ImageIcon("images/x.gif"),
    new ImageIcon("images/o.gif"),
};
\$\endgroup\$
  • \$\begingroup\$ Ah I see. Did not think of initializing it like that. And I will try to revise it to handle illegal turns and wins. I think that might've been the point of this exercise... \$\endgroup\$ – FabZeros Jun 2 '15 at 14:24
1
\$\begingroup\$

Math.random is not very intuitive to use. Random makes it much easier. Create an instance of Random somewhere before the main loop, and generate the random indexes with random.nextInt(3) for values 0 1 2.

It's fine to use an array like you did to pick from the values randomly. When there are only few choices, like in this example, another option is to use a switch.

"display" is not a great name for an array of icons. icons would seem more natural.

A simple way to initialize an array:

ImageIcon[] icons = {
    new ImageIcon("images/x.gif"),
    new ImageIcon("images/o.gif"),
};
\$\endgroup\$
  • \$\begingroup\$ Thanks for reminding me about the Random class. I had forgotten about it due to the book's ubiquitous use of Math.random(). I will begin to learn and to use Random instances. Thanks also for the tip on variable-naming. It is much more intuitive now. \$\endgroup\$ – FabZeros Jun 2 '15 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.