import java.lang.Math;
import java.awt.*;
import java.awt.event.*;
class MyGame extends Frame implements ActionListener
{
  Frame f;
  static Image img,pi1,pi2;
  Color c1;
  Font ft;
  static int c,p=0,q=1;
  volatile int p1=0,p2=0,n=1;
  Button b1,player1,player2,start,stop,help;
  MyGame()
  {
    c1=new Color(0.2f,0.1f,0.3f);
    ft=new Font("Comic Sans MS",3,40);
    pi1=Toolkit.getDefaultToolkit().getImage("red.jpg");
    pi2=Toolkit.getDefaultToolkit().getImage("green.jpg");
    img=Toolkit.getDefaultToolkit().getImage("board.jpg");
    MediaTracker track=new MediaTracker(this);
    track.addImage(pi1,1);
    track.addImage(pi2,2);
    track.addImage(img,3);
    try{
      track.waitForID(1);
      track.waitForID(2);
      track.waitForID(3);
    }
    catch(InterruptedException e){}
    this.setLayout(null);
    setSize(1024,750);
    setVisible(true);
    setTitle("Snake and Ladder");
    b1=new Button("Number");
    b1.setEnabled(false);
    b1.setBounds(800,300,100,30);
    player1=new Button("MOVE PLAYER1");
    player1.setEnabled(false);
    player1.setBounds(800,400,100,30);
    player2=new Button("MOVE PLAYER2");
    player2.setEnabled(false);
    player2.setBounds(800,500,100,30);
    start=new Button("START");
    start.setBounds(650,300,100,30);
    stop=new Button("RESET");
    stop.setEnabled(false);
    stop.setBounds(650,400,100,30);
    help=new Button("HELP");
    help.setBounds(650,500,100,30);
    this.add(start);
    this.add(stop);
    this.add(help);
    this.add(b1);
    this.add(player1);
    this.add(player2);
    b1.addActionListener(this);
    player1.addActionListener(this);
    player2.addActionListener(this);
    start.addActionListener(this);
    stop.addActionListener(this);
    help.addActionListener(this);
    this.addWindowListener(new WindowAdapter()
                          {
                            public void windowClosing(WindowEvent we)
                            {
                              System.exit(0);
                            }
                           });
  }
  public synchronized void paint(Graphics g)
  {
    g.drawImage(img,5,30,null);
    g.setColor(c1);
    g.setFont(ft);
    g.drawString(""+c,800,270);
    g.drawString("P1:"+p1,800,370);
    g.drawString("P2:"+p2,800,470);
    g.drawString("Player 1:",600,80);
    g.drawString("Player 2:",600,150);
    g.drawImage(pi1,740,80,40,40,null);
    g.drawImage(pi2,740,150,40,40,null);
    if(p1==100)
    {
      g.drawString("PLAYER ONE WON THE GAME",100,690);
      b1.setEnabled(false);
    }
    else if(p1<100)
    {
      p=p1%10;
      q=p1/10;
      if(p==0&&p1!=p2)
        g.drawImage(pi1,(590-30),(640-(60*q)),null);
      else
        g.drawImage(pi1,(p*60-30),(590-(60*q)),null);
    }
    if(p2==100)
    {
      b1.setEnabled(false);
      g.drawString("PLAYER TWO WON THE GAME",100,690);
    }
    else if(p2<100)
    {
      p=p2%10;
      q=p2/10;
      if(p==0&&p1==p2)
        g.drawImage(pi1,(610-30),(640-(60*q)),null);
      else if(p!=0&&p1==p2)
        g.drawImage(pi1,(p*60-10),(590-(60*q)),null);
      if(p==0)
        g.drawImage(pi2,(590-30),(650-(60*q)),null);
      else
        g.drawImage(pi2,(p*60-30),(590-(60*q)),null);
     }
  }
  public void actionPerformed(ActionEvent me)
  {
    if(start==me.getSource())
    {
      stop.setEnabled(true);
      b1.setEnabled(true);
      start.setEnabled(false);
    }
    if(stop==me.getSource())
    {
      stop.setEnabled(false);
      start.setEnabled(true);
      player1.setEnabled(false);
      player2.setEnabled(false);
      b1.setEnabled(false);
      p1=0;
      p2=0;
      n=1;
      repaint();
    }
    if(help==me.getSource())
    {
      f=new Frame()
      {
      public void paint(Graphics g2)
      {
        Color c2=new Color(0.2f,0.1f,0.3f);
        Font ft2=new Font("Comic Sans MS",3,20);
        g2.setColor(c2);
        g2.setFont(ft2);
        String s[]={"Author:SREEKANTH","Org:MIC College Of Tech","Version:1.0","Date:10/01/10","Click the START button to start a new game","Click the STOP button to stop the running game","Click NUMBER button to generate random number for the player","Click MOVE PLAYER1 button to move the player1","Click MOVE PLAYER2 button to move the player2"};
        for(int i=0;i<s.length;i++)
        g2.drawString(s[i],50,50*(i+2));
      }
      };
      f.addWindowListener(new WindowAdapter()
                          {
                            public void windowClosing(WindowEvent we)
                            {
                              f.setVisible(false);
                            }
                           });
      f.setVisible(true);
      f.setSize(750,550);
      f.setTitle("Help");
    }
    if(b1==me.getSource())
    {
      c=(int)Math.round(5*Math.random())+1;
      b1.setEnabled(false);
      if(n==1)
      {
        player1.setEnabled(true);
        n=2;
      }
      else
      {
        player2.setEnabled(true);
        n=1;
      }
      repaint();
    }
    if(player1==me.getSource())
    {
      p1=calculate(p1);
      if(p1>100)
        p1=p1-c;
      if(p1==11||p1==9||p1==31||p1==29||p1==46||p1==56||p1==78||p1==17||p1==22||p1==45||p1==54||p1==66||p1==82||p1==90||p1==97||p1==91)
      {}
      else
      {
        b1.setEnabled(true);
        player1.setEnabled(false);
      }
      repaint();
      c=0;
     }
    if(player2==me.getSource())
    {
      p2=calculate(p2);
      if(p2>100)
        p2=p2-c;
      if(p2==11||p2==9||p2==31||p2==29||p2==46||p2==56||p2==78||p2==17||p2==22||p2==45||p2==54||p2==66||p2==82||p2==90||p2==97||p2==91)
      {}
      else
      {
        b1.setEnabled(true);
        player2.setEnabled(false);
      }
      repaint();
      c=0;
    }
  }
  public int calculate(int i)
  {
    switch(i)
    {
      case 11:i=23;break;
      case 9: i=36;break;
      case 31:i=52;break;
      case 29:i=69;break;
      case 46:i=79;break;
      case 56:i=83;break;
      case 78:i=89;break;
      case 17:i=6; break;
      case 22:i=2; break;
      case 45:i=28;break;
      case 54:i=15;break;
      case 66:i=50;break;
      case 82:i=33;break;
      case 90:i=19;break;
      case 97:i=55;break;
      case 91:i=21;break;
      default:i=i+c;
    }
    return i;
  }
  public static void main(String args[])
  {
    new MyGame();
  }
}
