No older revisions available
No older revisions available
~cpp
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.*;
import javax.swing.*;
public class FirstJava extends JFrame{
int x, y;
int missClicked = 0;
int arr[][] = new int[3][3];
public void init() {
for ( int i=0;i<3;i++ ) {
for ( int j=0;j<3;j++ )
arr[i][j] = 0;
}
}
int iNyou = 1;
public FirstJava()
{
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
x = e.getX();
y = e.getY();
if(x>0 && x<200 && y>0&&y<200)
{
if ( arr[0][0]==0 ) {
arr[0][0]=iNyou;
missClicked = 0;
}
else if (arr[0][0]!=0){
missClicked = 1;
}
}
if(x>200 && x<400 && y>0 && y<200)
{
if ( arr[0][1]==0 ) {
arr[0][1]=iNyou;
missClicked = 0;
}
else
missClicked = 1;
}
if(x>400 && x<600 && y>0 && y<200)
{
if ( arr[0][2]==0 ) {
arr[0][2]=iNyou;
missClicked = 0;
}
else
missClicked = 1;
}
if(x>0 && x<200 && y>200 && y<400)
{
if ( arr[1][0]==0 ) {
arr[1][0]=iNyou;
missClicked = 0;
}
else
missClicked = 1;
}
if(x>200 && x<400 && y>200 && y<400)
{
if ( arr[1][1]==0 ) {
arr[1][1]=iNyou;
missClicked = 0;
}
else
missClicked = 1;
}
if(x>400 && x<600 && y>200 && y<400)
{
if ( arr[1][2]==0 ) {
arr[1][2]=iNyou;
missClicked = 0;
}
else
missClicked = 1;
}
if(x>0 && x<200 && y>400 && y<600)
{
if ( arr[2][0]==0 ) {
arr[2][0]=iNyou;
missClicked = 0;
}
else
missClicked = 1;
}
if(x>200 && x<400 && y>400 && y<600)
{
if ( arr[2][1]==0 ) {
arr[2][1]=iNyou;
missClicked = 0;
}
else
missClicked = 1;
}
if(x>400 && x<600 && y>400 && y<600)
{
if ( arr[2][2]==0 ) {
arr[2][2]=iNyou;
missClicked = 0;
}
else
missClicked = 1;
}
if ( missClicked == 0 ) {
if(iNyou==1)
iNyou = 2;
else if(iNyou==2)
iNyou = 1;
}
repaint();
}
});
}
public static void main(String args[]) {
FirstJava helloWorld = new FirstJava();
helloWorld.setBounds(100,100,600,600);
helloWorld.show();
}
public void paint(Graphics g)
{
super.paint(g);
g.drawLine(0,200,600,200);
g.drawLine(0,400,600,400);
g.drawLine(200,0,200,600);
g.drawLine(400,0,400,600);
if(arr[0][0]==1)
{
g.drawLine(0,0,200,200);
g.drawLine(0,200,200,0);
}
if(arr[0][1]==1)
{
g.drawLine(200,0,400,200);
g.drawLine(200,200,400,0);
}
if(arr[0][2]==1)
{
g.drawLine(400,0,600,200);
g.drawLine(400,200,600,0);
}
if(arr[1][0]==1)
{
g.drawLine(0,200,200,400);
g.drawLine(0,400,200,200);
}
if(arr[1][1]==1)
{
g.drawLine(200,200,400,400);
g.drawLine(400,200,200,400);
}
if(arr[1][2]==1)
{
g.drawLine(400,200,600,400);
g.drawLine(600,200,400,400);
}
if(arr[2][0]==1)
{
g.drawLine(0,400,200,600);
g.drawLine(0,600,200,400);
}
if(arr[2][1]==1)
{
g.drawLine(200,400,400,600);
g.drawLine(400,400,200,600);
}
if(arr[2][2]==1)
{
g.drawLine(600,400,400,600);
g.drawLine(400,400,600,600);
}
if(arr[0][0]==2)
{
g.drawOval(0,0,200,200);
}
if(arr[0][1]==2)
{
g.drawOval(200,0,200,200);
}
if(arr[0][2]==2)
{
g.drawOval(400,0,200,200);
}
if(arr[1][0]==2)
{
g.drawOval(0,200,200,200);
}
if(arr[1][1]==2)
{
g.drawOval(200,200,200,200);
}
if(arr[1][2]==2)
{
g.drawOval(400,200,200,200);
}
if(arr[2][0]==2)
{
g.drawOval(0,400,200,200);
}
if(arr[2][1]==2)
{
g.drawOval(200,400,200,200);
}
if(arr[2][2]==2)
{
g.drawOval(400,400,200,200);
}
}
}