Tuesday, 17 September 2013

TicTacToe

HTML Online Editor Sample

 

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tictactoe;
 
/**
 *
 * @author gaurav
 */
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
 
public class Tictactoe extends Applet implements ActionListener{
 
 Button [][]square;
 Button start,pause,reset;
 int s=0,a=0,b=0,l=1,g=0,h=0,x=0,y=0,z=0,w=0,u=0,v=0,n=0,o=0;
    @Override
public  void init()
{
    setLayout(null);
    start=new Button("START");
    start.reshape(305+25, 475,40 ,40);
    pause=new Button("Pause");
    pause.reshape(355+25,475,40,40);
    reset=new Button("RESET");
    reset.reshape(405+25,475,40,40);
    add(start);start.setBackground(Color.yellow);
    pause.setBackground(Color.GREEN);
    reset.setBackground(Color.white);reset.addActionListener(this);
    add(pause);
    add(reset);start.addActionListener(this);
    square=new Button[3][3];
    int k=1;
    for(int i=0;i<3;i++)
    {for (int j=0;j<3;j++){
    square[i][j]=new Button();
    
   square[i][j].reshape(325+50*j,300+50*i,50,50);add(square[i][j]);k++;   
   square[i][j].setBackground(Color.gray);
    square[i][j].addActionListener(this);
    
    }
}}
public void actionPerformed(ActionEvent ae)
{if(ae.getSource()==reset){
    for(int i=0;i<3;i++)
{for(int j=0;j<3;j++){square[i][j].setLabel("  ");n=1;repaint();
s=0;a=0;b=0;l=1;g=0;h=0;x=0;y=0;z=0;w=0;u=0;v=0;n=0;
    }}}
else if(ae.getSource()==start)
{ a=1; l=0;repaint();}
 
if(a==1)
 
{for(int j=0;j<3;j++)
{for(int k=0;k<3;k++){
    if(ae.getSource()==square[j][k]){
        if(b%2==0){if(square[j][k].getLabel()=="O"){JOptionPane.showMessageDialog(null, "wrong move");b--;}
        else
            square[j][k].setLabel("X");
        repaint();}
        else if(b%2==1){if(square[j][k].getLabel()=="X"){JOptionPane.showMessageDialog(null, "wrong move");b--;}
        else
            square[j][k].setLabel("O");
        repaint();}b++;check();
    }
}
 
 
 
 
}
    }if(b==9&&o==0){JOptionPane.showMessageDialog(null, "GAME IS DROW press RESET to paly again");return;}
    }
 
 
 
 
public void check(){
    g=0;h=0;x=0;y=0;z=0;u=0;v=0;w=0;o=0;
    for(int i=0;i<3;i++)
    {g=0;h=0;
        for(int j=0;j<3;j++)
    {if(square[i][j].getLabel()=="X")g++;
    else if(square[i][j].getLabel()=="O")
    h++;
        }if(g==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER ONE IS WINNER press RESET to play again");o=1;return;}
        else if(h==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER TWO IS WINNER press RESET to play again");o=1;return;}
        
    } for(int j=0;j<3;j++)
    {x=0;y=0;
        for(int i=0;i<3;i++)
    {if(square[i][j].getLabel()=="X"){x++;}
    else if(square[i][j].getLabel()=="O"){
    y++;}
        }if(x==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER ONE IS WINNER press RESET to play again");o=1;return;}
        else if(y==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER TWO IS WINNER");o=1;return;}
        
    }for(int j=0;j<3;j++)
    {
        for(int i=0;i<3;i++)
    {if(square[i][j].getLabel()=="X"&&i==j){z++;}
    else if(square[i][j].getLabel()=="O"&&i==j){
    w++;}
        }if(z==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER ONE IS WINNER press RESET to play again");o=1;return;}
        else if(w==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER TWO IS WINNER press RESET to play again");o=1;return;}
        
    } for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
    {if(square[i][j].getLabel()=="X"&&i+j==2){u++;}
    else if(square[i][j].getLabel()=="O"&&i+j==2)
    {v++;}
        }if(u==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER ONE IS WINNER press RESET to play again");o=1;return;}
        else if(v==3){JOptionPane.showMessageDialog(null, "GAME IS OVER and PLAYER TWO IS WINNER press RESET to play again");o=1;return;}
        
    }
 
 
 
 
 
 
 
 
}
 
 
public void paint(Graphics g){
    setBackground(Color.CYAN);
    g.setColor(Color.red);
    g.fillRect(200,200,400,400);
 
 
 
 
if(l==0&&n==0){
if(b%2==0&&b<9)
{g.setColor(Color.green);g.fillRect(325,225,150,40);g.setColor(Color.black);
    g.drawString("PLAYER ONE MOVE",325+25,250);}
else if(b%2==1&&b<9){g.setColor(Color.white);g.fillRect(325,225,150,40);g.setColor(Color.blue);
    g.drawString("PLAYER TWO MOVE", 325+25, 250);}
    
    
}else if(n==1){g.drawString(" ", 325+25,250);}
}}
   
    

No comments:

Post a Comment