Tic-tac-toe….program in java
I have got the idea of making the game in java just one day before the lab external.
It took 3 long days for me complete the program but i was supposed to complete it by 4h according to me.
The way i designed the game is the simple way that i follow to create any applet program, i trust the method and it works for any….
The screenshot of the program’s(game) implementation:
The CODE is:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
/*<applet code="rams" width=500 height=500>
</applet>*/
public class rams extends Applet implements MouseListener
{
int i1,i2,x,y;
boolean[] p1=new boolean[10];
boolean[] p2=new boolean[10];
int[] x1=new int[10];
int[] y1=new int[10];
int[] x2=new int[10];
int[] y2=new int[10];
public void init()
{
int i;
for(i=0;i<10;i++)
{
p1[i]=false;
p2[i]=false;
}
i1=0;
i2=0;
addMouseListener(this);
}
public void mouseEntered(MouseEvent me)
{
}
public void mouseExited(MouseEvent me)
{
}
public void mouseClicked(MouseEvent me)
{
}
public void mouseReleased(MouseEvent me)
{
}
public void mousePressed(MouseEvent me)
{
x=me.getX();
y=me.getY();
if(i1>i2)
{
if(x>100&&x<160&&y>100&&y<160)
{
x2[i2]=120;
y2[i2]=120;
p2[0]=true;
}
if(x>160&&x<220&&y>100&&y<160)
{
x2[i2]=180;
y2[i2]=120;
p2[1]=true;
}
if(x>220&&x<280&&y>100&&y<160)
{
x2[i2]=240;
y2[i2]=120;
p2[2]=true;
}
if(x>100&&x<160&&y>160&&y<220)
{
x2[i2]=120;
y2[i2]=180;
p2[3]=true;
}
if(x>160&&x<220&&y>160&&y<220)
{
x2[i2]=180;
y2[i2]=180;
p2[4]=true;
}
if(x>220&&x<280&&y>160&&y<220)
{
x2[i2]=240;
y2[i2]=180;
p2[5]=true;
}
if(x>100&&x<160&&y>220&&y<280)
{
x2[i2]=120;
y2[i2]=240;
p2[6]=true;
}
if(x>160&&x<220&&y>220&&y<280)
{
x2[i2]=180;
y2[i2]=240;
p2[7]=true;
}
if(x>220&&x<280&&y>220&&y<280)
{
x2[i2]=240;
y2[i2]=240;
p2[8]=true;
}
i2++;
}
else
{
if(x>100&&x<160&&y>100&&y<160)
{
x1[i1]=120;
y1[i1]=120;
p1[0]=true;
}
if(x>160&&x<220&&y>100&&y<160)
{
x1[i1]=180;
y1[i1]=120;
p1[1]=true;
}
if(x>220&&x<280&&y>100&&y<160)
{
x1[i1]=240;
y1[i1]=120;
p1[2]=true;
}
if(x>100&&x<160&&y>160&&y<220)
{
x1[i1]=120;
y1[i1]=180;
p1[3]=true;
}
if(x>160&&x<220&&y>160&&y<220)
{
x1[i1]=180;
y1[i1]=180;
p1[4]=true;
}
if(x>220&&x<280&&y>160&&y<220)
{
x1[i1]=240;
y1[i1]=180;
p1[5]=true;
}
if(x>100&&x<160&&y>220&&y<280)
{
x1[i1]=120;
y1[i1]=240;
p1[6]=true;
}
if(x>160&&x<220&&y>220&&y<280)
{
x1[i1]=180;
y1[i1]=240;
p1[7]=true;
}
if(x>220&&x<280&&y>220&&y<280)
{
x1[i1]=240;
y1[i1]=240;
p1[8]=true;
}
i1++;
}
repaint();
}
public void paint(Graphics g)
{
g.drawLine(100,100,100,280);
g.drawLine(100,160,280,160);
g.drawLine(100,220,280,220);
g.drawLine(100,280,280,280);
g.drawLine(160,100,160,280);
g.drawLine(220,100,220,280);
g.drawLine(280,100,280,280);
g.drawLine(100,100,280,100);
for(int e=0;e<i1;e++)
{
g.fillOval(x1[e],y1[e],40,40);
}
for(int e=0;e<i2;e++)
{
g.fillRect(x2[e],y2[e],40,40);
}
if(i1>=3)
{
if(p1[0]==true)
{
if(p1[1]==true)
{
if(p1[2]==true)
{
JOptionPane.showMessageDialog(null,"Player1 won");
}
}
else if(p1[3]==true)
{
if(p1[6]==true)
{
JOptionPane.showMessageDialog(null,"Player1 won");
}
}
else if(p1[4]==true)
{
if(p1[8]==true)
{
JOptionPane.showMessageDialog(null,"Player1 won");
}
}
}
else if(p1[1]==true)
{
if(p1[4]==true)
{
if(p1[7]==true)
{
JOptionPane.showMessageDialog(null,"Player1 won");
}
}
}
else if(p1[2]==true)
{
if(p1[5]==true)
{
if(p1[8]==true)
{
JOptionPane.showMessageDialog(null,"Player1 won");
}
}
else if(p1[4]==true)
{
if(p1[6]==true)
{
JOptionPane.showMessageDialog(null,"Player1 won");
}
}
}
else if(p1[3]==true)
{
if(p1[4]==true)
{
if(p1[5]==true)
{
JOptionPane.showMessageDialog(null,"Player1 won");
}
}
}
else if(p1[6]==true)
{
if(p1[7]==true)
{
if(p1[8]==true)
{
JOptionPane.showMessageDialog(null,"Player1 won");
}
}
}
}
if(i2>=3)
{
if(p2[0]==true)
{
if(p2[1]==true)
{
if(p2[2]==true)
{
JOptionPane.showMessageDialog(null,"Player2 won");
}
}
else if(p2[3]==true)
{
if(p2[6]==true)
{
JOptionPane.showMessageDialog(null,"Player2 won");
}
}
else if(p2[4]==true)
{
if(p2[8]==true)
{
JOptionPane.showMessageDialog(null,"Player2 won");
}
}
}
else if(p2[1]==true)
{
if(p2[4]==true)
{
if(p2[7]==true)
{
JOptionPane.showMessageDialog(null,"Player2 won");
}
}
}
else if(p2[2]==true)
{
if(p2[5]==true)
{
if(p2[8]==true)
{
JOptionPane.showMessageDialog(null,"Player2 won");
}
}
else if(p2[4]==true)
{
if(p2[6]==true)
{
JOptionPane.showMessageDialog(null,"Player2 won");
}
}
}
else if(p2[3]==true)
{
if(p2[4]==true)
{
if(p2[5]==true)
{
JOptionPane.showMessageDialog(null,"Player2 won");
}
}
}
else if(p2[6]==true)
{
if(p2[7]==true)
{
if(p2[8]==true)
{
JOptionPane.showMessageDialog(null,"Player2 won");
}
}
}
}
if(i1==4&&i2==4)
{
JOptionPane.showMessageDialog(null,"Game draw");
}
}
}
June 1, 2010 at 7:50 am
wow. . . . . Super man u had got a gr8 idea keep it up man.
January 9, 2011 at 8:50 pm
Nice job, but whef, that’s a lot to scroll! Have you tried surrounding that code with <div style=”height:500px; overflow:auto”> and </div> tags? It might help you out a bit if you’re going to be posting a lot of code on this blog.
January 20, 2011 at 8:44 pm
Hi there,
I have a question for the webmaster/admin here at rameshchalla.wordpress.com.
Can I use some of the information from your blog post right above if I give a backlink back to this site?
Thanks,
John
January 24, 2011 at 12:03 am
There are times that i dont read more than two lines but i honestly enjoyed what i read. Bravo !