Results 1 to 16 of 16
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default Tic Tac Toe confusion

    I really need help, here is the objective create tic tac toe basically, but it doesn't have to be clickable


    So like here are the main problems, like it does NOT work when you check if that slot is already taken. And when you draw another one the previous thing drawn gets erased how would I get past that?
    I really could use some help :)



    Main
    Java Code:
     import java.awt.*;
      import javax.swing.*;
    import java.util.Scanner;
    public class TicTacToe {
        
        /** Creates a new instance of TicTacToe */
        public TicTacToe() {
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
        
             DisplayWindow d = new DisplayWindow();
        TryoutPanel p = new TryoutPanel(Color.blue);
       
        
        
        
        d.addPanel(p);
       d.showFrame();
        
       
    
        while(p.isover()==false) {
            
            System.out.println("Enter the first cordinate X axis");  
            Scanner sc = new Scanner(System.in);
                 int i = sc.nextInt();
                 System.out.println("Enter another cordinate Y axis");
                 int m = sc.nextInt();
                 p.draweverything(i,m); //sends the cordinates to tryoutpanel
                 d.showFrame(); //this will call paintcomponent and some other shiz
                 
        }
        
    
      
        
        }
        
    }



    Java Code:
    import java.awt.*;
    import javax.swing.*;
    public  class DisplayWindow extends JFrame{
    
      private Container c;
    
      public DisplayWindow(){
        super("Tic Tac Toe");
        c = this.getContentPane();
      }
    
      public void addPanel(JPanel p){
        p.setPreferredSize(new Dimension(400,300));
        c.add(p);
      }
    
      public void showFrame(){
        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class TryoutPanel extends JPanel{
        int [][] nums = new int[4][4]; //makes an array 
        
        private Color myColor;
        int counter=0; //used to check when it's X or O's turn'
        
        int x;
        int y;
        
        public TryoutPanel(Color c){
            myColor = c;
            for(int i=0; i<4; i++) { //fills the array with 0s
                nums[i][i]=0;
                
            }
            
        }
        
        
        public void draweverything(int x, int y) {
            this.x=x;
            this.y=y; //gets the x and y cordinate from the main
            
        }
        
        public void paintComponent(Graphics g){ 
            
            
            super.paintComponent(g);
            setForeground(myColor);
            g.drawLine(133,0,133,300);
            g.drawLine(266,0,266,400);
            g.drawLine(0,100, 695,100);
            g.drawLine(0,200, 695,200); //draws the board
           
            
            
            
            if(nums[x][y]==1) {
                System.out.println("This slot is already taken<>, enter another place");
            }  //SUPPOSED to check whether or not if there is an o(I think o, but it may be X) is already there does not work
            
            if(nums[x][y]==-1) {
                System.out.println("This slot is already taken!!");//SUPPOSED to check whether or not if there is an o(I think x, but it may be o) is already there does not work
            }
            
            
            
            if(nums[x][y]==0) {
                if(counter%2==0) {
                    nums[x][y]=1;
                    if(x==1 && y==1) {
                        g.drawLine(10,10,10,10);
                    } else if(x==2&&y==2) {
                        System.out.println("HELLO");
                        g.drawLine(10,200,200,200);
                    } else if(x==3&&y==3) {
                        g.drawLine(10,250,250,250);
                    } else if(x==1 && y==2) {
                        g.drawLine(100,150,150,200);
                    }  //this part is not done supposed to check every possible combination /sigh
                    
                    
                } else {
                    nums[x][y]=-1;
                     g.drawOval(10,100,100,100);
                     
                } //this isn't done either'
                
                counter++;
            }
            
        }
    
        
        public boolean isover() {
            boolean b = false;
            return b; //checks if it's over but I havn't set it up yet'
        }
        
        
        
        /*g.drawString("top",10,50);
        g.drawLine(10,60, 200,60);
        g.drawString("middle",10,80);
        g.drawLine(10,90, 200,90);
        g.drawString("bottom",10,110);
        g.drawLine(10,120, 200,120);
        g.drawRect(200,300,100,50);
        g.drawOval(200,300,100,50);
        for(int j = 0; j < 9; j++)
        g.drawOval(50,200, 10 + 20*j, 210 - 20*j);
         */
        
        
        
        
        
        
        
    }
    When I run it I get a display but why does it say the place I want is already in use when it's not?? I mean it doesn't even work when it should..

    here look
    init:
    deps-jar:
    Compiling 2 source files to C:\Documents and Settings\Student\TicTacToe\build\classes
    compile:
    run:
    Enter the first cordinate X axis
    This slot is already taken<>, enter another place
    1
    Enter another cordinate Y axis
    1
    Enter the first cordinate X axis
    1
    Enter another cordinate Y axis
    1
    Enter the first cordinate X axis
    2
    Enter another cordinate Y axis
    2
    Enter the first cordinate X axis

  2. #2
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    First off the when you paint it draws everything in that method.

    The reason everything gets erased is because the background is being redrawn over it.
    The best solution to this is to redraw what you want to have displayed every turn or to keep the background from being redrawn if you don't need to erase anything from the screen.

    The first way you could do something like
    Java Code:
    ArrayList<Markers> list = new ArrayList<Markers>();
    list.add(new XMarker(1, 1);
    list.add(new OMaker(2,2);
    Then draw them every frame by using
    Java Code:
    for(Marker obj : list) {
        obj.draw();
    }
    Otherwise you could take out
    Java Code:
            super.paintComponent(g);
    To keep the background from being repainted (I think).

    Java Code:
    Also, I noticed you didn't actually set the 2D array to all ones.
            for(int i=0; i<4; i++) { //fills the array with 0s
                nums[i][i]=0;  
            }
    Should be:
    Java Code:
            for (int i = 0; i < nums.length; i++) {
                for (int j = 0; j < nums[i].length; j++) {
                    nums[i][j] = 0;
                }
            }
    I hope that was of some help to you.

    Good luck,

    Mr.Beans

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

    Default

    You've got your program logic inside of your paintComponent method which is something you should never do. For one, you can't fully control when paintComponent will be called as it can be called any time the operating system thinks that this portion of the GUI needs repainting. In fact, I strongly suggest that you take your program logic completely outside of the GUI. Create a TicTacToeModel class (or whatever you want to name it), that holds the array, that has a method to place an x or o in a grid position and perhaps returns a boolean to show whether this is an allowed move or not. Then build the GUI around the model. Also, I would recommend against trying to mix a console application with a GUI as it can be very difficult to do correctly, without stepping on the Swing Event Dispatch Thread (EDT).
    Last edited by Fubarable; 04-10-2009 at 02:58 AM.

  4. #4
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by Mr.Beans View Post
    First off the when you paint it draws everything in that method.

    The reason everything gets erased is because the background is being redrawn over it.
    The best solution to this is to redraw what you want to have displayed every turn or to keep the background from being redrawn if you don't need to erase anything from the screen.

    The first way you could do something like
    Java Code:
    ArrayList<Markers> list = new ArrayList<Markers>();
    list.add(new XMarker(1, 1);
    list.add(new OMaker(2,2);
    Then draw them every frame by using
    Java Code:
    for(Marker obj : list) {
        obj.draw();
    }
    Otherwise you could take out
    Java Code:
            super.paintComponent(g);
    To keep the background from being repainted (I think).

    Java Code:
    Also, I noticed you didn't actually set the 2D array to all ones.
            for(int i=0; i<4; i++) { //fills the array with 0s
                nums[i][i]=0;  
            }
    Should be:
    Java Code:
            for (int i = 0; i < nums.length; i++) {
                for (int j = 0; j < nums[i].length; j++) {
                    nums[i][j] = 0;
                }
            }
    I hope that was of some help to you.

    Good luck,

    Mr.Beans
    Thanks for the help, I changed the for loop but I dont get what you mean by

    Java Code:
    ArrayList<Markers> list = new ArrayList<Markers>();
    list.add(new XMarker(1, 1);
    list.add(new OMaker(2,2);
    Then draw them every frame by using
    Java Code:
    for(Marker obj : list) {
        obj.draw();
    }
    Otherwise you could take out
    Java Code:
            super.paintComponent(g);
    I don't get that code could you rephrase it? Or does the same effect happen just by getting rid of this super.paintComponent(g);


    To fuburable I think I know what you mean when my program was just standing still my console kept getting slammed with "This slot is already taken!!" but I wasn't pressing anything. Is this what you were talking about when it would be called when it think the screen needs to be refreshed(I dragged the window)? I don't really understand how I would separate the code like that.
    Last edited by jigglywiggly; 04-10-2009 at 07:11 AM.

  5. #5
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Oh and I'm still confused, how come when I run the program it immedietly says "This slot is already taken<>, enter another place" I mean how could it possibly do that when it should have a zero in them, not -1.

  6. #6
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Oh also I changed this to test what's going on
    Java Code:
      public void paintComponent(Graphics g){
    
    
    //        super.paintComponent(g);
          System.out.println(nums[x][y]);
            setForeground(myColor);
            g.drawLine(133,0,133,300);
            g.drawLine(266,0,266,400);
            g.drawLine(0,100, 695,100);
            g.drawLine(0,200, 695,200); //draws the board
    
    System.out.println(nums[x][y]);
    
    
            if(nums[x][y]==1) {
                System.out.println("This slot is already taken<>, enter another place");
            }  //SUPPOSED to check whether or not if there is an o(I think o, but it may be X) is already there does not work
    System.out.println(nums[x][y]);
            if(nums[x][y]==-1) {
                System.out.println("This slot is already taken!!");//SUPPOSED to check whether or not if there is an o(I think x, but it may be o) is already there does not work
                
            }
    
    
    
            if(nums[x][y]==0) {
                if(counter%2==0) {
                    nums[x][y]=1;
                  g.drawRect(100,100,100,100);
                    /*if(x==1 && y==1) {
                        g.drawLine(10,10,10,10);
                    } else if(x==2&&y==2) {
                        System.out.println("HELLO");
                        g.drawLine(10,200,200,200);
                    } else if(x==3&&y==3) {
                        g.drawLine(10,250,250,250);
                    } else if(x==1 && y==2) {
                        g.drawLine(100,150,150,200);
                    }  //this part is not done supposed to check every possible combination /sigh
    
    */
                } else {
                    nums[x][y]=-1;
                     g.drawOval(10,100,100,100);
    
                } //this isn't done either'
    
                counter++;
            }
    
        }
    But then my output makes no sense?!



    run:
    Enter the first cordinate X axis
    0
    0
    0
    1
    1
    This slot is already taken<>, enter another place
    1

    I never even gave a user input gyah D:

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

    Default

    Again, you have program logic in the paintComponent, and it doesn't belong there.

    By "program logic", I mean statements like this:
    Java Code:
            if(nums[x][y]==1) {
                System.out.println("This slot is already taken<>, enter another place");
            }  //SUPPOSED to check whether or not if there is an o(I think o, but it may be X) is already there does not work
    System.out.println(nums[x][y]);
            if(nums[x][y]==-1) {
                System.out.println("This slot is already taken!!");//SUPPOSED to check whether or not if there is an o(I think x, but it may be o) is already there does not work
                
            }
    
    
    
            if(nums[x][y]==0) {
                if(counter%2==0) {
                    nums[x][y]=1;
                  g.drawRect(100,100,100,100);
                    /*if(x==1 && y==1) {
                        g.drawLine(10,10,10,10);
                    } else if(x==2&&y==2) {
                        System.out.println("HELLO");
                        g.drawLine(10,200,200,200);
                    } else if(x==3&&y==3) {
                        g.drawLine(10,250,250,250);
                    } else if(x==1 && y==2) {
                        g.drawLine(100,150,150,200);
                    }  //this part is not done supposed to check every possible combination /sigh
    
    */
                } else {
                    nums[x][y]=-1;
                     g.drawOval(10,100,100,100);
    
                } //this isn't done either'
    
                counter++;
            }
    I imagine that you want these lines called whenever a player takes a turn, but that's not what paintComponent is for. It can't be made to be called if and only if a player takes a turn and in fact may be called many many times if you ever resize your app, or have another app overlying yours, or on a whim, the operating system just desires to repaint a portion of your apps screen. So get this code out of the paintComponent section and in an actionListener or MouseListener where it belongs. Then after your logic portion runs, you have it call repaint to tell the JVM to call paintComponent.

  8. #8
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Again, you have program logic in the paintComponent, and it doesn't belong there.

    By "program logic", I mean statements like this:
    Java Code:
            if(nums[x][y]==1) {
                System.out.println("This slot is already taken<>, enter another place");
            }  //SUPPOSED to check whether or not if there is an o(I think o, but it may be X) is already there does not work
    System.out.println(nums[x][y]);
            if(nums[x][y]==-1) {
                System.out.println("This slot is already taken!!");//SUPPOSED to check whether or not if there is an o(I think x, but it may be o) is already there does not work
                
            }
    
    
    
            if(nums[x][y]==0) {
                if(counter%2==0) {
                    nums[x][y]=1;
                  g.drawRect(100,100,100,100);
                    /*if(x==1 && y==1) {
                        g.drawLine(10,10,10,10);
                    } else if(x==2&&y==2) {
                        System.out.println("HELLO");
                        g.drawLine(10,200,200,200);
                    } else if(x==3&&y==3) {
                        g.drawLine(10,250,250,250);
                    } else if(x==1 && y==2) {
                        g.drawLine(100,150,150,200);
                    }  //this part is not done supposed to check every possible combination /sigh
    
    */
                } else {
                    nums[x][y]=-1;
                     g.drawOval(10,100,100,100);
    
                } //this isn't done either'
    
                counter++;
            }
    I imagine that you want these lines called whenever a player takes a turn, but that's not what paintComponent is for. It can't be made to be called if and only if a player takes a turn and in fact may be called many many times if you ever resize your app, or have another app overlying yours, or on a whim, the operating system just desires to repaint a portion of your apps screen. So get this code out of the paintComponent section and in an actionListener or MouseListener where it belongs. Then after your logic portion runs, you have it call repaint to tell the JVM to call paintComponent.
    Oh the hard way probably the best way though, I guess I'll start on that tomorrow. Thanks for the suggestions :o

  9. #9
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class TryoutPanel extends JPanel{
        int [][] nums = new int[4][4]; //makes an array
    
        private Color myColor;
        int counter=0; //used to check when it's X or O's turn'
    
        int x;
        int y;
    
    
        public void checker() {
        for (int i = 0; i < nums.length; i++) {
                for (int j = 0; j < nums[i].length; j++) {
                    nums[i][j] = 0;
                }
    
        }
        }
    
    
    
        public void checker2() {
            if(nums[x][y]==1) {
                System.out.println("This slot is already taken!!");
    
            }
            if(nums[x][y]==-1) {
                System.out.println("This slot is already taken<>");
            }
    
        }
    
    
        public TryoutPanel(Color c){
            myColor = c;
            int count = 1;
    
            }
    
    
    
        public void draweverything(int x, int y) {
            this.x=x;
            this.y=y; //gets the x and y cordinate from the main
    
        }
    
        public void paintComponent(Graphics g){
    
    
               super.paintComponent(g);
            setForeground(myColor);
            g.drawLine(133,0,133,300);
            g.drawLine(266,0,266,400);
            g.drawLine(0,100, 695,100);
            g.drawLine(0,200, 695,200); //draws the board
    
    
            if(nums[x][y]==0) {
                if(counter%2==0) {
                    nums[x][y]=1;
                    if(x==1 && y==1) {
                        g.drawLine(10,10,10,10);
                    } else if(x==2&&y==2) {
    
                        g.drawLine(10,200,200,200);
                    } else if(x==3&&y==3) {
                        g.drawLine(10,250,250,250);
                    } else if(x==1 && y==2) {
                        g.drawLine(100,150,150,200);
                    }  //this part is not done supposed to check every possible combination /sigh
    
    
                } else {
                    nums[x][y]=-1;
                    g.drawOval(10,100,100,100);
    
                } //this isn't done either'
    
                counter++;
            }
    
        }
    
    
        public boolean isover() {
            boolean b = false;
            return b; //checks if it's over but I havn't set it up yet'
        }
    
    
    
        /*g.drawString("top",10,50);
        g.drawLine(10,60, 200,60);
        g.drawString("middle",10,80);
        g.drawLine(10,90, 200,90);
        g.drawString("bottom",10,110);
        g.drawLine(10,120, 200,120);
        g.drawRect(200,300,100,50);
        g.drawOval(200,300,100,50);
        for(int j = 0; j < 9; j++)
        g.drawOval(50,200, 10 + 20*j, 210 - 20*j);
         */
    
    
    
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.Scanner;
    public class TicTacToe {
    
        /** Creates a new instance of TicTacToe */
        public TicTacToe() {
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    
            DisplayWindow d = new DisplayWindow();
            TryoutPanel p = new TryoutPanel(Color.blue);
    
    
    
    
            d.addPanel(p);
            d.showFrame();
            p.checker();
    
    
    
            while(p.isover()==false) {
    
                System.out.println("Enter the first cordinate X axis");
                Scanner sc = new Scanner(System.in);
                int i = sc.nextInt();
                System.out.println("Enter another cordinate Y axis");
                int m = sc.nextInt();
                p.draweverything(i,m); //sends the cordinates to tryoutpanel
                p.checker2(); //checks if they are already taken
                d.showFrame(); //this will call paintcomponent and some other stuff
    
    
            }
    
    
    
    
        }
    
    }

    I tried changing TryoutPanel and I moved some of the code out of it. My java instructor said I don't see why putting code in paintcompoennt wouldn't be a problem just move the systme.out.prints but I don't know if that's right. Because this program is like exploding lol.

    Weird part is school pc uses netbeans 5.5, I use 6.5 at home. They draw different things... like at my house it doesn't draw the circle or X but it draws the board. At school it draws the circle but usually not the X

    Can anyone help me fix it? Also at school, the program would tell me if I already have entered that input before which is good, but at home it doesn't? I am totally confused help is appreciated :D And my main quesiton is how would I call paintcomponent? It wants a Graphics G. Like if I had another method telling it what to do, it would work sort of... how would I run paint component? It wants a Graphics G? And how would I tell it what to draw D:
    Last edited by jigglywiggly; 04-11-2009 at 12:07 AM.

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

    Default

    You've got a thread in the forums.sun.com that needs replying to:
    Swing - TicTacToe drawing not centering correctly

  11. #11
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Ain't my thread.

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

    Default

    did they copy from you? or else same school class? as much of the code is very similar. This confuses me.

  13. #13
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    I really have no idea, but I'll tell you that I didn't make that thread.

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

    Default

    I believe you. But I'm still confused.
    I'm Encephalopathic by the way.

  15. #15
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Oh wait I think I found out out the problem, when I want something new to be displayed I have to refresh the screen. I have to refresh the screen for some stuff to show up, it's so weird. checker2 needs the screen to be refreshed I know that.

    Also most importantly, how do I make the previous drawlines not get over written? Mr.Beans provided some code above, but I don't understand it D: (BTW I got everything else working, isover setup, the drawing setup it all works) I just need to fix it so it remembers where it draws the stuff before it refreshes. Because when it refreshes I lose my old progress.
    Last edited by jigglywiggly; 04-11-2009 at 07:34 AM.

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

    Default

    If you're still having problems, please post your latest code. Best of luck.

Similar Threads

  1. Confusion over arrays
    By dbashby in forum New To Java
    Replies: 5
    Last Post: 04-04-2009, 10:38 PM
  2. [SOLVED] Confusion with classes (Please Help!)
    By kbullard516 in forum New To Java
    Replies: 8
    Last Post: 02-24-2009, 04:50 AM
  3. JList A real confusion.
    By darshak_patel in forum AWT / Swing
    Replies: 8
    Last Post: 02-07-2009, 01:17 AM
  4. System.in.read() confusion
    By ankurdh in forum New To Java
    Replies: 5
    Last Post: 01-08-2009, 09:28 AM
  5. bracket confusion...?
    By gallimaufry in forum New To Java
    Replies: 4
    Last Post: 10-28-2008, 12:17 PM

Posting Permissions

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