• Log in or sign up for an account here: https://www.sythe.org/login

Tic-Tac-Toe Help [JCreator Pro]

Banned
Joined
May 15, 2007
Sythe Gold
0
Messages
6,663
Age
34
Location
Arizona
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.
 
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.
 
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.

So can you provide me with the code? I'm in beginning programming and this isn't taught to us. So yeah. :S
 
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.
 
Runescape Gamblingrunescape gambling
Back
Top