Tic-Tac-Toe Help [JCreator Pro]

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

Tic-Tac-Toe Help [JCreator Pro]
  1. Unread #1 - Dec 6, 2008 at 3:22 PM
  2. Feren Silver
    Joined:
    May 15, 2007
    Posts:
    6,663
    Referrals:
    8
    Sythe Gold:
    0

    Feren Silver Hero
    Banned

    Tic-Tac-Toe Help [JCreator Pro]

    I'm having some problems. I can't get it so that just one picture can be in a square (in mine, an O can take the spot X picked). Also, I need a restart button in the case that a cats game occurs.

    Here is the code I have so far:
    Code:
    /**
    // 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();
      }
     }
    
     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);
     }

    Any and all help is greatly appreciated.
     
  3. Unread #2 - Dec 7, 2008 at 11:07 AM
  4. slashshot007
    Joined:
    May 6, 2006
    Posts:
    164
    Referrals:
    0
    Sythe Gold:
    0

    slashshot007 Active Member

    Tic-Tac-Toe Help [JCreator Pro]

    I made the same exact program for my intro to java course last year, and all i did was create buttons, where the text of them was either "X" or "O". The buttons where on a 3 by 3 gridlayout.
     
  5. Unread #3 - Dec 7, 2008 at 11:02 PM
  6. Feren Silver
    Joined:
    May 15, 2007
    Posts:
    6,663
    Referrals:
    8
    Sythe Gold:
    0

    Feren Silver Hero
    Banned

    Tic-Tac-Toe Help [JCreator Pro]

    So can you provide me with the code? I'm in beginning programming and this isn't taught to us. So yeah. :S
     
  7. Unread #4 - Dec 17, 2008 at 10:42 PM
  8. Weebs
    Joined:
    Nov 5, 2005
    Posts:
    7
    Referrals:
    0
    Sythe Gold:
    0

    Weebs Newcomer

    Tic-Tac-Toe Help [JCreator Pro]

    Your main code should look more like this:
    Code:
    public void onMouseClick(Location point)
    {
     int r = (int)point.getX()/100;
     int c = (int)point.getY()/100;
     if (ttt[r][c].getSpot() != null)
     {
      	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);
      	}
     }
     else
     {
    	System.out.println("Error: That spot has already been chosen.");
     }
      checkwin();
      if (!win)isX = !isX;
      if (win)
     {
      canvas.clear();
      begin();
     }
    Notice the if I added around your isX section.

    Also, in the case of the cats game, just call your begin() method again.
     
< Quick help with Tic-Tac-Toe [JCreator Pro] | Webbrowser help >

Users viewing this thread
1 guest


 
 
Adblock breaks this site