Adblock breaks this site

Quick help with Tic-Tac-Toe [JCreator Pro]

Discussion in 'Programming General' started by Feren Silver, Dec 10, 2008.

  1. Feren Silver

    Feren Silver Hero
    Banned

    Joined:
    May 15, 2007
    Posts:
    6,663
    Referrals:
    8
    Sythe Gold:
    0
    Quick help with Tic-Tac-Toe [JCreator Pro]

    Alright. I've got my code and everything almost ready to go with my game I've been working on.

    I'm only running into one problem and that is that for example player X can choose where player O played. So if someone could help me with this code it'd be greatly appreciated.

    TicTacToe.java:
    Code:
    /**
       @Author:
          Date: Nov 28, 2006
       Teacher:
           Lab:
          Misc:
      */
    // note: when you want to port your project to the web, just copy everything
    //       in the "classes" folder that this project contains over to where you want
    
    // this is an example file within this project just to show you how a project works.
    // if you change the name of the main class (below it's TicTacToe), then
    // you'll have to edit the TicTacToe.htm file so it matches
    //
    // the TicTacToe.htm file contains an "archive" parameter that
    // helps the browswer find the objectdraw.jar
    import objectdraw.WindowController;
    import objectdraw.FilledRect;
    import objectdraw.Location;
    import objectdraw.*;
    
    import java.awt.Color;
    import java.awt.*;
    
    public class TicTacToe extends WindowController 
    {
      private TicTacToeSpot[][] ttt;
      private Image empty,x,y;
      private boolean isX,win;
    
      public void begin()
      { 
      
      
      
    	isX = true;
    	win = false;
        ttt = new TicTacToeSpot[3][3];
        empty = getImage("empty20.gif");
        x = getImage("x2.gif");
        y = getImage("o2.gif");
        
        for(int i = 0; i < 300; i+=100)
        {
        	for (int j = 0; j < 300; j+=100)
        	{
        		ttt[i/100][j/100] = new TicTacToeSpot(empty, i,j,canvas);
        	}
        }
      }
    
      public void onMouseClick(Location point)
      {
      	int r = (int)point.getX()/100;
      	int c = (int)point.getY()/100;
      	
        if (isX)
        {
        	//THIS IS WHERE THE CODE NEEDS TO BE EDITED TO FIX IT SO THAT TWO IMAGES CAN'T BE PLACED OVER ONE ANOTHER.
        	
        	ttt[r][c].setSpot('x');
        	ttt[r][c].setImage(x,r,c);
            
        }
        else
        {
            ttt[r][c].setSpot('o');
        	ttt[r][c].setImage(y,r,c);	
        }
       	checkwin();
        if (!win)isX = !isX;  
        if (win)
      	{
      		canvas.clear();
      		begin();
      	}
      	
      	int spotsLeft=0;
      	for(int q = 0; q < 3; q++)
      	{
      		for (int p = 0; p < 3; p++)
      		{
      			if (ttt[q][p].getSpot()==' ')
      			  spotsLeft++;
      		}
      		
      	}
      	if (spotsLeft==0)
      	{
      		canvas.clear();
      		begin();
      	}
      }
      
      public void checkwin()
      {
      	char spot;
      	if (isX) spot = 'x'; else  spot = 'o';
      	for (int i = 0; i < 3&& !win; i++)
      	{
      		if(ttt[i][0].getSpot()==spot &&ttt[i][1].getSpot()==spot &&ttt[i][2].getSpot()==spot)
      		{
      			win = true;
      		}
      		
      		if(ttt[0][0].getSpot()==spot &&ttt[1][1].getSpot()==spot &&ttt[2][2].getSpot()==spot)
      		{
      			win = true;
      		}
      		
            if(ttt[0][2].getSpot()==spot &&ttt[1][1].getSpot()==spot &&ttt[2][0].getSpot()==spot)
      		{
      			win = true;
      		}
    
      	}
      	for (int j = 0; j < 3 && !win;j++)
      	{
      		if(ttt[0][j].getSpot()==spot&&ttt[1][j].getSpot()==spot&&ttt[2][j].getSpot()==spot)
      		{
      			win = true;
      		}
      	}
     System.out.println(win);
    
      }
    }
    
    TicTacToeSpot.java:
    Code:
    import objectdraw.WindowController;
    import objectdraw.FilledRect;
    import objectdraw.Location;
    import objectdraw.*;
    
    import java.awt.Color;
    import java.awt.*;
    
    public class TicTacToeSpot
    {
    	private boolean isEmpty;
    	private char currentChar;
    	private VisibleImage thePic;
    	private DrawingCanvas canvas;
    	
    	
    	public TicTacToeSpot(Image pic, int x, int y, DrawingCanvas acanvas)
    	{
    		canvas = acanvas;
    		isEmpty = true;
    		currentChar = ' ';
    		thePic = new VisibleImage(pic,x,y,canvas);	
    	}
    	
    	public boolean setSpot(char cc)
    	{
    		if (isEmpty)
    		{
    			currentChar = cc;
    			isEmpty = false;	
    			return true;
    		}
    		return false;
    		
    	}
    	
    	public char getSpot()
    	{	
    		return currentChar;
    	}
    	
    	public void setImage(Image pic,int x,int y)
    	{
    		thePic = new VisibleImage(pic,x*100,y*100,canvas);
    	}
    }
    
    I haven't been able to find any guides or examples of this and would like some help. Please feel free to post here to give me an e-mail at <[email protected]>.

    Thanks.
     
  2. war833

    war833 Member

    Joined:
    Dec 11, 2008
    Posts:
    82
    Referrals:
    0
    Sythe Gold:
    0
    Quick help with Tic-Tac-Toe [JCreator Pro]

    Just add an if statement so that if its x's turn put an x; if it's o's turn put an o. Also, don't make two theads on exactly the same thing, use your other thread.
     
  3. Feren Silver

    Feren Silver Hero
    Banned

    Joined:
    May 15, 2007
    Posts:
    6,663
    Referrals:
    8
    Sythe Gold:
    0
    Quick help with Tic-Tac-Toe [JCreator Pro]

    My problem isn't that. It's say player X plays in the top left corner. When it's player O's turn, he has the ability to take that spot still. Plus just adding a if statement wouldn't do it I don't think. (What I've picked up from Java so far).
     
  4. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Quick help with Tic-Tac-Toe [JCreator Pro]

    actually, an if statement WOULD work.

    What you need to do is check if the spot that is clicked is taken, and if it is, simply return the method with the return keyword. You are just checking to see if the space is taken or not.
     
  5. Feren Silver

    Feren Silver Hero
    Banned

    Joined:
    May 15, 2007
    Posts:
    6,663
    Referrals:
    8
    Sythe Gold:
    0
    Quick help with Tic-Tac-Toe [JCreator Pro]

    Could you help me with the code? I haven't learned this stuff in my class, I'm doing this outside of class.
     
  6. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    Quick help with Tic-Tac-Toe [JCreator Pro]

    If you know how an if statement works you should be able to solve your own dilemma. It seems that you were smart enough to write all of the other code.
     
  7. Feren Silver

    Feren Silver Hero
    Banned

    Joined:
    May 15, 2007
    Posts:
    6,663
    Referrals:
    8
    Sythe Gold:
    0
    Quick help with Tic-Tac-Toe [JCreator Pro]

    My teacher gave me bits and pieces of the previous code to guide me. She's never taught us this.
     
  8. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    Quick help with Tic-Tac-Toe [JCreator Pro]

    You do not know how an if statement works? Then you should not be making this program.
     
  9. Zyloch

    Zyloch Member

    Joined:
    Apr 21, 2005
    Posts:
    63
    Referrals:
    0
    Sythe Gold:
    0
    Quick help with Tic-Tac-Toe [JCreator Pro]

    I have not read this thread, but it seems to me that you need to be more proactive. People have mentioned the if clause; if you don't know how it works, look it up. The key to succeeding in computer programming is being interested and finding things out for yourself.
     
  10. hampe-92

    hampe-92 Forum Addict

    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    Quick help with Tic-Tac-Toe [JCreator Pro]

    Code:
    if ( (1+1) == 2) //if it's true, then the code in the brackets will be executed, else it will be skipped
    {
    do whatever you want to do
    }
    else
    {
    blah blah
    }
    
    Code:
    if ( (94 * 347) < 34694)
    {
    do whatever you want to do
    }
    else if ((94 * 347) != 32618)
    {
    blah blah
    }
    else
    {
    blah blah
    }
    
    and btw, http://www.letmegooglethatforyou.com/?q=java+if+statement
     
  11. Feren Silver

    Feren Silver Hero
    Banned

    Joined:
    May 15, 2007
    Posts:
    6,663
    Referrals:
    8
    Sythe Gold:
    0
    Quick help with Tic-Tac-Toe [JCreator Pro]

    Thank you for the pointless comment. I hope you feel good that you got a +1 on your post count. You can go tell your friends how cool you are now.

    I did look it up. Thanks though.>_<


    What good did that do me? You Googled an if statement for me. That doesn't really help me. I would like if someone didn't give me a response like, "Look it up!"
     
  12. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    Quick help with Tic-Tac-Toe [JCreator Pro]

    well you have used if statements throughout the program, so if you do not know how they work, then you should not have been able to write the program

    here

    Code:
    if(spotIsTaken = true)
    {
    //disable spot. :/
    }
    
    if each spot is a button, then disable the button... make a method that checks if the spot is taken, or just use 9 booleans that set to true the instant the spot gets taken, then check if the boolean is true and if it is, then do not let them take the spot...

    Plus none of us want to do it for you, which is what you seem to be asking.

    Your setSpot has a boolean return... whats that do? if isEmpty does what it looks like it does, sets false if the spot is taken, you just need to use that to check if the spot is taken before you change it.

    i have basically just said what swan said, just slightly more detailed... i think that may be all your going to get...
     
  13. hampe-92

    hampe-92 Forum Addict

    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    Quick help with Tic-Tac-Toe [JCreator Pro]

    well, besides the link, I actually did showed you how a if statement works... :)
     
  14. war833

    war833 Member

    Joined:
    Dec 11, 2008
    Posts:
    82
    Referrals:
    0
    Sythe Gold:
    0
    Quick help with Tic-Tac-Toe [JCreator Pro]

    Dude, he used if statements in his program already. He clearly understands them...>_<
     
  15. Zyloch

    Zyloch Member

    Joined:
    Apr 21, 2005
    Posts:
    63
    Referrals:
    0
    Sythe Gold:
    0
    Quick help with Tic-Tac-Toe [JCreator Pro]

    He does not necessarily understand them if his teacher provided most of the code for him.

    To the original poster, however, I apologize. I only skimmed the thread, and I was mistaken as to what you were confused about.

    I have taken a look at the code. It seems the problem is that you call setImage() after calling setSpot(). While setSpot() has code that checks whether the spot is taken already or not, setImage() does not. Either add the extra check inside there, or call setImage() directly from setSpot().
     
  16. Feren Silver

    Feren Silver Hero
    Banned

    Joined:
    May 15, 2007
    Posts:
    6,663
    Referrals:
    8
    Sythe Gold:
    0
    Quick help with Tic-Tac-Toe [JCreator Pro]


    Thank you. That's all the help I was asking for. ;)

    Learn to read a thread before you post useless information.

    Thanks. You're one of the only people to actually post something productive here. :)
     
< I needs help x.x | Tic-Tac-Toe Help [JCreator Pro] >


 
 
Adblock breaks this site