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:
TicTacToeSpot.java:
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.
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.












