Results 1 to 11 of 11
  1. #1
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default Tic Tac Toe Game

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


    public class TicTacToe implements ActionListener {

    //Create a gridlayout (3x3)
    //Determines if either the x or the o goes first
    //Determines if either the x or the o wins
    //If the X win, create a message say either the x or o win/
    //Else is a draw game

    public TicTacToe(){

    JPanel frame=new JPanel();

    LayoutManager grid = new GridLayout(3, 3);
    frame.setLayout(grid);


    setSize(500, 500); //Size of the Frame
    setLocationRelativeTo(null); // Center the Frame
    setDefaultCloseOperation(EXIT_ON_CLOSE); //Close
    frame.setLayout(new GridLayout(3, 3)); // Create Grid(row,column)_
    ImageIcon o= new ImageIcon("cross.gif");
    ImageIcon x= new ImageIcon("not.gif");

    JFrame window = new JFrame();
    JButton button1 = new JButton("");
    JButton button2 = new JButton("");
    JButton button3 = new JButton("");
    JButton button4 = new JButton("");
    JButton button5 = new JButton("");
    JButton button6 = new JButton("");
    JButton button7 = new JButton("");
    JButton button8 = new JButton("");
    JButton button9 = new JButton("");
    String letter = "";
    private int count = 0;
    private boolean win = true;







    window.add(button1);
    window.add(button2);
    window.add(button3);
    window.add(button4);
    window.add(button5);
    window.add(button6);
    window.add(button7);
    window.add(button8);
    window.add(button9);



    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);
    button6.addActionListener(this);
    button7.addActionListener(this);
    button8.addActionListener(this);
    button9.addActionListener(this);


    window.setVisible(true);
    }

    public void actionPerformed(ActionEvent a) {
    count++;

    ImageIcon o= new ImageIcon("cross.gif");
    ImageIcon x=new ImageIcon("not.gif");


    if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
    letter="not.gif";

    } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
    letter = "cross.gif";
    }

    /*Display X's or O's on the buttons*/
    if(a.getSource() == button1){
    button1.setText(letter);
    button1.setEnabled(false);
    } else if(a.getSource() == button2){
    button2.setText(letter);
    button2.setEnabled(false);
    } else if(a.getSource() == button3){
    button3.setText(letter);
    button3.setEnabled(false);
    } else if(a.getSource() == button4){
    button4.setText(letter);
    button4.setEnabled(false);
    } else if(a.getSource() == button5){
    button5.setText(letter);
    button5.setEnabled(false);
    } else if(a.getSource() == button6){
    button6.setText(letter);
    button6.setEnabled(false);
    } else if(a.getSource() == button7){
    button7.setText(letter);
    button7.setEnabled(false);
    } else if(a.getSource() == button8){
    button8.setText(letter);
    button8.setEnabled(false);
    } else if(a.getSource() == button9){
    button9.setText(letter);
    button9.setEnabled(false);
    }


    if( button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != ""){
    win = true;
    }
    else if(button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != ""){
    win = true;
    }
    else if(button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != ""){
    win = true;
    }


    else if(button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != ""){
    win = true;
    }
    else if(button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != ""){
    win = true;
    }
    else if(button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != ""){
    win = true;
    }


    else if(button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != ""){
    win = true;
    }
    else if(button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != ""){
    win = true;
    }
    else {
    win = false;
    }

    /*Show a dialog if someone wins or the game is tie*/


    if(win == true){
    JOptionPane.showMessageDialog(null, letter + " WINS!");
    } else if(count == 9 && win == false){
    JOptionPane.showMessageDialog(null,"DRAW GAME!");
    }
    }

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


    I have the following error:

    TicTacToe.java:42: illegal start of expression
    private int count = 0;
    ^
    TicTacToe.java:43: illegal start of expression
    private boolean win = true;
    ^

    And the other issue I am trying to figure out is instead of using the letter o and x, how will i be able to use images instead?

  2. #2
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    I would like to know what do i have to do to get rid of the error, and how can i write the program to use images instead of letter o and x for the game.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The error looks to be coming from using the private access specifier for a variable that is declared in your constructor. Since the variable is local to the constructor, private has no real meaning. Perhaps you meant to declare this and some other variables in the class and not in the constructor -- ask yourself, will you need to reference the variable outside of the constructor?

    Regarding using images, I'd first worry about fixing your bugs before worrying about adding bells and whistles.

  4. #4
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    Another tic tac toe game :D!
    Quote Originally Posted by hoangx87 View Post

    I have the following error:

    TicTacToe.java:42: illegal start of expression
    private int count = 0;
    ^
    TicTacToe.java:43: illegal start of expression
    private boolean win = true;
    ^

    And the other issue I am trying to figure out is instead of using the letter o and x, how will i be able to use images instead?
    Somebody correct me if I'm wrong, but I believe its not possible to define private variables in a public constructor, as then the scope of the variables is tighter than the scope of the method/constructor.

    Move those variables right under the class declaration, that is, right under here:

    Java Code:
    public class TicTacToe implements ActionListener {
    And initialize them in the constructor, so inside it do:
    Java Code:
    this.count = 0;
    this.win = true;
    You can load images for your frame using

    Java Code:
    ((Graphics2D)frame.getGraphics()).drawImage(nameOfImage, xCoordinate, yCoordinate, this);
    where:
    Java Code:
    Image nameOfImage = Toolkit.getDefaultToolkit().getImage("src/Image.jpg");
    provided your classes are in the src package, and you place the image there as well.

    Essentially try adding this method to your code and running it. You'll see how you can draw image this way(but won't be doublebuffered without doing additional stuff).


    Java Code:
        public void paint(Graphics g){        
    
            Graphics2D g2d = (Graphics2D) g;
            Image temp = Toolkit.getDefaultToolkit().getImage("src/Sprites/NFS.jpg");
              g2d.drawImage(temp, 50, 50, this);
    }
    (wrote above from memory but I think it should work).

  5. #5
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    @maximus, i still dont understand what you mean. The 2 image file are name cross.gif and not.gif. Right now it is using the letter o and x. I just want to remove the letter and replace it with cross.gif and not.gif.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Sorry, but I think you should ignore what maximus said. The easiest way to use images is to make your grid a grid JLabels and create three ImageIcons, one that has a blank image, one a cross and one an O image. Then simply call the setIcon method for the JLabel of interest and pass in the appropriate icon. To clear the board, call setIcon for all the JLabels and pass in the blank ImageIcon.

    But again, fix the program logic before trying the fancier GUI stuff.

  7. #7
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    because of that, will i have to rewrite my entire program over?

  8. #8
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

    this is what i have at the moment.

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


    public class TicTacToe implements ActionListener {
    int count = 0;
    boolean win = false;


    JFrame frame = new JFrame();
    JButton b1 = new JButton("");
    JButton b2 = new JButton("");
    JButton b3 = new JButton("");
    JButton b4 = new JButton("");
    JButton b5 = new JButton("");
    JButton b6 = new JButton("");
    JButton b7 = new JButton("");
    JButton b8 = new JButton("");
    JButton b9 = new JButton("");
    String letter = "";


    public TicTacToe(){

    this.count = 0;
    this.win = true;



    frame.setSize(500,500);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setLayout(new GridLayout(3,3));
    ImageIcon o= new ImageIcon("cross.gif");
    ImageIcon x= new ImageIcon("not.gif");



    frame.add(b1);
    frame.add(b2);
    frame.add(b3);
    frame.add(b4);
    frame.add(b5);
    frame.add(b6);
    frame.add(b7);
    frame.add(b8);
    frame.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);


    frame.setVisible(true);



    }

    public void actionPerformed(ActionEvent a) {
    count++;


    if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9){
    letter = "X";
    } else if(count == 2 || count == 4 || count == 6 || count == 8 || count == 10){
    letter = "O";
    }


    if(a.getSource() == b1){
    b1.setText(letter);
    b1.setEnabled(false);
    } else if(a.getSource() == b2){
    b2.setText(letter);
    b2.setEnabled(false);
    } else if(a.getSource() == b3){
    b3.setText(letter);
    b3.setEnabled(false);
    } else if(a.getSource() == b4){
    b4.setText(letter);
    b4.setEnabled(false);
    } else if(a.getSource() == b5){
    b5.setText(letter);
    b5.setEnabled(false);
    } else if(a.getSource() == b6){
    b6.setText(letter);
    b6.setEnabled(false);
    } else if(a.getSource() == b7){
    b7.setText(letter);
    b7.setEnabled(false);
    } else if(a.getSource() == b8){
    b8.setText(letter);
    b8.setEnabled(false);
    } else if(a.getSource() == b9){
    b9.setText(letter);
    b9.setEnabled(false);
    }


    if( b1.getText() == b2.getText() && b2.getText() == b3.getText() && b1.getText() != ""){
    win = true;
    }
    else if(b4.getText() == b5.getText() && b5.getText() == b6.getText() && b4.getText() != ""){
    win = true;
    }
    else if(b7.getText() == b8.getText() && b8.getText() == b9.getText() && b7.getText() != ""){
    win = true;
    }


    else if(b1.getText() == b4.getText() && b4.getText() == b7.getText() && b1.getText() != ""){
    win = true;
    }
    else if(b2.getText() == b5.getText() && b5.getText() == b8.getText() && b2.getText() != ""){
    win = true;
    }
    else if(b3.getText() == b6.getText() && b6.getText() == b9.getText() && b3.getText() != ""){
    win = true;
    }


    else if(b1.getText() == b5.getText() && b5.getText() == b9.getText() && b1.getText() != ""){
    win = true;
    }
    else if(b3.getText() == b5.getText() && b5.getText() == b7.getText() && b3.getText() != ""){
    win = true;
    }
    else {
    win = false;
    }


    if(win == true){
    JOptionPane.showMessageDialog(null, letter + " WINS!");
    } else if(count == 9 && win == false){
    JOptionPane.showMessageDialog(null, "DRAW GAME");
    }
    }

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

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by hoangx87 View Post
    because of that, will i have to rewrite my entire program over?
    No, you should re-write the entire program because of its oppressive redundancies. If you used 2-D arrays, you'd be able to eliminate 60-80% of your code and reduce your bug potential 100-fold.

  10. #10
    hoangx87 is offline Member
    Join Date
    Mar 2011
    Posts
    44
    Rep Power
    0

    Default

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

    public class TicTacToeGame implements ActionListener{
    JFrame frame = new JFrame("Tic-Tac-DBZ");
    JPanel panel1 = new JPanel(new GridLayout(3, 3));
    // ImageIcon image = new ImageIcon("goku.jpg");
    int count = 0;
    boolean win = false;

    JButton buttons[] = new JButton[9];
    int[][] winCombinations = new int[][] {
    {0, 1, 2},
    {3, 4, 5},
    {6, 7, 8},
    {0, 3, 6},
    {1, 4, 7},
    {2, 5, 8},
    {0, 4, 8},
    {2, 4, 6}};
    public TicTacToeGame(){
    frame.setSize(500, 500);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setLayout(new GridLayout(1, 1));
    frame.add(panel1);
    for(int i=0; i<=8; i++){
    buttons[i] = new JButton();
    panel1.add(buttons[i]);
    buttons[i].addActionListener(this);
    }


    frame.setVisible(true);
    }



    public void actionPerformed(ActionEvent a){
    count++;
    ImageIcon image = new ImageIcon("cross.gif");
    ImageIcon image2 = new ImageIcon("not.gif");



    if(count % 2 == 0){
    buttons[count].setIcon(image);
    } else {
    buttons[count].setIcon(image2);
    }


    JButton pressedButton = (JButton)a.getSource();
    pressedButton.setIcon(image);
    pressedButton.setEnabled(false);


    for(int i=0; i<=7; i++){
    if( buttons[winCombinations[i][0]].getIcon().equals(buttons[winCombinations[i][1]].getIcon()) &&
    buttons[winCombinations[i][1]].getIcon().equals(buttons[winCombinations[i][2]].getIcon()) &&
    buttons[winCombinations[i][0]].getIcon() != image){
    win = true;
    }
    }

    if(win == true){
    JOptionPane.showMessageDialog(null, " WINS!");
    } else if(count == 9 && win == false){
    JOptionPane.showMessageDialog(null, "DRAW GAME");
    }
    }


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

    Problem I have is that it will compile. But let just say if i click on box number 1 which will show and x, in box 2 it will display and o. So what can i do to get the image to work properly?

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Did you ever go through the first link in Fubarable's signature?

    db

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. 2D Game
    By Elffus in forum Java 2D
    Replies: 58
    Last Post: 08-05-2010, 01:47 AM
  3. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 08:06 AM
  4. Game 21
    By aRTx in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2009, 12:33 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •