Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java tic tac toe gui based using applet

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;


public class TicTacToe extends Applet implements MouseListener 
{
    Frame f;
    int flag=2,n,m,i=0;
    char ch[]=new char[9];
    
    
    
    public TicTacToe()         
    {
        f=new Frame("Tic Tac Toe");
        f.setLayout(null);
        f.setVisible(true);
        f.setSize(600,600);
        f.addMouseListener(this); 
        for(i=0;i<9;i++)
        ch[i]='B';
    }
    public void mouseClicked(MouseEvent e)
    {
        Graphics g=f.getGraphics();
        g.drawLine(200,0,200,600);
        g.drawLine(400,0,400,600);
        g.drawLine(0,200,600,200);
        g.drawLine(0,400,600,400);
        flag--;
        int x=e.getX();
        int y=e.getY();
        if(flag==1)
        {
            if(x<200&&y<200){m=0;n=0;ch[0]='R';}
            if((x>200&&x<400)&&(y<200)){m=200;n=0;ch[1]='R';}
            if((x>400&&x<600)&&(y<200)){m=400;n=0;ch[2]='R';}
            if(x<200&&(y>200&&y<400)){m=0;n=200;ch[3]='R';}
            if((x>200&&x<400)&&(y>200&&y<400)){m=200;n=200;ch[4]='R';}
            if((x>400&&x<600)&&(y>200&&y<400)){m=400;n=200;ch[5]='R';}
            if(x<200&&(y>400&&y<600)){m=0;n=400;ch[6]='R';}
            if((x>200&&x<400)&&(y>400&&y<600)){m=200;n=400;ch[7]='R';}
            if((x>400&&x<600)&&(y>400&&y<600)){m=400;n=400;ch[8]='R';}
            g.setColor(Color.red);
            g.drawLine(m,n,m+199,n+199);
            g.drawLine(m+199,n,m,n+199);
        }
        
        if(flag==0)
        {
            
            if(x<200&&y<200){m=0;n=20;ch[0]='P';}
            if((x>200&&x<400)&&(y<200)){m=200;n=20;ch[1]='P';}
            if((x>400&&x<600)&&(y<200)){m=400;n=20;ch[2]='P';}
            if(x<200&&(y>200&&y<400)){m=0;n=200;ch[3]='P';}
            if((x>200&&x<400)&&(y>200&&y<400)){m=200;n=200;ch[4]='P';}
            if((x>400&&x<600)&&(y>200&&y<400)){m=400;n=200;ch[5]='P';}
            if(x<200&&(y>400&&y<600)){m=0;n=400;ch[6]='P';}
            if((x>200&&x<400)&&(y>400&&y<600)){m=200;n=400;ch[7]='P';}
            if((x>400&&x<600)&&(y>400&&y<600)){m=400;n=400;ch[8]='P';}
            g.setColor(Color.green);
            g.drawOval(m+10,n+10,169,169);
            //  g.drawLine(m,n,m+189,n+189);
            //  g.drawLine(m+199,n,m,n+199);
            flag=flag+2;
        }
        
        for(i=0;i<9;i++)   // for draw
        {
            if(ch[i]!='B')
            {
                if(i==8)
                draw();
            }
            else
            break;
        }
        
        for(i=0;i<3;i++)     //for vertical
        {
            //      System.out.print(ch[i]);
            if(ch[i]!='B')
            {
                if((ch[i+3]==ch[i])&&(ch[i+6]==ch[i]))
                win();
            }
        }
        for(i=0;i<7;i++)   //for horizontal
        {
            
            if(ch[i]!='B')
            {
                if((ch[i]==ch[i+1])&&(ch[i]==ch[i+2]))
                win();
                i=i+2;
            }
            else
            i=i+2;
        }
        
        if(ch[4]!='B')     //for diagonals
        {
            if(((ch[0]==ch[4])&&(ch[4]==ch[8]))||((ch[2]==ch[4])&&(ch[4]==ch[6])))
            win();
        }
        
        
    }
    public Frame win()
    {
        
        Frame m=new Frame("Result");
        Label l=new Label("you win");
        m.setLayout(null);
        m.add(l);
        l.setBounds(20,20,60,60);
        m.setVisible(true);
        m.setSize(100,100);
        return m;
    }
    
    public Frame draw()
    {
        Frame m=new Frame("Result");
        Label l1=new Label("Stalemate");
        m.setLayout(null);
        m.add(l1);
        l1.setBounds(20,20,60,60);
        m.setVisible(true);
        m.setSize(100,100);
        return m;
        
    }
    public void mouseReleased(MouseEvent e)
    {
        System.out.print("");
    }
    
    
    
    
    public void mouseEntered(MouseEvent e)
    
    {
        System.out.print("");
    }
    public void mouseExited(MouseEvent e)
    {
        System.out.print("");
    }
    public void mousePressed(MouseEvent e)
    {
        System.out.print("");
    }
    
    public static void main(String args [])
    {
        new TicTacToe();
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: Java create an object of the static class Mammal 
Java :: how to change default size kafka 
Java :: how to get visibility of element android 
Java :: Write a java program to print the ip address 
Java :: a java program must have at least one of these: 
Java :: how to create an indefilite loop in java 
Java :: java get if contains else 0 
Java :: springboot getting body value 
Java :: number pattern in java 
Java :: function name in java 
Java :: 16 kare koku 4 in java 
Java :: reset a jTable without deleting rows 
Java :: mock stream java 
Java :: add two numbers in java 
Java :: Jax-RS POST annotation 
Java :: tomcat allow remote access to manager 
Java :: How can I store user inputs in an array of integers? 
Java :: webview send to console android 
Java :: java static inner class 
Java :: Meeting time: Merging Ranges (return) 
Java :: function compose method java 8 
Java :: how to make 2 nested loops to count to 100 
Java :: intent for youtube android stackoveroverflow 
Java :: java declare and populate array 
Java :: java running sum 
Java :: Rotate array to left k cells python 
Java :: Create dynamic tree view using JavaScript 
Java :: activity selection java solution 
Java :: first method in jdbc 
Java :: how to make edittext not editable in android studio 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =