Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Tic Tac Toe Program

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Tic Tac Toe Program

    Alright, so I have run into a bit of an error and I am not sure where I have gone wrong. I have two sets of code that should work together. The problem seems to be in the second file TicTacEvent on line 5. I read something about associating the file, but I do not fully understand what the means or how I am supposed to go about doing that.

      void b2()
      {
        clicks = clicks + 1;
        if ((clicks%2)==1)
        {
          gui.boxes[0][1].setIcon(a);
          check[0][1] = 1; 
        }
        else
        {
          gui.boxes[0][1].setIcon(b);
          check[0][1] = 2;
        }
      }

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class TicTacEvent implements ItemListener, ActionListener, Runnable
    {
      TicTac gui;
      Thread playing;
      ImageIcon a = new ImageIcon("x.jpg");
      ImageIcon b = new ImageIcon("o.jpg");
      int clicks = 0;
      int win = 0;
      int[][] check = new int[3][3];
     
      public TicTacEvent (TicTac in)
      {
        gui = in;
        for (int row=0; row<=2; row++)
        {
          for (int col=0; col<=2; col++)
          {
            check[row][col]=0;
          }
        }
      }
     
      public void actionPerformed (ActionEvent event)
      {
        String command = event.getActionCommand();
     
        if (command.equals("Play")) 
        {
          startPlaying();
        }
        if (command.equals("1")) 
        {
          b1();
        }
        if (command.equals("2")) 
        {
          b2();
        }
        if (command.equals("3")) 
        {
          b3();
        }
        if (command.equals("4")) 
        {
          b4();
        }
        if (command.equals("5")) 
        {
          b5();
        }
        if (command.equals("6")) 
        {
          b6();
        }
        if (command.equals("7")) 
        {
          b7();
        }
        if (command.equals("8")) 
        {
          b8();
        }
        if (command.equals("9")) 
        {
          b9();
        }
      }
     
      void b1()
      {
        clicks = clicks + 1;
        if ((clicks%2)==1)
        {
          gui.boxes[0][0].setIcon(a);
          check[0][0] = 1;
        }
        else
        {
          gui.boxes[0][0].setIcon(b);
          check[0][0] = 2;
        }
      }
     
      void b2()
      {
        clicks = clicks + 1;
        if ((clicks%2)==1)
        {
          gui.boxes[0][1].setIcon(a);
          check[0][1] = 1; 
        }
        else
        {
          gui.boxes[0][1].setIcon(b);
          check[0][1] = 2;
        }
      }
     
      void b3()
      {
        clicks = clicks + 1;
        if ((clicks%2)==1)
        {
          gui.boxes[0][2].setIcon(a);
          check[0][2] = 1; 
        }
        else
        {
          gui.boxes[0][2].setIcon(b);
          check[0][2] = 2;
        }
      }
     
      void b4()
      {
        clicks = clicks + 1;
        if ((clicks%2)==1)
        {
          gui.boxes[1][0].setIcon(a);
          check[1][0] = 1; 
        }
        else
        {
          gui.boxes[1][0].setIcon(b);
          check[1][0] = 2;
        }
      }
     
      void b5()
      {
        clicks = clicks + 1;
        if ((clicks%2)==1)
        {
          gui.boxes[1][1].setIcon(a);
          check[1][1] = 1; 
        }
        else
        {
          gui.boxes[1][1].setIcon(b);
          check[1][1] = 2;
        }
      }
     
      void b6()
      {
        clicks = clicks + 1;
        if ((clicks%2)==1)
        {
          gui.boxes[1][2].setIcon(a);
          check[1][2] = 1; 
        }
        else
        {
          gui.boxes[1][2].setIcon(b);
          check[1][2] = 2;
        }
      }
     
      void b7()
      {
        clicks = clicks + 1;
        if ((clicks%2)==1)
        {
          gui.boxes[2][0].setIcon(a);
          check[2][0] = 1; 
        }
        else
        {
          gui.boxes[2][0].setIcon(b);
          check[2][0] = 2;
        }
      }
     
      void b8()
      {
        clicks = clicks + 1;
        if ((clicks%2)==1)
        {
          gui.boxes[2][1].setIcon(a);
          check[2][1] = 1; 
        }
        else
        {
          gui.boxes[2][1].setIcon(b);
          check[2][1] = 2;
        }
      }
     
      void b9()
      {
        clicks = clicks + 1;
        if ((clicks%2)==1)
        {
          gui.boxes[2][2].setIcon(a);
          check[2][2] = 1; 
        }
        else
        {
          gui.boxes[2][2].setIcon(b);
          check[2][2] = 2;
        }
      }
     
      void startPlaying()
      {
        playing = new Thread(this);
        playing.start();
        gui.play.setEnabled(false);
      }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Tic Tac Toe Program

    Quote Originally Posted by shodai View Post
    a bit of an error .... something about associating the file..
    Post the full text of the error message

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe Program

    This is the error message that I am getting


    1 error found:
    File: /Users/brad/Desktop/TicTacEvent.java [line: 5]
    Error: /Users/brad/Desktop/TicTacEvent.java:5: TicTacEvent is not abstract and does not override abstract method itemStateChanged(java.awt.event.ItemEvent) in java.awt.event.ItemListener

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tic Tac Toe Program

    method itemStateChanged(java.awt.event.ItemEvent)
    The compiler is telling you to define that method in your class because you promised to do that when you coded: implements ItemListener
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Tic Tac Toe Program

    Ok, so I have been trying to get my head around this but I am clearly missing something basic. I have to be honest and just say that I just do not understand what I need to do. The standard approach to define a method would be something like public void ItemListener(), but does not seem to make sense and if it did I would have no clue where to put it. Then there is declaring an abstract class which seems to make more sense but again, I am confused by how to use it.

    I apologize as this is the most advanced thing that I have ever done and I am try to learn by putting different pieces of information together.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tic Tac Toe Program

    implements ItemListener, ActionListener, Runnable
    The class implements several interfaces. See what it did for the ActionListener interface and the Runnable interface and do the same kind of thing for the ItemListener interface's required method.

    http://docs.oracle.com/javase/tutori...interface.html
    http://docs.oracle.com/javase/tutori...interface.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help with a tic tac toe program
    By RodePope4546 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 22nd, 2012, 05:47 PM
  2. Tic Tac Toe Program, Can someone help me out????
    By KVM in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 24th, 2012, 08:11 PM
  3. Tic Tac Toe Program, Can someone help me out????
    By KVM in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2012, 04:47 PM
  4. [SOLVED] Tic-Tac-Toe program
    By Actinistia in forum Java Theory & Questions
    Replies: 2
    Last Post: April 28th, 2011, 11:18 PM