11 Replies - 1132 Views - Last Post: 02 October 2012 - 02:35 PM Rate Topic: -----

#1 MitulP91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 18-July 12

Fully Automated Tic Tac Toe

Posted 02 October 2012 - 12:51 PM

Hello everyone. I'm currently taking a basic Java course and have been given an assignment to create a fully automated Tic Tac Toe game with a GUI. We have been provided with .png files for X,O, and an empty frame. Each game is supposed to randomly place 4 X's, 4 O's, and one empty grid every time, and then output a message to say if X won, O won, both won, or it was a draw. The following is the code I've managed so far:

// Imports libraries necessary for creation of GUI
import javax.swing.*;
import java.awt.*;

public class TicTacToe extends JFrame {
    // Prepares the X, O, and empty image to be used in the GUI
    private ImageIcon xIcon = new ImageIcon("src/x.png");
    private ImageIcon oIcon = new ImageIcon("src/o.png");
    private ImageIcon eIcon = new ImageIcon("src/e.png");

    // Creates the frame
    public TicTacToe() {
        JPanel p1 = new JPanel();   // Creates a panel
        p1.setLayout(new GridLayout(3,3,5,5));  // Sets up the frame with a 3x3 grid
        p1.setBackground(Color.GREEN);  // Sets a green background

        // **JUST TO CHECK IF IMAGES WORK**. Code not correct.
        for (int i = 1; i <=9; i++) {
            p1.add(new JButton(oIcon));
        }

        // Creates second panel
        JPanel p2 = new JPanel(new BorderLayout()); // Creates a border layout
        p2.add(p1, BorderLayout.SOUTH); // Sets up location of p1

        // Creates a font type
        Font font1 = new Font("Serif", Font.BOLD, 24);

        //Sets up location of p2
        add(p2, BorderLayout.NORTH);

        // Creates a label to hold the result of the game
        JLabel result = new JLabel("Result Here", JLabel.CENTER);

        // Sets the color and font of the label
        result.setBackground(Color.RED);
        result.setForeground(Color.BLACK);
        result.setFont(font1);

        add(result);    // Adds result to the GUI

    }

    // Main function
    public static void main(String[] args) {
        TicTacToe frame = new TicTacToe();
        frame.setTitle("Tic Tac Toe");
        frame.setSize(400, 350);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}



So I've got the basic format of the GUI outputting correctly, however I am unsure how to make the program randomly place 4 X's, O's and 1 empty image into the given grid. I am also unsure of how to determine and output the result. Any help would be much appreciated. Look forward to hearing from you.

Is This A Good Question/Topic? 0
  • +

Replies To: Fully Automated Tic Tac Toe

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,839
  • Joined: 05-April 11

Re: Fully Automated Tic Tac Toe

Posted 02 October 2012 - 12:57 PM

I would advice you to use a GridLayout for your board :) Just make a 3x3 grid

You could use JLabels for each cell. JLabels are capable of showing pictures :)!

To do the random placing, you could either look into using the Random class or the Math.random method
Was This Post Helpful? 1
  • +
  • -

#3 MitulP91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 18-July 12

Re: Fully Automated Tic Tac Toe

Posted 02 October 2012 - 12:59 PM

I have the GridLayout for the board. The BorderLayout is simply to have the board above and a message on the bottom indicating who won. There is a way to use a Math.random method to randomly place images into a grid? I'll look for it. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1291
  • View blog
  • Posts: 2,859
  • Joined: 30-January 11

Re: Fully Automated Tic Tac Toe

Posted 02 October 2012 - 01:02 PM

Using the Random class will probably be easier for that.
Was This Post Helpful? 1
  • +
  • -

#5 MitulP91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 18-July 12

Re: Fully Automated Tic Tac Toe

Posted 02 October 2012 - 01:07 PM

Just thinking out loud here, but is it possible for me to use the Random class to pick a random number from an array (numbers from 1 to 9), have it assign an image to that number, and eliminate that number from the selection so the process can be repeated? Otherwise isn't it possible that using the random class with cause a repetition of the same number?
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,839
  • Joined: 05-April 11

Re: Fully Automated Tic Tac Toe

Posted 02 October 2012 - 01:09 PM

View PostMitulP91, on 02 October 2012 - 07:59 PM, said:

I have the GridLayout for the board. The BorderLayout is simply to have the board above and a message on the bottom indicating who won. There is a way to use a Math.random method to randomly place images into a grid? I'll look for it. Thanks.


Oh sorry my bad.

You could do that.

I would have used a 1D array with all the X & Os and shuffled the array. I would then add 8 JLabels in the sequence they appear in the array.
I would do it this way because it will be easier for ME, and I am able to visualize it all in my head.
You should do it the way that seems most straight forward for you :)
Was This Post Helpful? 1
  • +
  • -

#7 MitulP91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 18-July 12

Re: Fully Automated Tic Tac Toe

Posted 02 October 2012 - 01:10 PM

Ahh thank you CasiOo. That makes sense to me. I will attempt to do something along those lines. I appreciate all the help guys. Any of you have any ideas as to determining and outputting the result?
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,839
  • Joined: 05-April 11

Re: Fully Automated Tic Tac Toe

Posted 02 October 2012 - 01:37 PM

View PostMitulP91, on 02 October 2012 - 08:10 PM, said:

Ahh thank you CasiOo. That makes sense to me. I will attempt to do something along those lines. I appreciate all the help guys. Any of you have any ideas as to determining and outputting the result?


To begin with you won't need a smart way to determine the winner.
Just do it like you would when playing the game physically, check every horizontal row, check every vertical row, check diagonals.
Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Fully Automated Tic Tac Toe

Posted 02 October 2012 - 01:54 PM

Just put your ImageIcon in an ArrayList<ImageIcon>
and then randomly remove from them

    ArrayList<ImageIcon> al = new ArrayList<ImageIcon>();
    al.add(new ImageIcon(....
    al.add(new ImageIcon(....
    al.add(new ImageIcon(....
    al.add(new ImageIcon(....

    Random ran = new Random();
    while(al.size() > 0) {
       int index = ran.nextInt(al.size());
       ImageIcon ic = al.remove(index);
       ... do what ever you want with ic
    }


Was This Post Helpful? 2
  • +
  • -

#10 MitulP91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 18-July 12

Re: Fully Automated Tic Tac Toe

Posted 02 October 2012 - 02:25 PM

Thank you PBL, but I managed it earlier. I apologize for not posting and telling you all. I did the following to get it to work:

ImageIcon[] image =
        {xIcon, xIcon, xIcon, xIcon, oIcon, oIcon, oIcon, oIcon, eIcon};

        Collections.shuffle(Arrays.asList(image));

        for (int i = 1; i <=9; i++) {
            p1.add(new JLabel(image[i-1]));
        }



Thank you all for your help. I currently have my result set up as a text field so I can enter the result manually in the GUI, however I would still like to find a method to analyze the randomized game results and output a String according to that. Can't find a way to get it to work though...anyone have some ideas?
Was This Post Helpful? 1
  • +
  • -

#11 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,839
  • Joined: 05-April 11

Re: Fully Automated Tic Tac Toe

Posted 02 October 2012 - 02:32 PM

View PostMitulP91, on 02 October 2012 - 09:25 PM, said:

Thank you PBL, but I managed it earlier. I apologize for not posting and telling you all. I did the following to get it to work:

ImageIcon[] image =
        {xIcon, xIcon, xIcon, xIcon, oIcon, oIcon, oIcon, oIcon, eIcon};

        Collections.shuffle(Arrays.asList(image));

        for (int i = 1; i <=9; i++) {
            p1.add(new JLabel(image[i-1]));
        }



Thank you all for your help. I currently have my result set up as a text field so I can enter the result manually in the GUI, however I would still like to find a method to analyze the randomized game results and output a String according to that. Can't find a way to get it to work though...anyone have some ideas?


It was not my intention to scare you away from your original idea about 2D array shuffling ^^

It is not as hard as you might think. I have made an example to show you it
There are two solutions to shuffle the array in it, one of them is commented out
They could of course be optimized =)
import java.util.Random;

public class Random2D {

	public static void main(String[] args) {
		new Random2D();
	}

	private Random rnd = new Random();

	public Random2D() {
		char[][] board = getRandomBoard();
		print(board);
	}
	
	private char[][] getRandomBoard() {
		char[][] board = { { 'x', 'x', 'x' },
											 { 'x', 'o', 'o' },
											 { 'o', 'o', ' ' }};
		
		for (int y=0; y<board.length; y++) {
			for (int x=0; x<board[y].length; x++) {
				int nextRow = rnd.nextInt(3);
				int nextCol = rnd.nextInt(3);
				char temp = board[y][x];
				board[y][x] = board[nextRow][nextCol];
				board[nextRow][nextCol] = temp;
			}
		}
		
//		for (int prevIndex=0; prevIndex<9; prevIndex++) {
//			int nextIndex = rnd.nextInt(9); //0-8
//			int nextRow = nextIndex / 3;
//			int nextCol = nextIndex % 3;
//			char temp = board[prevIndex / 3][prevIndex % 3];
//			board[prevIndex / 3][prevIndex % 3] = board[nextRow][nextCol];
//			board[nextRow][nextCol] = temp;
//		}
		
		return board;
	}

	private void print(char[][] array) {
		for (char[] row : array) {
			for (char c : row)
				System.out.print(c);
			System.out.println();
		}
	}
}



Was This Post Helpful? 1
  • +
  • -

#12 MitulP91  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 18-July 12

Re: Fully Automated Tic Tac Toe

Posted 02 October 2012 - 02:35 PM

Hahaha thanks CasiOo, I actually got both to work. I'm trying to get a greater understanding of Java so I did both just so I knew I could. I just found the initial method to be significantly easier and concise. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1